Created
January 28, 2015 14:31
-
-
Save geirsagberg/c8d8ea32cdbae9ac344c to your computer and use it in GitHub Desktop.
MvxActionBarFragmentActivity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class MvxActionBarFragmentActivity : MvxActionBarEventSource, IMvxAndroidView, IMvxEventSourceFragmentActivity | |
{ | |
private readonly List<WeakReference<Fragment>> _fragments = new List<WeakReference<Fragment>>(); | |
public object DataContext | |
{ | |
get { return BindingContext.DataContext; } | |
set { BindingContext.DataContext = value; } | |
} | |
public IMvxViewModel ViewModel | |
{ | |
get { return DataContext as IMvxViewModel; } | |
set | |
{ | |
DataContext = value; | |
OnViewModelSet(); | |
} | |
} | |
public IMvxBindingContext BindingContext { get; set; } | |
public IList<Fragment> Fragments | |
{ | |
get | |
{ | |
var fragments = new List<Fragment>(); | |
foreach (var weakReference in _fragments) { | |
Fragment fragment; | |
if (!weakReference.TryGetTarget(out fragment)) continue; | |
if (fragment.IsVisible) | |
fragments.Add(fragment); | |
} | |
return fragments; | |
} | |
} | |
protected MvxActionBarFragmentActivity() | |
{ | |
BindingContext = new MvxAndroidBindingContext(this, this); | |
this.AddEventListeners(); | |
} | |
public void MvxInternalStartActivityForResult(Intent intent, int requestCode) | |
{ | |
StartActivityForResult(intent, requestCode); | |
} | |
protected virtual void OnViewModelSet() | |
{ | |
} | |
public override void SetContentView(int layoutResId) | |
{ | |
var view = this.BindingInflate(layoutResId, null); | |
SetContentView(view); | |
} | |
public override void OnAttachFragment(Fragment fragment) | |
{ | |
base.OnAttachFragment(fragment); | |
_fragments.Add(new WeakReference<Fragment>(fragment)); | |
} | |
} | |
public abstract class MvxActionBarFragmentActivity<TViewModel> : MvxActionBarFragmentActivity, IMvxAndroidView<TViewModel> | |
where TViewModel : class, IMvxViewModel | |
{ | |
public new TViewModel ViewModel | |
{ | |
get { return (TViewModel) base.ViewModel; } | |
set { base.ViewModel = value; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment