public RoomsViewModel(Page page) { this.page = page; ablyMessenger = DependencyService.Get <IAblyMessenger>(); ConnectCommand = new Command(async() => { var success = false; string message = string.Empty; try { IsBusy = true; success = await ablyMessenger.InitializeAsync(); } catch (Exception ex) { message = ex.Message; } finally { IsBusy = false; } if (success) { await page.DisplayAlert("Success", "Now joining #general.", "OK"); await page.Navigation.PushAsync(new MainChatPage()); } else { await page.DisplayAlert("Sad Monkeys", $"Unable to join #general: {message}", "OK"); } }); }
public MainChatViewModel() { // Initialize with default values ablyMessenger = DependencyService.Get <IAblyMessenger>(); Messages = new ObservableRangeCollection <Message>(); SendCommand = new Command(() => { var message = new Message { Text = OutGoingText, IsIncoming = false, MessageDateTime = DateTime.Now }; Messages.Add(message); ablyMessenger?.SendMessage(message.Text); OutGoingText = string.Empty; }); LocationCommand = new Command(async() => { try { var local = await CrossGeolocator.Current.GetPositionAsync(new TimeSpan(0, 0, 10)); var map = $"https://maps.googleapis.com/maps/api/staticmap?center={local.Latitude.ToString(CultureInfo.InvariantCulture)},{local.Longitude.ToString(CultureInfo.InvariantCulture)}&zoom=17&size=400x400&maptype=street&markers=color:red%7Clabel:%7C{local.Latitude.ToString(CultureInfo.InvariantCulture)},{local.Longitude.ToString(CultureInfo.InvariantCulture)}&key="; var message = new Message { Text = "I am here", AttachementUrl = map, IsIncoming = false, MessageDateTime = DateTime.Now }; Messages.Add(message); ablyMessenger?.SendMessage("attach:" + message.AttachementUrl); } catch (Exception ex) { } }); if (ablyMessenger == null) { return; } ablyMessenger.MessageAdded = (message) => { Messages.Add(message); }; }