/// <summary>
        /// A helper method to initilize the viewmodel commands
        /// </summary>
        private void InitCommands()
        {
            // Init the refresh command
            RefreshCommand = new DelegateCommand(async() => await DialogHost.Show(new Resources.ProgressDialog(), "RootDialog", async(s, e) =>
            {
                // fetch profiles from server
                var profiles = (await _hotspotClient.LoadAllProfilesAsync()).ToList().Select(u => new HotspotUserProfileViewModel(u)
                {
                    SelectionChangedCommand = SharedItemSelectionChangedCommand, DeleteProfileCommand = SharedDeleteProfileCommand
                });

                // Update user list
                Profiles.Clear();
                Profiles.AddRange(profiles);

                e.Session.Close();

                RaisePropertyChanged(nameof(ProfilesCount));
                RaisePropertyChanged(nameof(SelectedCount));
                RaisePropertyChanged(nameof(HasSelectedProfiles));

                GC.Collect();
            }, null));

            // Init the deleting command
            RemoveSelectedProfilesCommand = new DelegateCommand(async() =>
            {
                var selected = Profiles.Where(p => p.IsSelected && p.Name != "default").ToList();
                if (selected.Count <= 0)
                {
                    return;
                }

                var confirm = _dialogService.ShowMessageAsync("Confirm", $"Are sure that you want to delete the selected {selected.Count} selected profiles?", MessageDialogStyle.AffirmativeAndNegative);
                if (await confirm == MessageDialogResult.Negative)
                {
                    return;
                }

                var dlg = await _dialogService.ShowProgressAsync("Deleting...", $"Deleting profile 1 of {selected.Count}...");
                dlg.SetCancelable(true);

                int done = 0;
                for (int i = 0; i < selected.Count; i++)
                {
                    if (dlg.IsCanceled)
                    {
                        break;
                    }

                    dlg.SetMessage($"Deleting profile {i + 1} of {selected.Count}...");
                    dlg.SetProgress((double)i / selected.Count);

                    if (await _hotspotClient.RemoveProfileAsync(selected[i].UserProfileModel))
                    {
                        Profiles.Remove(selected[i]);
                        ++done;
                    }
                }

                var msg = $"Deleted {done} profiles successfully!";
                _eventAggregator.GetEvent <NotificationEvent>().Publish(new NotificationEventArgs(msg, null, new SideNotificationViewModel("Hotspot User Profiles", msg)));

                await dlg.CloseAsync();
                RefreshSelectionState();
                GC.Collect();
            });

            //Init the global delete command
            SharedDeleteProfileCommand = new DelegateCommand <HotspotUserProfileViewModel>(async vm =>
            {
                if (vm?.Name == "default")
                {
                    _eventAggregator.GetEvent <NotificationEvent>().Publish(new NotificationEventArgs("Can't delete the default hotspot user profile!"));
                    return;
                }

                var confirm = _dialogService.ShowMessageAsync("Confirm", $"Are you sure that you want to delete profile `{vm.Name}` ?", MessageDialogStyle.AffirmativeAndNegative);
                if (await confirm == MessageDialogResult.Negative)
                {
                    return;
                }

                var done = _hotspotClient.RemoveProfileAsync(vm.UserProfileModel);
                var msg  = await done ? $"Profile `{vm.Name}` Deleted Successfully!" : $"Can't Delete the profile";

                _eventAggregator.GetEvent <NotificationEvent>().Publish(new NotificationEventArgs(msg, null, new SideNotificationViewModel("Hotspot User Profiles", msg)));
                if (await done)
                {
                    Profiles.Remove(vm);
                }

                RefreshSelectionState();
                GC.Collect();
            });

            // Init the global selection changed command
            SharedItemSelectionChangedCommand = new DelegateCommand(RefreshSelectionState);

            // Init the selection commands
            SelectAllCommand = new DelegateCommand(() =>
            {
                Profiles.ForEach(p => { if (p.Name == "default")
                                        {
                                            return;
                                        }
                                        p.IsSelected = true; });
                RefreshSelectionState();
            });

            // Init the invert selection command
            InverseSelectionCommand = new DelegateCommand(() => { Profiles.ForEach(p => p.IsSelected ^= true); RefreshSelectionState(); });

            // Init the command which will show the adding profile dialog
            AddProfileCommand = new DelegateCommand(() => DialogHost.Show(new AddHotspotUserProfileView {
                DataContext = new AddHotspotUserProfileViewModel(_eventAggregator, null)
            }));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="eventAggregator"></param>
        /// <param name="hotspotClient"></param>
        public AddMultipleHotspotUsersViewModel(IEventAggregator eventAggregator, IHotspotClient hotspotClient)
        {
            UsernameAndPasswordViewModel = new AddMultipleUsersUsernameAndPasswordViewModel();
            ProfileAndLimitsViewModel    = new AddMultipleHotspotUsersProfileAndLimitsViewModel();

            ((DelegateCommand)UsernameAndPasswordViewModel.NextCommand).RaiseCanExecuteChanged();

            ProfileAndLimitsViewModel.FinishCommand = new DelegateCommand(() =>
            {
                eventAggregator.GetEvent <AddMultipleHotspotUsersEvent>().Publish(this);
                DialogHost.CloseDialogCommand.Execute(null, null);
            });

            ProfileAndLimitsViewModel.RefreshProfilesCommand = new DelegateCommand(async() => ProfileAndLimitsViewModel.Profiles = (await hotspotClient.LoadAllProfilesAsync()).Select(p => p.Name));
            ProfileAndLimitsViewModel.RefreshProfilesCommand.Execute(null);
        }
Ejemplo n.º 3
0
        public AddHotspotUserViewModel(IEventAggregator eventAggregator, IHotspotClient hotspotClient)
        {
            IsUpdate = false;

            #region Init Commands

            // A Command to refresh the list of the user profiles and select the first one if the list count is greater than 0
            BasicViewModel.RefreshProfilesCommand = new DelegateCommand(async() =>
            {
                // Load profiles
                BasicViewModel.Profiles = (await hotspotClient.LoadAllProfilesAsync()).Select(p => p.Name).ToList();

                // Select the first one
                if (BasicViewModel.Profiles.Count > 0)
                {
                    BasicViewModel.SelectedProfileIndex = _userToEdit != null ? BasicViewModel.Profiles.IndexOf(_userToEdit.Profile) : 0;
                }
            });

            // A Command to add the built user
            LimitsViewModel.FinishCommand = new DelegateCommand(() =>
            {
                // Init a new user
                var user = _userToEdit?.UserModel ?? new HotspotUser();

                user.Name     = BasicViewModel.Username;
                user.Password = BasicViewModel.Password;

                // Calculate the profile value
                user.Profile = BasicViewModel.SelectedProfileIndex >= 0 ? (BasicViewModel.Profiles.Count - 1 >= BasicViewModel.SelectedProfileIndex ? BasicViewModel.Profiles[BasicViewModel.SelectedProfileIndex] : "default") : "default";


                // TODO:
                // Do a record add for the user notes

                // Set bandwidth limit
                if (LimitsViewModel.HasBandwidthLimits)
                {
                    // Array of the common sizes
                    var mul = new long[] { 1024, 1024 * 1024, 1024 * 1024 * 1024 };

                    // Set the bandwidth limits
                    user.LimitBytesOut = (!string.IsNullOrEmpty(LimitsViewModel.DownloadAmount) ? int.Parse(LimitsViewModel.DownloadAmount) : 0) * mul[LimitsViewModel.DownloadType];
                    user.LimitBytesIn  = (!string.IsNullOrEmpty(LimitsViewModel.UploadAmount) ? int.Parse(LimitsViewModel.UploadAmount) : 0) * mul[LimitsViewModel.UploadType];
                }

                // Set timelimit if the user has set one
                if (LimitsViewModel.HasTimeLimits)
                {
                    // Calculate the timelimt using timespan and replace the time span dot with mikrotik "d " symbole
                    var timeLimit = new TimeSpan(!string.IsNullOrEmpty(LimitsViewModel.TimeLimitDays) ? int.Parse(LimitsViewModel.TimeLimitDays) : 0,
                                                 !string.IsNullOrEmpty(LimitsViewModel.TimeLimitDays) ? int.Parse(LimitsViewModel.TimeLimitDays) : 0,
                                                 !string.IsNullOrEmpty(LimitsViewModel.TimeLimitDays) ? int.Parse(LimitsViewModel.TimeLimitDays) : 0
                                                 );

                    user.LimitUptime = timeLimit.ToString().Replace(".", "d ");
                }

                // Raise the add event
                User = user;
                eventAggregator.GetEvent <AddHotspotUserEvent>().Publish(this);

                // Close the dialog
                DialogHost.CloseDialogCommand.Execute(null, null);
            });

            #endregion

            BasicViewModel.RefreshProfilesCommand.Execute(null);
        }