예제 #1
0
파일: Voting.cs 프로젝트: fredyfx/Aiva
        private void OnUserVoted(object sender, VotedUser e)
        {
            switch (e.Option)
            {
            case Aiva.Models.Enums.VotingOption.Option1:
                ChartValues.Option1.Value++;
                break;

            case Aiva.Models.Enums.VotingOption.Option2:
                ChartValues.Option2.Value++;
                break;

            case Aiva.Models.Enums.VotingOption.Option3:
                ChartValues.Option3.Value++;
                break;

            case Aiva.Models.Enums.VotingOption.Option4:
                ChartValues.Option4.Value++;
                break;

            case Aiva.Models.Enums.VotingOption.Option5:
                ChartValues.Option5.Value++;
                break;

            case Aiva.Models.Enums.VotingOption.Option6:
                ChartValues.Option6.Value++;
                break;
            }
            Application.Current.Dispatcher.Invoke(() => VotedUsers.Add(e));
        }
예제 #2
0
        public PollViewModel(GroupChatRoom groupChatRoom)
        {
            this.groupChatRoom = groupChatRoom;
            if (groupChatRoom.Admin.Equals(UserSetting.UserEmail))
            {
                IsAdmin = true;
                OnPropertyChanged("IsAdmin");
            }

            OnCreatePollCommand = new Command(async() => {
                Option = new ObservableCollection <SelectableData <string> >();
                if (_Poll.PollId != null)
                {
                    if (!_Poll.IsClose)
                    {
                        await App.Current.MainPage.DisplayAlert("Something wrong....", "Please close this poll first. ", "Ok");
                        return;
                    }
                }

                await App.Current.MainPage.Navigation.PushAsync(new CreatePollView(this.groupChatRoom));
            });

            OnAddOptionCommand = new Command(() =>
            {
                if (!string.IsNullOrWhiteSpace(InputOption))
                {
                    TempOption.Add(InputOption);
                }
                InputOption = string.Empty;
                OnPropertyChanged("TempOption");
                OnPropertyChanged("InputOption");
            });

            OnPostCommand = new Command(async() =>
            {
                if (string.IsNullOrWhiteSpace(StrTitle))
                {
                    await App.Current.MainPage.DisplayAlert("Title is required.", "Please entry title.", "Ok");
                    return;
                }
                else if (TempOption.Count < 2)
                {
                    await App.Current.MainPage.DisplayAlert("Option is required.", "At least 2 option is required.", "Ok");
                    return;
                }

                Poll poll  = new Poll("", groupChatRoom.RoomID, StrTitle, false, TempOption, null);
                var status = await DependencyService.Get <IFirebaseDatabase>().AddPoll(poll);
                if (status)
                {
                    await App.Current.MainPage.DisplayAlert("Done", "Poll created", "Ok");
                    await App.Current.MainPage.Navigation.PopAsync();
                    await App.Current.MainPage.Navigation.PopAsync();
                }
                else
                {
                    await App.Current.MainPage.DisplayAlert("Something wrong....", "Poll Unable to create, Please try again", "Ok");
                }
            });

            OnCheckResult = new Command(async() => {
                bool status = false;

                foreach (string VotedUser in _Poll.Result.Keys)
                {
                    if (UserSetting.UserEmail.Equals(VotedUser.Replace(":", ".")))
                    {
                        status = true;
                    }
                }

                if (!status)
                {
                    await App.Current.MainPage.DisplayAlert("Something need to do first.", "Please vote first", "Ok");
                }
                else
                {
                    string FinalResult  = "";
                    int Count           = 0;
                    string OptionResult = "";
                    foreach (string option in _Poll._Option)
                    {
                        OptionResult = option;
                        foreach (string key in _Poll.Result.Keys)
                        {
                            if (_Poll.Result[key].Equals(option))
                            {
                                Count++;
                            }
                        }
                        FinalResult += OptionResult + ": " + Count + "\n";
                        Count        = 0;
                    }

                    await App.Current.MainPage.DisplayAlert("Result", FinalResult, "Ok");
                }
            });

            OnCloseCommand = new Command(async() =>
            {
                if (_Poll.IsClose)
                {
                    await App.Current.MainPage.DisplayAlert("Something wrong....", "This Poll already closed", "Ok");
                    return;
                }

                var status = await DependencyService.Get <IFirebaseDatabase>().UpdatePollCloseStatus(_Poll.PollId);
                if (status)
                {
                    await App.Current.MainPage.DisplayAlert("Done", "The poll had been closed.", "Ok");
                    _Poll.IsClose = true;
                    IsClosed      = true;
                    IsEnablePoll  = false;
                    OnPropertyChanged("IsEnablePoll");
                    OnPropertyChanged("IsClosed");
                }
                else
                {
                    await App.Current.MainPage.DisplayAlert("Something wrong....", "Poll unable to close, Please try again.", "Ok");
                }
            });
        }