-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReloadManager.cs
156 lines (131 loc) · 6.57 KB
/
ReloadManager.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DryIoc;
using Prism.Navigation;
using Prism.Services;
using Serilog;
using Tbc.Core.Models;
using tbc.sample.prism.Extensions;
using Tbc.Target;
using Tbc.Target.Interfaces;
using Tbc.Target.Requests;
using Xamarin.Forms;
using static Tbc.Core.Models.Outcome;
namespace tbc.sample.prism
{
public class ReloadManager : ReloadManagerBase
{
private readonly IContainer _container;
private readonly ILogger _logger;
private readonly Func<INavigationService> _navigationServiceFactory;
public ReloadManager(IContainer container, ILogger logger, Func<INavigationService> navigationServiceFactory)
{
_container = container;
_logger = logger;
_navigationServiceFactory = navigationServiceFactory;
}
public override async Task<Outcome> ProcessNewAssembly(ProcessNewAssemblyRequest req)
{
var types = req.Assembly.GetTypes();
_logger.Information("Types: {Types}", types);
// replace reloader
var reloaderType = types.Where(x => x.ImplementsServiceType<IReloadManager>() && x != GetType()).LastOrDefault();
if (reloaderType != null)
{
_container.Register(typeof(IReloadManager), reloaderType, ifAlreadyRegistered: IfAlreadyRegistered.Replace);
var newReloader = _container.Resolve<IReloadManager>();
NotifyReplacement(newReloader);
return await newReloader.ProcessNewAssembly(req);
}
// replace services
foreach (var serviceType in types.Where(t => t.IsSubclassOf(typeof(ServiceBase)) && !t.IsAbstract))
{
_logger.Information("Registering service type {PageType} from assembly {Assembly}", serviceType, req.Assembly);
foreach (var @if in serviceType.GetImplementedInterfaces())
{
_logger.Information("Registering service type {ImplementationType} against {ServiceType} from assembly {Assembly}",
serviceType, @if, req.Assembly);
_container.Register(
@if,
serviceType,
Reuse.Singleton,
ifAlreadyRegistered: IfAlreadyRegistered.Replace,
setup: Setup.With(asResolutionCall: true)
);
}
}
// replace viewmodels
foreach (var vm in types.Where(t => t.IsSubclassOf(typeof(ViewModelBase)) && !t.IsAbstract))
{
_logger.Information("Registering view model type {PageType} from assembly {Assembly}", vm, req.Assembly);
_container.Register(vm, ifAlreadyRegistered: IfAlreadyRegistered.Replace, setup: Setup.With(asResolutionCall:true));
}
// replace pages
foreach (var page in types.Where(p => p.IsSubclassOf(typeof(ContentPage))))
{
_logger.Information("Registering page type {PageType} from assembly {Assembly}", page, req.Assembly);
_container.Register(typeof(object), page, serviceKey: page.Name, made: Made.Of(FactoryMethod.ConstructorWithResolvableArguments), ifAlreadyRegistered: IfAlreadyRegistered.Replace);
}
// go to the primary type
var primaryType = req.PrimaryType ?? types.FirstOrDefault(x => x.IsSubclassOf(typeof(ContentPage)));
if (primaryType != null && primaryType.IsSubclassOf(typeof(ContentPage)))
{
_logger.Information("Navigate to {PrimaryType}", primaryType);
await Device.InvokeOnMainThreadAsync(
() => _navigationServiceFactory().NavigateAsync(primaryType.Name, new NavigationParameters(), null, animated: false));
}
return new Outcome { Success = true };
}
public override async Task<Outcome> ExecuteCommand(CommandRequest commandRequest)
{
var (command, args) = (commandRequest.Command, commandRequest.Args);
_logger.Information("Execute Command \"{Command}\" with args {@Args}", command, args);
switch (command)
{
case "goto":
await Device.InvokeOnMainThreadAsync(
async () => await GotoPage(args.Any() ? args[0] : null, message:"where to my friend?"));
return new Outcome { Success = true };
default:
_logger.Warning("No logic to handle command \"{Command}\" with args {@Args}", command, args);
return new Outcome
{
Success = true,
Messages = new List<OutcomeMessage>()
{
new OutcomeMessage
{
Message =
$"No logic to handle command \"{command}\" with args {String.Join(", ", args)}"
}
}
};
}
}
private async Task GotoPage(string pageName = null, string message = "choose")
{
var pageDialogService = _container.Resolve<IPageDialogService>();
var navigationService = _container.Resolve<INavigationService>();
if (String.IsNullOrWhiteSpace(pageName))
{
var pages =
_container.GetServiceRegistrations()
.Where(x => x.ImplementationType.IsSubclassOf(typeof(ContentPage)))
.GroupBy(x => x.ImplementationType.FullName)
.Select(x => x.OrderByDescending(y => y.FactoryRegistrationOrder).First())
.ToList();
var choice = await pageDialogService.PresentChoice(message, pages, x => x.ImplementationType.Name);
if (choice.ServiceType != null)
pageName = choice.ImplementationType?.Name;
}
if (String.IsNullOrWhiteSpace(pageName))
{
_logger.Warning("No page chosen for navigation");
return;
}
await navigationService.NavigateAsync(pageName);
}
}
}