Skip to content

Commit

Permalink
MvvmCross#1894 Changed GlobalLayout calls for rest of the activity ty…
Browse files Browse the repository at this point in the history
…pes in the Android projects. Also did slight changes so things like the MvxTabsFragmentActivity wouldn’t cause the Appeared event to happen twice.
  • Loading branch information
Bowman74 committed Jun 1, 2017
1 parent 2299dcf commit b4e7bba
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,9 @@ protected override void OnCreate(Bundle bundle)

base.OnCreate(bundle);

var rootView = Window.DecorView.RootView;
_view = Window.DecorView.RootView;

EventHandler onGlobalLayout = null;
onGlobalLayout = (sender, args) =>
{
rootView.ViewTreeObserver.GlobalLayout -= onGlobalLayout;
ViewModel?.Appeared();
};

rootView.ViewTreeObserver.GlobalLayout += onGlobalLayout;
_view.ViewTreeObserver.AddOnGlobalLayoutListener(this);

if (bundle == null)
HandleIntent(Intent);
Expand All @@ -90,6 +83,13 @@ protected override void OnCreate(Bundle bundle)
}
}

public override void SetContentView(int layoutResId)
{
var view = this.BindingInflate(layoutResId, null);

SetContentView(view);
}

protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
using MvvmCross.Core.ViewModels;
using System;
using MvvmCross.Droid.Support.V4.EventSource;
using Android.Views;
using Android.OS;

namespace MvvmCross.Droid.Support.V4
{
[Register("mvvmcross.droid.support.v4.MvxFragmentActivity")]
public class MvxFragmentActivity
: MvxEventSourceFragmentActivity, IMvxAndroidView
: MvxEventSourceFragmentActivity, IMvxAndroidView, ViewTreeObserver.IOnGlobalLayoutListener
{
protected View _view;

protected MvxFragmentActivity()
{
BindingContext = new MvxAndroidBindingContext(this, this);
Expand Down Expand Up @@ -60,18 +64,11 @@ protected virtual void OnViewModelSet()

public override void SetContentView(int layoutResId)
{
var view = this.BindingInflate(layoutResId, null);
_view = this.BindingInflate(layoutResId, null);

EventHandler onGlobalLayout = null;
onGlobalLayout = (sender, args) =>
{
view.ViewTreeObserver.GlobalLayout -= onGlobalLayout;
ViewModel?.Appeared();
};
_view.ViewTreeObserver.AddOnGlobalLayoutListener(this);

view.ViewTreeObserver.GlobalLayout += onGlobalLayout;

SetContentView(view);
SetContentView(_view);
}

protected override void AttachBaseContext(Context @base)
Expand All @@ -91,6 +88,24 @@ public override void OnDetachedFromWindow()
ViewModel?.Disappearing(); // we don't have anywhere to get this info
ViewModel?.Disappeared();
}

public void OnGlobalLayout()
{
if (_view != null)
{
if (Build.VERSION.SdkInt<BuildVersionCodes.JellyBean)
{
#pragma warning disable CS0618 // Type or member is obsolete
_view.ViewTreeObserver.RemoveGlobalOnLayoutListener(this);
#pragma warning restore CS0618 // Type or member is obsolete
}
else
{
_view.ViewTreeObserver.RemoveOnGlobalLayoutListener(this);
}
ViewModel?.Appeared();
}
}
}

public abstract class MvxFragmentActivity<TViewModel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Android.Views;
using Android.Widget;
using MvvmCross.Platform.Core;
using MvvmCross.Binding.Droid.BindingContext;
using MvvmCross.Core.ViewModels;
using System;
using System.Collections.Generic;
Expand All @@ -23,7 +24,9 @@ namespace MvvmCross.Droid.Support.V4
public abstract class MvxTabsFragmentActivity
: MvxFragmentActivity
, TabHost.IOnTabChangeListener
, ViewTreeObserver.IOnGlobalLayoutListener
{

private const string SavedTabIndexStateKey = "__savedTabIndex";

private readonly Dictionary<string, TabInfo> _lookup = new Dictionary<string, TabInfo>();
Expand Down Expand Up @@ -87,16 +90,9 @@ protected override void OnCreate(Bundle savedInstanceState)

SetContentView(_layoutId);

var rootView = Window.DecorView.RootView;

EventHandler onGlobalLayout = null;
onGlobalLayout = (sender, args) =>
{
rootView.ViewTreeObserver.GlobalLayout -= onGlobalLayout;
ViewModel?.Appeared();
};
_view = Window.DecorView.RootView;

rootView.ViewTreeObserver.GlobalLayout += onGlobalLayout;
_view.ViewTreeObserver.AddOnGlobalLayoutListener(this);

InitializeTabHost(savedInstanceState);

Expand All @@ -106,6 +102,13 @@ protected override void OnCreate(Bundle savedInstanceState)
}
}

