-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMainViewModel.cs
More file actions
155 lines (147 loc) · 5.05 KB
/
MainViewModel.cs
File metadata and controls
155 lines (147 loc) · 5.05 KB
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
using Lemon.ModuleNavigation.Abstractions;
using Lemon.ModuleNavigation.Core;
using Lemon.ModuleNavigation.Extensions;
using Microsoft.Extensions.Logging;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reactive;
namespace Lemon.ModuleNavigation.Sample.ViewModels;
public class MainViewModel : ReactiveObject, IServiceAware
{
private readonly NavigationService _navigationService;
private readonly IServiceProvider _serviceProvider;
private readonly IDialogService _dialogService;
private readonly IRegionManager _regionManager;
private readonly ILogger _logger;
public MainViewModel(IEnumerable<IModule> modules,
IServiceProvider serviceProvider,
NavigationService navigationService,
IDialogService dialogService,
IRegionManager regionManager,
ILogger<MainViewModel> logger)
{
_logger = logger;
_navigationService = navigationService;
_serviceProvider = serviceProvider;
_dialogService = dialogService;
_regionManager = regionManager;
// default views for different regions
_navigationService.RequestViewNavigation("ContentRegion", "ViewAlpha");
_navigationService.RequestViewNavigation("TransitioningContentRegion", "ViewAlpha");
Modules = [.. modules];
ToViewCommand = ReactiveCommand.Create<string>(content =>
{
var viewName = content;
var requestNew = false;
if (content.EndsWith(".RequestNew"))
{
viewName = content.Replace(".RequestNew",string.Empty);
requestNew = true;
}
_navigationService.RequestViewNavigation("ContentRegion",
viewName,
new NavigationParameters { { "requestNew", requestNew } });
_navigationService.RequestViewNavigation("TabRegion",
viewName,
new NavigationParameters { { "requestNew", requestNew } });
_navigationService.RequestViewNavigation("ItemsRegion",
viewName,
new NavigationParameters { { "requestNew", requestNew } });
_navigationService.RequestViewNavigation("TransitioningContentRegion",
viewName,
new NavigationParameters { { "requestNew", requestNew } });
});
ShowCommand = ReactiveCommand.Create<string>(content =>
{
var param = new DialogParameters
{
{ "parent", nameof(MainViewModel) }
};
_dialogService.Show(content, null, param, result =>
{
_logger.LogDebug($"Call back:{result}");
});
_logger.LogDebug($"Show over!");
});
ShowDialogCommand = ReactiveCommand.CreateFromTask<string>(async content =>
{
var param = new DialogParameters
{
{ "parent", nameof(MainViewModel) }
};
await _dialogService.ShowDialog(content,
nameof(CustomDialogWindow),
param,
result =>
{
_logger.LogDebug($"Call back:{result}");
});
_logger.LogDebug($"ShowDialog over!");
});
ShowDialogSyncCommand = ReactiveCommand.Create<string>(content =>
{
var param = new DialogParameters
{
{ "parent", nameof(MainViewModel) }
};
var result = _dialogService.WaitShowDialog(content,
nameof(CustomDialogWindow),
param);
_logger.LogDebug($"ShowDialog over:{result}");
});
UnloadViewCommand = ReactiveCommand.Create<NavigationContext>((context) =>
{
_regionManager.RequestViewUnload(context);
});
_regionManager.NavigationSubscribe<NavigationContext>(n =>
{
_logger.LogDebug($"Request to : {n.RegionName}.{n.ViewName}");
});
_regionManager.NavigationSubscribe<IRegion>(r =>
{
_logger.LogDebug($"New region : {r.Name}");
});
}
public ReactiveCommand<NavigationContext, Unit> UnloadViewCommand
{
get;
}
public ReactiveCommand<string, Unit> ToViewCommand
{
get;
set;
}
public ReactiveCommand<string, Unit> ShowCommand
{
get;
set;
}
public ReactiveCommand<string, Unit> ShowDialogCommand
{
get;
set;
}
public ReactiveCommand<string, Unit> ShowDialogSyncCommand
{
get;
set;
}
public ObservableCollection<IModule> Modules
{
get;
set;
}
private IModule? _selectedModule;
public IModule? SelectedModule
{
get => _selectedModule;
set
{
this.RaiseAndSetIfChanged(ref _selectedModule, value);
_navigationService.RequestModuleNavigate(_selectedModule!, null);
}
}
public IServiceProvider ServiceProvider => _serviceProvider;
}