forked from martijn00/MvvmCross-AndroidSupport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMvxFragmentStatePagerAdapter.cs
95 lines (79 loc) · 3.2 KB
/
MvxFragmentStatePagerAdapter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// MvxFragmentStatePagerAdapter.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, [email protected]
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Support.V4.App;
using MvvmCross.Platform;
using MvvmCross.Core.ViewModels;
using MvvmCross.Core.Views;
using Java.Lang;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MvvmCross.Droid.Support.V4
{
internal class MvxFragmentStatePagerAdapter
: FragmentStatePagerAdapter
{
private readonly Context _context;
public IEnumerable<FragmentInfo> Fragments { get; private set; }
public override int Count => Fragments.Count();
protected MvxFragmentStatePagerAdapter(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
}
public MvxFragmentStatePagerAdapter(
Context context, FragmentManager fragmentManager, IEnumerable<FragmentInfo> fragments)
: base(fragmentManager)
{
_context = context;
Fragments = fragments;
throw new InvalidOperationException("MvxFragmentStatePagerAdapter has broken cache implementation, use MvxFragmentPagerAdapter at this moment.");
}
public override Fragment GetItem(int position)
{
var fragInfo = Fragments.ElementAt(position);
if (fragInfo.CachedFragment == null)
{
fragInfo.CachedFragment = Fragment.Instantiate(_context, FragmentJavaName(fragInfo.FragmentType));
var request = new MvxViewModelRequest (fragInfo.ViewModelType, null, null, null);
((IMvxView)fragInfo.CachedFragment).ViewModel = Mvx.Resolve<IMvxViewModelLoader>().LoadViewModel(request, null);
}
return fragInfo.CachedFragment;
}
protected static string FragmentJavaName(Type fragmentType)
{
var namespaceText = fragmentType.Namespace ?? "";
if (namespaceText.Length > 0)
namespaceText = namespaceText.ToLowerInvariant() + ".";
return namespaceText + fragmentType.Name;
}
public override ICharSequence GetPageTitleFormatted(int position)
{
return new Java.Lang.String(Fragments.ElementAt(position).Title);
}
public override void RestoreState (IParcelable state, ClassLoader loader)
{
//Don't call restore to prevent crash on rotation
//base.RestoreState (state, loader);
}
public class FragmentInfo
{
public FragmentInfo(string title, Type fragmentType, Type viewModelType)
{
Title = title;
FragmentType = fragmentType;
ViewModelType = viewModelType;
}
public string Title { get; set; }
public Type FragmentType { get; private set; }
public Type ViewModelType { get; private set; }
public Fragment CachedFragment { get; set; }
}
}
}