public override void SetContentView(int layoutResId)
{
var view = this.BindingInflate(layoutResId, null);

SetContentView(view);
}

protected override void OnSaveInstanceState(Bundle outState)
{
outState.PutString(SavedTabIndexStateKey, _tabHost.CurrentTabTag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class MvxAppCompatActivity
, IMvxAndroidView
, ViewTreeObserver.IOnGlobalLayoutListener
{
private View _View;
private View _view;

protected MvxAppCompatActivity()
{
Expand Down Expand Up @@ -67,11 +67,11 @@ protected virtual void OnViewModelSet()

public override void SetContentView(int layoutResId)
{
_View = this.BindingInflate(layoutResId, null);
_view = this.BindingInflate(layoutResId, null);

_View.ViewTreeObserver.AddOnGlobalLayoutListener(this);
_view.ViewTreeObserver.AddOnGlobalLayoutListener(this);

SetContentView(_View);
SetContentView(_view);
}

protected override void AttachBaseContext(Context @base)
Expand All @@ -93,9 +93,9 @@ public override void OnAttachedToWindow()

public override void OnDetachedFromWindow()
{
base.OnDetachedFromWindow();
ViewModel?.Disappearing(); // we don't have anywhere to get this info
ViewModel?.Disappeared();
base.OnDetachedFromWindow();
ViewModel?.Disappearing(); // we don't have anywhere to get this info
ViewModel?.Disappeared();
}

public override View OnCreateView(View parent, string name, Context context, IAttributeSet attrs)
Expand All @@ -106,20 +106,19 @@ public override View OnCreateView(View parent, string name, Context context, IAt

public void OnGlobalLayout()
{
if (_View != null)
if (_view != null)
{
if (Build.VERSION.SdkInt < BuildVersionCodes.JellyBean)
{
if (Build.VERSION.SdkInt < BuildVersionCodes.JellyBean)
{
#pragma warning disable CS0618 // Type or member is obsolete
_View.ViewTreeObserver.RemoveGlobalOnLayoutListener(this);
_view.ViewTreeObserver.RemoveGlobalOnLayoutListener(this);
#pragma warning restore CS0618 // Type or member is obsolete
}
else
{
_View.ViewTreeObserver.RemoveOnGlobalLayoutListener(this);
}

ViewModel?.Appeared();
}
else
{
_view.ViewTreeObserver.RemoveOnGlobalLayoutListener(this);
}
ViewModel?.Appeared();
}
}
}
Expand Down
35 changes: 25 additions & 10 deletions MvvmCross/Droid/Droid/Views/MvxActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace MvvmCross.Droid.Views
using System;

using Android.Content;
using Android.OS;
using Android.Runtime;

using MvvmCross.Binding.BindingContext;
Expand All @@ -24,7 +25,10 @@ namespace MvvmCross.Droid.Views
public abstract class MvxActivity
: MvxEventSourceActivity
, IMvxAndroidView
, ViewTreeObserver.IOnGlobalLayoutListener
{
private View _view;

protected MvxActivity(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{}
Expand Down Expand Up @@ -60,18 +64,11 @@ public void MvxInternalStartActivityForResult(Intent intent, int requestCode)

public override void SetContentView(int layoutResId)
{
var view = this.BindingInflate(layoutResId, null);
_view = this.BindingInflate(layoutResId, null);

EventHandler onGlobalLayout = null;
onGlobalLayout = (sender, args) =>
{
view.ViewTreeObserver.GlobalLayout -= onGlobalLayout;
ViewModel?.Appeared();
};
_view.ViewTreeObserver.AddOnGlobalLayoutListener(this);

view.ViewTreeObserver.GlobalLayout += onGlobalLayout;

SetContentView(view);
SetContentView(_view);
}

protected virtual void OnViewModelSet()
Expand Down Expand Up @@ -101,6 +98,24 @@ public override void OnDetachedFromWindow()
ViewModel?.Disappearing(); // we don't have anywhere to get this info
ViewModel?.Disappeared();
}

public void OnGlobalLayout()
{
if (_view != null)
{
if (Build.VERSION.SdkInt < BuildVersionCodes.JellyBean)
{
#pragma warning disable CS0618 // Type or member is obsolete
_view.ViewTreeObserver.RemoveGlobalOnLayoutListener(this);
#pragma warning restore CS0618 // Type or member is obsolete
}
else
{
_view.ViewTreeObserver.RemoveOnGlobalLayoutListener(this);
}
ViewModel?.Appeared();
}
}
}

public abstract class MvxActivity<TViewModel>
Expand Down
37 changes: 26 additions & 11 deletions MvvmCross/Droid/Droid/Views/MvxTabActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ namespace MvvmCross.Droid.Views
using System.Collections.Generic;

using Android.Content;

using Android.OS;
using Android.Views;
using MvvmCross.Binding.BindingContext;
using MvvmCross.Binding.Droid.BindingContext;
using MvvmCross.Binding.Droid.Views;
Expand All @@ -26,7 +27,10 @@ public abstract class MvxTabActivity
: MvxEventSourceTabActivity
, IMvxAndroidView
, IMvxChildViewModelOwner
, ViewTreeObserver.IOnGlobalLayoutListener
{
private View _view;

private readonly List<int> _ownedSubViewModelIndicies = new List<int>();

public List<int> OwnedSubViewModelIndicies => this._ownedSubViewModelIndicies;
Expand Down Expand Up @@ -66,18 +70,11 @@ protected virtual void OnViewModelSet()

public override void SetContentView(int layoutResId)
{
var view = this.BindingInflate(layoutResId, null);

EventHandler onGlobalLayout = null;
onGlobalLayout = (sender, args) =>
{
view.ViewTreeObserver.GlobalLayout -= onGlobalLayout;
ViewModel?.Appeared();
};
_view = this.BindingInflate(layoutResId, null);

view.ViewTreeObserver.GlobalLayout += onGlobalLayout;
_view.ViewTreeObserver.AddOnGlobalLayoutListener(this);

this.SetContentView(view);
this.SetContentView(_view);
}

protected override void AttachBaseContext(Context @base)
Expand All @@ -97,6 +94,24 @@ public override void OnDetachedFromWindow()
ViewModel?.Disappearing(); // we don't have anywhere to get this info
ViewModel?.Disappeared();
}

public void OnGlobalLayout()
{
if (_view != null)
{
if (Build.VERSION.SdkInt<BuildVersionCodes.JellyBean)
{
#pragma warning disable CS0618 // Type or member is obsolete
_view.ViewTreeObserver.RemoveGlobalOnLayoutListener(this);
#pragma warning restore CS0618 // Type or member is obsolete
}
else
{
_view.ViewTreeObserver.RemoveOnGlobalLayoutListener(this);
}
ViewModel?.Appeared();
}
}
}

[Obsolete("TabActivity is obsolete. Use ViewPager + Indicator or any other Activity with Toolbar support.")]
Expand Down
23 changes: 12 additions & 11 deletions MvvmCross/Droid/FullFragging/Caching/MvxCachingFragmentActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using MvvmCross.Droid.Shared.Presenter;
using MvvmCross.Droid.Shared.Fragments;
using MvvmCross.Droid.Shared.Caching;
using MvvmCross.Binding.Droid.BindingContext;

namespace MvvmCross.Droid.FullFragging.Caching
{
Expand Down Expand Up @@ -347,20 +348,13 @@ protected virtual string GetTagFromFragment(Fragment fragment)
return mvxFragmentView.UniqueImmutableCacheTag;
}

protected override void OnCreate (Bundle bundle)
protected override void OnCreate(Bundle bundle)
{
base.OnCreate (bundle);
base.OnCreate(bundle);

var rootView = Window.DecorView.RootView;
_view = Window.DecorView.RootView;

EventHandler onGlobalLayout = null;
onGlobalLayout = (sender, args) =>
{
rootView.ViewTreeObserver.GlobalLayout -= onGlobalLayout;
ViewModel.Appeared();
};

rootView.ViewTreeObserver.GlobalLayout += onGlobalLayout;
_view.ViewTreeObserver.AddOnGlobalLayoutListener(this);

if (bundle == null) {
var fragmentRequestText = Intent.Extras?.GetString (ViewModelRequestBundleKey);
Expand All @@ -375,6 +369,13 @@ protected override void OnCreate (Bundle bundle)
}
}

public override void SetContentView(int layoutResId)
{
var view = this.BindingInflate(layoutResId, null);

SetContentView(view);
}

/// <summary>
/// Close Fragment with a specific tag at a specific placeholder
/// </summary>
Expand Down
Loading

0 comments on commit b4e7bba

Please sign in to comment.