Skip to content
Permalink
 
 
Cannot retrieve contributors at this time
150 lines (128 sloc) 4.08 KB
// MvxAppCompatActivity.cs
// (c) Copyright Cirrious Ltd. http://www.cirrious.com
// MvvmCross is licensed using Microsoft Public License (Ms-PL)
// Contributions and inspirations noted in readme.md and license.txt
//
// Project Lead - Stuart Lodge, @slodge, me@slodge.com
using System;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using MvvmCross.Binding.BindingContext;
using MvvmCross.Binding.Droid.BindingContext;
using MvvmCross.Binding.Droid.Views;
using MvvmCross.Core.ViewModels;
using MvvmCross.Droid.Support.V7.AppCompat.EventSource;
using MvvmCross.Droid.Views;
namespace MvvmCross.Droid.Support.V7.AppCompat
{
[Register("mvvmcross.droid.support.v7.appcompat.MvxAppCompatActivity")]
public class MvxAppCompatActivity
: MvxEventSourceAppCompatActivity, IMvxAndroidView
{
private View _view;
protected MvxAppCompatActivity()
{
BindingContext = new MvxAndroidBindingContext(this, this);
this.AddEventListeners();
}
protected MvxAppCompatActivity(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
}
public object DataContext
{
get { return BindingContext.DataContext; }
set { BindingContext.DataContext = value; }
}
public IMvxViewModel ViewModel
{
get
{
return DataContext as IMvxViewModel;
}
set
{
DataContext = value;
OnViewModelSet();
}
}
public void MvxInternalStartActivityForResult(Intent intent, int requestCode)
{
StartActivityForResult(intent, requestCode);
}
protected virtual void OnViewModelSet()
{
}
public IMvxBindingContext BindingContext { get; set; }
public override void SetContentView(int layoutResId)
{
_view = this.BindingInflate(layoutResId, null);
SetContentView(_view);
}
protected override void AttachBaseContext(Context @base)
{
if (this is IMvxAndroidSplashScreenActivity)
{
// Do not attach our inflater to splash screens.
base.AttachBaseContext(@base);
return;
}
base.AttachBaseContext(MvxContextWrapper.Wrap(@base, this));
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
ViewModel?.ViewCreated();
}
protected override void OnDestroy()
{
base.OnDestroy();
ViewModel?.ViewDestroy(IsFinishing);
}
protected override void OnStart()
{
base.OnStart();
ViewModel?.ViewAppearing();
}
protected override void OnResume()
{
base.OnResume();
ViewModel?.ViewAppeared();
}
protected override void OnPause()
{
base.OnPause();
ViewModel?.ViewDisappearing();
}
protected override void OnStop()
{
base.OnStop();
ViewModel?.ViewDisappeared();
}
public override View OnCreateView(View parent, string name, Context context, IAttributeSet attrs)
{
var view = MvxAppCompatActivityHelper.OnCreateView(parent, name, context, attrs);
return view ?? base.OnCreateView(parent, name, context, attrs);
}
}
public abstract class MvxAppCompatActivity<TViewModel>
: MvxAppCompatActivity, IMvxAndroidView<TViewModel>
where TViewModel : class, IMvxViewModel
{
protected MvxAppCompatActivity(IntPtr ptr, JniHandleOwnership ownership)
: base(ptr, ownership)
{
}
protected MvxAppCompatActivity()
{
}
public new TViewModel ViewModel
{
get { return (TViewModel)base.ViewModel; }
set { base.ViewModel = value; }
}
}
}