-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomePageViewModel.cs
More file actions
153 lines (131 loc) · 5.15 KB
/
HomePageViewModel.cs
File metadata and controls
153 lines (131 loc) · 5.15 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
using System.ComponentModel;
using System.Runtime.CompilerServices;
using AirshipDotNet;
using AirshipDotNet.MessageCenter;
using System.Windows.Input;
namespace MauiSample
{
public class HomePageViewModel : INotifyPropertyChanged
{
// Internal Fields
private string _channelId = null;
private Boolean _showEnablePushButton = false;
// Commands
public ICommand OnChannelIdClicked { get; }
public ICommand OnEnablePushButtonClicked { get; }
public ICommand OnMessageCenterButtonClicked { get; }
public ICommand OnPrefCenterButtonClicked { get; }
// Properties
public string ChannelId
{
get => _channelId;
set
{
if (_channelId != value)
{
_channelId = value;
OnPropertyChanged();
}
}
}
public Boolean ShowEnablePushButton
{
get => _showEnablePushButton;
set
{
if (_showEnablePushButton != value)
{
_showEnablePushButton = value;
OnPropertyChanged();
}
}
}
// INotifyPropertyChanged Impl
public event PropertyChangedEventHandler PropertyChanged;
public HomePageViewModel()
{
OnChannelIdClicked = new Command(PerformOnChannelIdClicked);
OnEnablePushButtonClicked = new Command(PerformOnEnablePushButtonClicked);
OnMessageCenterButtonClicked = new Command(PerformOnMessageCenterButtonClicked);
OnPrefCenterButtonClicked = new Command(PerformOnPrefCenterButtonClicked);
AirshipDotNet.Airship.Instance.OnChannelCreation += OnChannelEvent;
AirshipDotNet.Airship.Instance.OnPushNotificationStatusUpdate += OnPushNotificationStatusEvent;
Refresh();
}
~HomePageViewModel()
{
AirshipDotNet.Airship.Instance.OnChannelCreation -= OnChannelEvent;
AirshipDotNet.Airship.Instance.OnPushNotificationStatusUpdate -= OnPushNotificationStatusEvent;
}
private void OnChannelEvent(object sender, EventArgs e) => Refresh();
private void OnPushNotificationStatusEvent(object sender, EventArgs e) => Refresh();
public async void Refresh()
{
try
{
// Using new modular API - Channel.GetChannelId()
var channelId = await AirshipDotNet.Airship.Channel.GetChannelId();
ChannelId = channelId;
ShowEnablePushButton = !AirshipDotNet.Airship.Push.UserNotificationsEnabled;
}
catch (Exception ex)
{
Console.WriteLine($"Error refreshing channel: {ex.Message}");
}
}
private static async void PerformOnChannelIdClicked()
{
try
{
var channel = await AirshipDotNet.Airship.Channel.GetChannelId();
if (!string.IsNullOrEmpty(channel))
{
await Clipboard.Default.SetTextAsync(channel);
Console.WriteLine("Channel ID '{0}' copied to clipboard!", channel);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error copying channel ID: {ex.Message}");
}
}
private static async void PerformOnEnablePushButtonClicked()
{
try
{
// Enable push notifications with fallback to system settings if permission was denied
var args = new EnableUserPushNotificationsArgs(PromptPermissionFallback.SystemSettings);
var enabled = await AirshipDotNet.Airship.Push.EnableUserNotifications(args);
// Check the current notification permission status
var status = await AirshipDotNet.Airship.Push.GetPushNotificationStatus();
Console.WriteLine($"Push notifications enabled: {enabled}");
Console.WriteLine($"Permission status: {status.NotificationPermissionStatus}, Opt-in: {status.IsOptIn}");
}
catch (Exception ex)
{
Console.WriteLine($"Error enabling push notifications: {ex.Message}");
}
}
private static async void PerformOnMessageCenterButtonClicked()
{
try
{
await AirshipDotNet.Airship.MessageCenter.Display();
}
catch (Exception ex)
{
Console.WriteLine($"Error displaying message center: {ex.Message}");
}
}
private static void PerformOnPrefCenterButtonClicked()
{
OpenPreferenceCenter("app_default");
}
private void OnPropertyChanged([CallerMemberName] string name = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
private static async void OpenPreferenceCenter(string prefCenterId)
{
await AirshipDotNet.Airship.PreferenceCenter.Open(prefCenterId);
}
}
}