-
Notifications
You must be signed in to change notification settings - Fork 7
/
Shell.xaml.cs
182 lines (160 loc) · 6.75 KB
/
Shell.xaml.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using Chat.Common;
using Chat.ContentDialogs;
using Chat.Controls;
using Chat.Pages;
using Chat.ViewModels;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Globalization;
using System.Linq;
using System.Windows.Input;
using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.Contacts;
using Windows.Devices.Sms;
using Windows.UI.Xaml.Controls;
using MUXC = Microsoft.UI.Xaml.Controls;
namespace Chat
{
public sealed partial class Shell : UserControl
{
public ShellViewModel ViewModel { get; } = new ShellViewModel();
public Shell()
{
InitializeComponent();
}
public void HandleArguments(object e)
{
if (e is ProtocolActivatedEventArgs args)
{
HandleArguments(args);
}
if (e is ToastNotificationActivatedEventArgs args2)
{
HandleArguments(args2);
}
}
private async void HandleArguments(ProtocolActivatedEventArgs args)
{
Uri uri = args.Uri;
string unescpateduri = Uri.UnescapeDataString(uri.Query);
string contactid = unescpateduri.Replace("?ContactRemoteIds=", "", StringComparison.InvariantCulture);
if (uri.Scheme == "ms-ipmessaging")
{
ContactStore store = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AppContactsReadWrite);
Contact contact = await store.GetContactAsync(contactid);
_ = MainFrame.Navigate(typeof(ComposePage), contact);
}
}
private async void HandleArguments(ToastNotificationActivatedEventArgs args)
{
string arguments = args.Argument;
string action = args.Argument.Split('&').First(x => x.ToLower(new CultureInfo("en-US")).StartsWith("action=", StringComparison.InvariantCulture)).Split('=')[1];
string from = args.Argument.Split('&').First(x => x.ToLower(new CultureInfo("en-US")).StartsWith("from=", StringComparison.InvariantCulture)).Split('=')[1];
string deviceid = args.Argument.Split('&').First(x => x.ToLower(new CultureInfo("en-US")).StartsWith("deviceid=", StringComparison.InvariantCulture)).Split('=')[1];
switch (action.ToLower(new CultureInfo("en-US")))
{
case "reply":
{
try
{
string messagetosend = (string)args.UserInput["textBox"];
SmsDevice2 smsDevice = SmsDevice2.FromId(deviceid);
_ = await SmsUtils.SendTextMessageAsync(smsDevice, from, messagetosend);
}
catch
{
}
break;
}
case "openthread":
{
ChatMenuItemControl selectedConvo = null;
foreach (ChatMenuItemControl convo in ViewModel.ChatConversations)
{
Contact contact = convo.ViewModel.Contact;
foreach (ContactPhone num in contact.Phones)
{
if (ContactUtils.ArePhoneNumbersMostLikelyTheSame(from, num.Number))
{
selectedConvo = convo;
break;
}
}
if (selectedConvo != null)
{
break;
}
}
if (selectedConvo != null)
{
ViewModel.SelectedItem = selectedConvo;
}
break;
}
}
}
private ICommand _newConvoCommand;
public ICommand NewConvoCommand
{
get
{
_newConvoCommand ??= new RelayCommand(
() =>
{
_ = MainFrame.Navigate(typeof(ComposePage));
ViewModel.SelectedItem = null;
});
return _newConvoCommand;
}
}
private ICommand _openAboutCommand;
public ICommand OpenAboutCommand
{
get
{
_openAboutCommand ??= new RelayCommand(
async () =>
{
#if !DEBUG
await new AboutContentDialog().ShowAsync();
#else
Windows.ApplicationModel.Chat.ChatMessageStore store = await Windows.ApplicationModel.Chat.ChatMessageManager.RequestStoreAsync();
string transportId = await Windows.ApplicationModel.Chat.ChatMessageManager.RegisterTransportAsync();
Windows.ApplicationModel.Chat.ChatMessage msg = new()
{
Body = "Hello how are you?",
TransportId = transportId,
MessageOperatorKind = Windows.ApplicationModel.Chat.ChatMessageOperatorKind.Sms,
Status = Windows.ApplicationModel.Chat.ChatMessageStatus.Sent
};
bool alternate = new Random().Next(2) == 2;
msg.Recipients.Clear();
msg.From = "";
DateTimeOffset offset = new(DateTime.Now);
msg.LocalTimestamp = offset;
msg.NetworkTimestamp = offset;
msg.IsIncoming = alternate;
if (msg.IsIncoming)
{
msg.From = "Random dude";
}
else
{
msg.Recipients.Add("Random dude");
}
alternate = !alternate;
await store.SaveMessageAsync(msg);
#endif
});
return _openAboutCommand;
}
}
private void NavigationView_SelectionChanged(MUXC.NavigationView sender, MUXC.NavigationViewSelectionChangedEventArgs args)
{
if (args.SelectedItem != null)
{
_ = MainFrame.Navigate(typeof(ConversationPage), (args.SelectedItem as ChatMenuItemControl).ConversationId);
}
}
}
}