Created
January 28, 2015 14:30
-
-
Save geirsagberg/785da95e3fd559ee6f33 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Collections.Generic; | |
using Android.App; | |
using Android.Content; | |
using Cirrious.MvvmCross.Binding.BindingContext; | |
using Cirrious.MvvmCross.Binding.Droid.BindingContext; | |
using Cirrious.MvvmCross.Droid.Views; | |
using Cirrious.MvvmCross.ViewModels; | |
namespace ECC.Mobile.Droid.Views | |
{ | |
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