-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
MvxFragmentExtensions.cs
97 lines (82 loc) · 3.85 KB
/
MvxFragmentExtensions.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
95
96
97
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MS-PL license.
// See the LICENSE file in the project root for more information.
using System;
using Microsoft.Extensions.Logging;
using MvvmCross.Exceptions;
using MvvmCross.Logging;
using MvvmCross.Platforms.Android.Presenters.Attributes;
using MvvmCross.Presenters;
using MvvmCross.ViewModels;
using MvvmCross.Views;
namespace MvvmCross.Platforms.Android.Views
{
public static class MvxFragmentExtensions
{
public static Type FindAssociatedViewModelType(this IMvxFragmentView fragmentView, Type fragmentActivityParentType)
{
var viewModelType = fragmentView.FindAssociatedViewModelTypeOrNull();
var type = fragmentView.GetType();
if (viewModelType == null)
{
if (!type.HasBasePresentationAttribute())
throw new InvalidOperationException($"Your fragment of type {type.FullName} is not generic and it does not have {nameof(MvxFragmentPresentationAttribute)} attribute set!");
var cacheableFragmentAttribute = type.GetBasePresentationAttribute();
if (cacheableFragmentAttribute.ViewModelType == null)
throw new InvalidOperationException($"Your fragment of type {type.FullName} is not generic and it does not use {nameof(MvxFragmentPresentationAttribute)} with ViewModel Type constructor.");
viewModelType = cacheableFragmentAttribute.ViewModelType;
}
return viewModelType;
}
public static IMvxViewModel LoadViewModel(this IMvxFragmentView fragmentView, IMvxBundle savedState, Type fragmentParentActivityType,
MvxViewModelRequest request = null)
{
var viewModelType = fragmentView.FindAssociatedViewModelType(fragmentParentActivityType);
if (viewModelType == typeof(MvxNullViewModel))
return new MvxNullViewModel();
if (viewModelType == null
|| viewModelType == typeof(IMvxViewModel))
{
MvxLogHost.Default?.Log(LogLevel.Trace, "No ViewModel class specified for {fragmentViewType} in LoadViewModel",
fragmentView.GetType().Name);
}
if (request == null)
request = MvxViewModelRequest.GetDefaultRequest(viewModelType);
var viewModelCache = Mvx.IoCProvider.Resolve<IMvxChildViewModelCache>();
if (viewModelCache.Exists(viewModelType))
{
var viewModelCached = viewModelCache.Get(viewModelType);
viewModelCache.Remove(viewModelType);
return viewModelCached;
}
var loaderService = Mvx.IoCProvider.Resolve<IMvxViewModelLoader>();
var viewModel = loaderService.LoadViewModel(request, savedState);
return viewModel;
}
public static void RunViewModelLifecycle(IMvxViewModel viewModel, IMvxBundle savedState,
MvxViewModelRequest request)
{
try
{
if (request != null)
{
var parameterValues = new MvxBundle(request.ParameterValues);
viewModel.CallBundleMethods("Init", parameterValues);
}
if (savedState != null)
{
viewModel.CallBundleMethods("ReloadState", savedState);
}
viewModel.Start();
}
catch (Exception exception)
{
throw exception.MvxWrap("Problem running viewModel lifecycle of type {0}", viewModel.GetType().Name);
}
}
public static string FragmentJavaName(this Type fragmentType)
{
return Java.Lang.Class.FromType(fragmentType).Name;
}
}
}