public FileManagerViewModel(IDialogs dialogs, IFileSystem fileSystem) { this.dialogs = dialogs; this.fileSystem = fileSystem; this.Select = ReactiveCommand.CreateFromTask <FileEntryViewModel>(async entry => { var cfg = new Dictionary <string, Action>(); if (entry.IsDirectory) { cfg.Add("Enter", () => this.CurrentPath = entry.Entry.FullName); } else { cfg.Add("View", async() => { var text = File.ReadAllText(entry.Entry.FullName); await this.dialogs.Alert(text, entry.Entry.Name); }); //cfg.Add("Copy", () => //{ // //var progress = dialogs.Progress(new ProgressDialogConfig // //{ // // Title = "Copying File" // //}); // var fn = Path.GetFileNameWithoutExtension(entry.Entry.Name); // var ext = Path.GetExtension(entry.Entry.Name); // var newFn = ext.IsEmpty() ? $"fn_1" : $"{fn}_1.{ext}"; // var target = new FileInfo(newFn); // var file = new FileInfo(entry.Entry.FullName); // file // .CopyProgress(target, true) // .Subscribe(p => // { // //progress.Title = "Copying File - Seconds Left: " + p.TimeRemaining.TotalSeconds; // //progress.PercentComplete = p.PercentComplete; // }); //}); } //cfg.Add("Delete", () => Confirm("Delete " + entry.Name, entry.Entry.Delete)); await dialogs.ActionSheet("Actions", cfg, true); }); this.showBack = this.WhenAnyValue(x => x.CurrentPath) .Select(x => x == this.CurrentPath) .ToProperty(this, x => x.ShowBack); this.WhenAnyValue(x => x.CurrentPath) .Skip(1) .Subscribe(_ => this.LoadEntries()) .DisposeWith(this.DestroyWith); }
public static Task PickEnumValue <T>(this IDialogs dialogs, string title, Action <T> action) { var keys = Enum.GetNames(typeof(T)); var actions = new List <(string Key, Action Action)>(keys.Length); foreach (var key in keys) { var value = (T)Enum.Parse(typeof(T), key); actions.Add((key, () => action(value))); } return(dialogs.ActionSheet(title, false, actions.ToArray())); }
public GpsViewModel(IGpsManager manager, IDialogs dialogs) { this.manager = manager; this.IsUpdating = this.manager.IsListening; this.WhenAnyValue(x => x.UseBackground) .Subscribe(x => this.Access = this.manager.GetCurrentStatus( new GpsRequest { UseBackground = this.UseBackground }).ToString() ); this.WhenAnyValue(x => x.IsUpdating) .Select(x => x ? "Stop Listening" : "Start Updating") .ToPropertyEx(this, x => x.ListenerText); this.GetCurrentPosition = ReactiveCommand.CreateFromTask(async _ => { var result = await dialogs.RequestAccess(() => this.manager.RequestAccess(new GpsRequest())); if (!result) { return; } var reading = await this.manager.GetLastReading(); if (reading == null) { await dialogs.Alert("Could not getting GPS coordinates"); } else { this.SetValues(reading); } }); this.BindBusyCommand(this.GetCurrentPosition); ReactiveCommand.Create(() => dialogs.ActionSheet( "Select Priority", false, ("Highest", () => this.Priority = GpsPriority.Highest), ("Normal", () => this.Priority = GpsPriority.Normal), ("Low", () => this.Priority = GpsPriority.Low) )); this.ToggleUpdates = ReactiveCommand.CreateFromTask( async() => { if (this.manager.IsListening) { await this.manager.StopListener(); this.gpsListener?.Dispose(); } else { var result = await dialogs.RequestAccess(() => this.manager.RequestAccess(new GpsRequest { UseBackground = this.UseBackground })); if (!result) { await dialogs.Alert("Insufficient permissions"); return; } var request = new GpsRequest { UseBackground = this.UseBackground, Priority = this.Priority, }; if (IsInterval(this.DesiredInterval)) { request.Interval = ToInterval(this.DesiredInterval); } if (IsInterval(this.ThrottledInterval)) { request.ThrottledInterval = ToInterval(this.ThrottledInterval); } await this.manager.StartListener(request); } this.IsUpdating = this.manager.IsListening; }, this.WhenAny( x => x.IsUpdating, x => x.DesiredInterval, x => x.ThrottledInterval, (u, i, t) => { if (u.GetValue()) { return(true); } var isdesired = IsInterval(i.GetValue()); var isthrottled = IsInterval(t.GetValue()); if (isdesired && isthrottled) { var desired = ToInterval(i.GetValue()); var throttle = ToInterval(t.GetValue()); if (throttle.TotalSeconds >= desired.TotalSeconds) { return(false); } } return(true); } ) ); this.UseRealtime = ReactiveCommand.Create(() => { var rt = GpsRequest.Realtime(false); this.ThrottledInterval = String.Empty; this.DesiredInterval = rt.Interval.TotalSeconds.ToString(); this.Priority = rt.Priority; }); this.RequestAccess = ReactiveCommand.CreateFromTask(async() => { var access = await this.manager.RequestAccess(new GpsRequest { UseBackground = this.UseBackground }); this.Access = access.ToString(); }); this.BindBusyCommand(this.RequestAccess); }
public GpsViewModel(IGpsManager manager, IDialogs dialogs) { this.manager = manager; var l = this.manager.CurrentListener; this.IsUpdating = l != null; this.UseBackground = l?.UseBackground ?? true; this.Priority = l?.Priority ?? GpsPriority.Normal; this.DesiredInterval = l?.Interval.TotalSeconds.ToString() ?? String.Empty; this.ThrottledInterval = l?.ThrottledInterval?.TotalSeconds.ToString() ?? String.Empty; this.MinimumDistanceMeters = l?.MinimumDistance?.TotalMeters.ToString() ?? String.Empty; this.NotificationTitle = manager.Title; this.NotificationMessage = manager.Message; this.WhenAnyValue(x => x.IsUpdating) .Select(x => x ? "Stop Listening" : "Start Updating") .ToPropertyEx(this, x => x.ListenerText); this.WhenAnyValue(x => x.NotificationTitle) .Skip(1) .Subscribe(x => this.manager.Title = x) .DisposedBy(this.DestroyWith); this.WhenAnyValue(x => x.NotificationMessage) .Skip(1) .Subscribe(x => this.manager.Message = x) .DisposedBy(this.DestroyWith); this.GetCurrentPosition = this.CreateOneReading(dialogs, LocationRetrieve.Current); this.GetLastReading = this.CreateOneReading(dialogs, LocationRetrieve.Last); this.GetLastOrCurrent = this.CreateOneReading(dialogs, LocationRetrieve.LastOrCurrent); ReactiveCommand.Create(() => dialogs.ActionSheet( "Select Priority", false, ("Highest", () => this.Priority = GpsPriority.Highest), ("Normal", () => this.Priority = GpsPriority.Normal), ("Low", () => this.Priority = GpsPriority.Low) )); this.ToggleUpdates = ReactiveCommand.CreateFromTask( async() => { if (this.manager.CurrentListener != null) { await this.manager.StopListener(); this.gpsListener?.Dispose(); } else { var result = await dialogs.RequestAccess(async() => { var access = await this.manager.RequestAccess(new GpsRequest { UseBackground = this.UseBackground }); this.Access = access.ToString(); return(access); }); if (!result) { await dialogs.Alert("Insufficient permissions"); return; } var request = new GpsRequest { UseBackground = this.UseBackground, Priority = this.Priority, }; if (IsNumeric(this.DesiredInterval)) { request.Interval = ToInterval(this.DesiredInterval); } if (IsNumeric(this.ThrottledInterval)) { request.ThrottledInterval = ToInterval(this.ThrottledInterval); } if (IsNumeric(this.MinimumDistanceMeters)) { request.MinimumDistance = Distance.FromMeters(Int32.Parse(this.MinimumDistanceMeters)); } await this.manager.StartListener(request); } this.IsUpdating = this.manager.CurrentListener != null; }, this.WhenAny( x => x.IsUpdating, x => x.DesiredInterval, x => x.ThrottledInterval, x => x.MinimumDistanceMeters, (u, i, t, d) => { if (u.GetValue()) { return(true); } var isdesired = IsNumeric(i.GetValue()); var isthrottled = IsNumeric(t.GetValue()); var ismindist = IsNumeric(d.GetValue()); if (isdesired && isthrottled) { var desired = ToInterval(i.GetValue()); var throttle = ToInterval(t.GetValue()); if (throttle.TotalSeconds >= desired.TotalSeconds) { return(false); } } return(true); } ) ); this.UseRealtime = ReactiveCommand.Create(() => { var rt = GpsRequest.Realtime(false); this.ThrottledInterval = String.Empty; this.DesiredInterval = rt.Interval.TotalSeconds.ToString(); this.Priority = rt.Priority; }); this.RequestAccess = ReactiveCommand.CreateFromTask(async() => { var access = await this.manager.RequestAccess(new GpsRequest { UseBackground = this.UseBackground }); this.Access = access.ToString(); }); this.BindBusyCommand(this.RequestAccess); }
public CreateViewModel(INavigationService navigationService, IHttpTransferManager httpTransfers, IDialogs dialogs, IPlatform platform, AppSettings appSettings) { this.platform = platform; this.Url = appSettings.LastTransferUrl; this.WhenAnyValue(x => x.IsUpload) .Subscribe(upload => { if (!upload && this.FileName.IsEmpty()) { this.FileName = Guid.NewGuid().ToString(); } this.Title = upload ? "New Upload" : "New Download"; }); this.ManageUploads = navigationService.NavigateCommand("ManageUploads"); this.ResetUrl = ReactiveCommand.Create(() => { appSettings.LastTransferUrl = null; this.Url = appSettings.LastTransferUrl; }); this.SelectUpload = ReactiveCommand.CreateFromTask(async() => { var files = platform.AppData.GetFiles("upload.*", SearchOption.TopDirectoryOnly); if (!files.Any()) { await dialogs.Alert("There are not files to upload. Use 'Manage Uploads' below to create them"); } else { var cfg = new Dictionary <string, Action>(); foreach (var file in files) { cfg.Add(file.Name, () => this.FileName = file.Name); } await dialogs.ActionSheet("Actions", cfg); } }); this.Save = ReactiveCommand.CreateFromTask( async() => { var path = Path.Combine(this.platform.AppData.FullName, this.FileName); var request = new HttpTransferRequest(this.Url, path, this.IsUpload) { UseMeteredConnection = this.UseMeteredConnection }; await httpTransfers.Enqueue(request); appSettings.LastTransferUrl = this.Url; await navigationService.GoBackAsync(); }, this.WhenAny ( x => x.IsUpload, x => x.Url, x => x.FileName, (up, url, fn) => { this.ErrorMessage = String.Empty; if (!Uri.TryCreate(url.GetValue(), UriKind.Absolute, out _)) { this.ErrorMessage = "Invalid URL"; } else if (up.GetValue() && fn.GetValue().IsEmpty()) { this.ErrorMessage = "You must select or enter a filename"; } return(this.ErrorMessage.IsEmpty()); } ) ); }