public async Task InitializeAsync() { const string SettingsPushTokenKey = "PushToken"; string pushToken = (string)ApplicationData.Current.LocalSettings.Values[SettingsPushTokenKey]; _realtimeSession = new WMSession(WebimAccount, WebimLocation, null); await _realtimeSession.StartSessionAsync(); _offlineSession = new WMOfflineSession(WebimAccount, WebimLocation, pushToken, null); await _offlineSession.Initialize(); Initialized = true; if (RealtimeSessionInitialized != null) { RealtimeSessionInitialized(); } if (OfflineSessionInitialized != null) { OfflineSessionInitialized(); } try { _pushNotificationChannel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync(); if (_pushNotificationChannel != null && !String.IsNullOrEmpty(_pushNotificationChannel.Uri)) { if (string.IsNullOrEmpty(pushToken) || !pushToken.Equals(_pushNotificationChannel.Uri)) { await _realtimeSession.SetDeviceTokenAsync(_pushNotificationChannel.Uri); ApplicationData.Current.LocalSettings.Values[SettingsPushTokenKey] = _pushNotificationChannel.Uri; } } } catch (Exception) {} }
void MaybeReloadMessagesForSession(WMSession session) { if (session.Chat != null && session.Chat.Messages != null) { ObservableCollection<WMMessage> collection = new ObservableCollection<WMMessage>(session.Chat.Messages); _realtimeMessagesModel.Messages = collection; } else { _realtimeMessagesModel.Messages.Clear(); } }
void RealtimeScrollToLastestMessage(WMSession session) { if (session.Chat != null && session.Chat.Messages != null) { var selectedItemIndex = session.Chat.Messages.Count - 1; if (selectedItemIndex < 0) { return; } RealtimeChatListView.SelectedIndex = selectedItemIndex; RealtimeChatListView.UpdateLayout(); RealtimeChatListView.ScrollIntoView(RealtimeChatListView.SelectedItem); } }
void session_SessionDidReceiveError(WMSession session, WMBaseSession.WMSessionError errorID) { UpdateCommandBarButtonsStates(); }
void session_SessionDidChangeSessionStatus(WMSession session) { MaybeReloadMessagesForSession(session); UpdateCommandBarButtonsStates(); }
void session_SessionDidReceiveFullUpdate(WMSession session) { MaybeReloadMessagesForSession(session); UpdateCommandBarButtonsStates(); }
void session_SessionDidReceiveMessage(WMSession session, WMMessage message) { MaybeReloadMessagesForSession(session); RealtimeScrollToLastestMessage(session); }
void session_SessionDidStartChat(WMSession session, WMChat chat) { MaybeReloadMessagesForSession(session); UpdateCommandBarButtonsStates(); }
void session_SessionDidUpdateOperator(WMSession session, WMOperator chatOperator) { }
private bool IsChatClosed(WMSession session) { return session.Chat == null || session.Chat.State == WMChat.WMChatState.WMChatStateUnknown || session.Chat.State == WMChat.WMChatState.WMChatStateClosed || session.Chat.State == WMChat.WMChatState.WMChatStateClosedByVisitor; }