Skip to content

Commit

Permalink
Merge pull request #137 from RightToAskOrg/TwoButtonPopup-refactoring
Browse files Browse the repository at this point in the history
Popups refactoring
  • Loading branch information
vteague authored Dec 5, 2022
2 parents 86d8536 + f8d430a commit e0ed712
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ public static async Task DoRegistrationCheck(BaseViewModel vm)
{
if (!IndividualParticipant.IsRegistered)
{
var popup = new TwoButtonPopup(vm, AppResources.MakeAccountQuestionText, AppResources.CreateAccountPopUpText, AppResources.CancelButtonText, AppResources.OKText);
_ = await App.Current.MainPage.Navigation.ShowPopupAsync(popup);
if (vm.ApproveButtonClicked)
var popup = new TwoButtonPopup(AppResources.MakeAccountQuestionText, AppResources.CreateAccountPopUpText, AppResources.CancelButtonText, AppResources.OKText, false);
var popupResult = await App.Current.MainPage.Navigation.ShowPopupAsync(popup);
if (popup.HasApproved(popupResult))
{
await Shell.Current.GoToAsync($"{nameof(RegisterPage1)}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,6 @@ public bool HasAnswer
get => _hasAnswer;
set => SetProperty(ref _hasAnswer, value);
}
// booleans stored for new style popups
private bool _approveClicked;
public bool ApproveClicked
{
get => _approveClicked;
set => SetProperty(ref _approveClicked, value);
}
private bool _cancelClicked;
public bool CancelClicked
{
get => _cancelClicked;
set => SetProperty(ref _cancelClicked, value);
}

// constructor needed for command creation
public Question()
Expand All @@ -254,9 +241,9 @@ public Question()
else
{
var message = AppResources.CreateAccountPopUpText;
var popup = new TwoButtonPopup(this, AppResources.MakeAccountQuestionText, message, AppResources.NotNowAnswerText, AppResources.OKText); // this instance uses a model instead of a VM
_ = await Application.Current.MainPage.Navigation.ShowPopupAsync(popup);
if (ApproveClicked)
var popup = new TwoButtonPopup(AppResources.MakeAccountQuestionText, message, AppResources.NotNowAnswerText, AppResources.OKText, true); // this instance uses a model instead of a VM
var popupResult = await Application.Current.MainPage.Navigation.ShowPopupAsync(popup);
if (popup.HasApproved(popupResult))
{
await Shell.Current.GoToAsync($"{nameof(RegisterPage1)}");
}
Expand Down Expand Up @@ -288,23 +275,11 @@ await Share.RequestAsync(new ShareTextRequest
IndividualParticipant.ReportedQuestionIDs.Remove(QuestionId);
}
});
PopupApproveCommand = new Command(() =>
{
ApproveClicked = true;
CancelClicked = false;
});
PopupCancelCommand = new Command(() =>
{
CancelClicked = true;
ApproveClicked = false;
});
}

// commands
public Command UpvoteCommand { get; }
public Command ReportCommand { get; }
public Command PopupApproveCommand { get; }
public Command PopupCancelCommand { get; }
public Command QuestionDetailsCommand { get; }
public IAsyncCommand ShareCommand { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public BaseViewModel()
PopupLabelText = "TestText";
HomeButtonCommand = new AsyncCommand(async () =>
{
var popup = new TwoButtonPopup(this, AppResources.GoHomePopupTitle, AppResources.GoHomePopupText, AppResources.CancelButtonText, AppResources.GoHomeButtonText);
_ = await Application.Current.MainPage.Navigation.ShowPopupAsync(popup);
if (ApproveButtonClicked)
var popup = new TwoButtonPopup(AppResources.GoHomePopupTitle, AppResources.GoHomePopupText, AppResources.CancelButtonText, AppResources.GoHomeButtonText, false);
var popupResult = await Application.Current.MainPage.Navigation.ShowPopupAsync(popup);
if (popup.HasApproved(popupResult))
{
await Application.Current.MainPage.Navigation.PopToRootAsync();
}
Expand All @@ -29,24 +29,6 @@ public BaseViewModel()
var popup = new InfoPopup(PopupHeaderText,PopupLabelText, AppResources.OKText);
_ = await Application.Current.MainPage.Navigation.ShowPopupAsync(popup);
});
ApproveCommand = new Command(() =>
{
ApproveButtonClicked = true;
CancelButtonClicked = false;
//Dismiss();
});
CancelCommand = new Command(() =>
{
ApproveButtonClicked = false;
CancelButtonClicked = true;
});
}

private bool _isBusy;
public bool IsBusy
{
get => _isBusy;
set => SetProperty(ref _isBusy, value);
}

private string _title = string.Empty;
Expand Down Expand Up @@ -77,14 +59,8 @@ public string ReportLabelText
set => SetProperty(ref _reportLabelText, value);
}

// booleans for feedback from generic popup button presses
public bool CancelButtonClicked { get; set; }
public bool ApproveButtonClicked { get; set; }

// commands
public IAsyncCommand HomeButtonCommand { get; }
public IAsyncCommand InfoPopupCommand { get; }
public Command ApproveCommand { get; set; }
public Command CancelCommand { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using RightToAskClient.HttpClients;
using System;
using RightToAskClient.HttpClients;
using RightToAskClient.Models;
using RightToAskClient.Resx;
using RightToAskClient.Views;
Expand Down Expand Up @@ -289,108 +290,137 @@ public FindMPsViewModel()

// methods
#region Methods

private async Task ShowOneButtonPopup(string? title, string message, string buttonText)
{
try
{
OneButtonPopup popup;
if (title != null)
{
popup = new OneButtonPopup(title,message, buttonText);
}
else
{
popup = new OneButtonPopup(message, buttonText);
}
_ = await Application.Current.MainPage.Navigation.ShowPopupAsync(popup);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}

private async Task<bool> ShowTwoButtonPopup(string popupTitle, string popupText, string cancelMessage, string approveMessage)
{
try
{
var popup = new TwoButtonPopup(
AppResources.InvalidPostcodePopupTitle,
AppResources.InvalidPostcodePopupText,
AppResources.CancelButtonText,
AppResources.ImSureButtonText,
false);
var popupResult = await Application.Current.MainPage.Navigation.ShowPopupAsync(popup);
return popup.HasApproved(popupResult);
}
catch (Exception e)
{
Console.WriteLine(e);
return false;
}
}

// If we don't even know the person's state, we have no idea so they have to go back and pick;
// If we know their state but not their Legislative Assembly or Council makeup, we can go on.
private async Task OnSubmitAddressButton_Clicked()
{
// see if we should prompt first
CheckPostcode();

if (!PostcodeIsValid)
{
var popup = new TwoButtonPopup(this, AppResources.InvalidPostcodePopupTitle, AppResources.InvalidPostcodePopupText, AppResources.CancelButtonText, AppResources.ImSureButtonText);
_ = await Application.Current.MainPage.Navigation.ShowPopupAsync(popup);
if (ApproveButtonClicked)
var popupResult = await ShowTwoButtonPopup(
AppResources.InvalidPostcodePopupTitle,
AppResources.InvalidPostcodePopupText,
AppResources.CancelButtonText,
AppResources.ImSureButtonText);
if (popupResult)
{
PostcodeIsValid = true;
}
else
{
return;
}
}
if (PostcodeIsValid)
{
IsBusy = true; // no longer displaying the activity indicator since the webview shows that it is being updated

var state = IndividualParticipant.ProfileData.RegistrationInfo.State;
var state = IndividualParticipant.ProfileData.RegistrationInfo.State;
if (string.IsNullOrEmpty(state))
{
await ShowOneButtonPopup(null, AppResources.SelectStateWarningText, AppResources.OKText);
return;
}

if (string.IsNullOrEmpty(state))
var addressValidation = _address.SeemsValid();
if (addressValidation.Failure)
{
var errorMessage = AppResources.InvalidAddress;
if (addressValidation is ErrorResult<bool> errorResult)
{
var popup = new OneButtonPopup(AppResources.SelectStateWarningText, AppResources.OKText);
_ = await Application.Current.MainPage.Navigation.ShowPopupAsync(popup);
return;
errorMessage += errorResult.Message;
}
await ShowOneButtonPopup(null, errorMessage, AppResources.OKText);
return;
}

var addressValidation = _address.SeemsValid();
if (addressValidation.Failure)
{
var errorMessage = AppResources.InvalidAddress;
if (addressValidation is ErrorResult<bool> errorResult)
{
errorMessage += errorResult.Message;
}
var httpResponse = await GeoscapeClient.GetFirstAddressData(_address + " " + state);
if (httpResponse == null)
{
return;
}

var popup = new OneButtonPopup(errorMessage, AppResources.OKText);
_ = await App.Current.MainPage.Navigation.ShowPopupAsync(popup);
// Display a popup if Electorates were not found
if (httpResponse.Failure)
{
ReportLabelText = ((httpResponse is ErrorResult<GeoscapeAddressFeature> errorResult)
? ReportLabelText = errorResult.Message
: ReportLabelText = AppResources.ErrorFindingAddress);
await ShowOneButtonPopup(null, AppResources.ElectoratesNotFoundErrorText, AppResources.OKText);
return;
}

return;
}
var bestAddress = httpResponse.Data;
var electoratePopupTitle = "Electorates not Found";
var electoratePopupText = "Please reformat the address and try again.";
// needs a federal electorate to be valid
if (!string.IsNullOrEmpty(bestAddress.Properties?.CommonwealthElectorate?.ToString()))
{
AddElectorates(bestAddress);
ShowFindMPsButton = true;
ReportLabelText = "";

var httpResponse = await GeoscapeClient.GetFirstAddressData(_address + " " + state);
electoratePopupTitle = "Electorates Found!";
electoratePopupText = ("Federal electorate: " +
IndividualParticipant.ProfileData.CommonwealthElectorate +
"\nState lower house electorate: " +
IndividualParticipant.ProfileData.StateLowerHouseElectorate);

// The compiler doesn't think this null check is necessary, but it is.
if (httpResponse != null)
{
if (httpResponse.Failure)
{
if (httpResponse is ErrorResult<GeoscapeAddressFeature> errorResult)
{
ReportLabelText = errorResult.Message;
}
else
{
// Generic error message if no specific one available.
ReportLabelText = AppResources.ErrorFindingAddress;
}

// Display a popup if Electorates were not found
var popup = new OneButtonPopup(AppResources.ElectoratesNotFoundErrorText, AppResources.OKText);
_ = await Application.Current.MainPage.Navigation.ShowPopupAsync(popup);
return;
}
// just save the address all the time now if it returned a valid electorate
SaveAddress();
}
await ShowOneButtonPopup(electoratePopupTitle, electoratePopupText, AppResources.OKText);

// httpResponse.Success
var bestAddress = httpResponse.Data;
// needs a federal electorate to be valid
if (!string.IsNullOrEmpty(bestAddress.Properties?.CommonwealthElectorate?.ToString()))
{
AddElectorates(bestAddress);
ShowFindMPsButton = true;
ReportLabelText = "";

var electoratePopupTitle = "Electorates Found!";
var electoratePopupText = "Federal electorate: " + IndividualParticipant.ProfileData.CommonwealthElectorate +
"\n" + "State lower house electorate: " + IndividualParticipant.ProfileData.StateLowerHouseElectorate;
var electoratesFoundPopup = new OneButtonPopup(electoratePopupTitle, electoratePopupText, AppResources.OKText);
_ = await Application.Current.MainPage.Navigation.ShowPopupAsync(electoratesFoundPopup);

// just save the address all the time now if it returned a valid electorate
SaveAddress();
}
else
{
var electoratesNotFoundPopup = new OneButtonPopup("Electorates not Found", "Please reformat the address and try again.", AppResources.OKText);
_ = await Application.Current.MainPage.Navigation.ShowPopupAsync(electoratesNotFoundPopup);
}
ShowSkipButton = false;
// display the map if we stored the Federal Electorate properly
if (!string.IsNullOrEmpty(IndividualParticipant.ProfileData.CommonwealthElectorate))
{
ShowMapFrame = true;
ShowKnowElectoratesFrame = false;
ShowAddressStack = false;
var electorateString = ParliamentData.ConvertGeoscapeElectorateToStandard(state, IndividualParticipant.ProfileData.CommonwealthElectorate);
ShowMapOfElectorate(electorateString);
}
}
ShowSkipButton = false;
// display the map if we stored the Federal Electorate properly
if (!string.IsNullOrEmpty(IndividualParticipant.ProfileData.CommonwealthElectorate))
{
ShowMapFrame = true;
ShowKnowElectoratesFrame = false;
ShowAddressStack = false;
var electorateString = ParliamentData.ConvertGeoscapeElectorateToStandard(state,
IndividualParticipant.ProfileData.CommonwealthElectorate);
ShowMapOfElectorate(electorateString);
}
}

Expand Down Expand Up @@ -521,6 +551,7 @@ private void OnStatePickerSelectedIndexChanged()

private void CheckPostcode()
{
// TODO: reduce complexity (need to be testablr - currently, SelectedStateEnum isn't accessible)
switch (SelectedStateEnum)
{
case ParliamentData.StateEnum.ACT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,10 +597,10 @@ private async void SendQuestionEditToServer()
// FIXME at the moment, the version isn't been correctly updated.
// TODO: Here, we'll need to ensure we've got the right version (from the server - get it returned from
// BuildSignAndUpload...
var popup = new TwoButtonPopup(QuestionViewModel.Instance, AppResources.QuestionEditSuccessfulPopupTitle, AppResources.QuestionEditSuccessfulPopupText, AppResources.StayOnCurrentPageButtonText, AppResources.GoHomeButtonText);
var result = await App.Current.MainPage.Navigation.ShowPopupAsync(popup);
var popup = new TwoButtonPopup(AppResources.QuestionEditSuccessfulPopupTitle, AppResources.QuestionEditSuccessfulPopupText, AppResources.StayOnCurrentPageButtonText, AppResources.GoHomeButtonText, false);
var popupResult = await App.Current.MainPage.Navigation.ShowPopupAsync(popup);

if (ApproveButtonClicked)
if (popup.HasApproved(popupResult))
{
await Application.Current.MainPage.Navigation.PopToRootAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,9 @@ private async void OnSaveButtonClicked()
if (!IndividualParticipant.ElectoratesKnown)
{
// if MPs have not been found for this user yet, prompt to find them
var popup = new TwoButtonPopup(this, AppResources.MPsPopupTitle, AppResources.MPsPopupText, AppResources.SkipButtonText, AppResources.YesButtonText);
_ = await Application.Current.MainPage.Navigation.ShowPopupAsync(popup);
if (ApproveButtonClicked)
var popup = new TwoButtonPopup(AppResources.MPsPopupTitle, AppResources.MPsPopupText, AppResources.SkipButtonText, AppResources.YesButtonText, false);
var popupResult = await Application.Current.MainPage.Navigation.ShowPopupAsync(popup);
if (popup.HasApproved(popupResult))
{
NavigateToFindMPsPage();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ public async void OnWebViewNavigating(object sender, WebNavigatingEventArgs e)
var alertCancel = AppResources.CancelButtonText;
var alertConfirmation = AppResources.NavigateOKText;

var popup = new TwoButtonPopup(baseViewModel, AppResources.NavigationPopupTitle, alertText, alertCancel, alertConfirmation);
_ = await Application.Current.MainPage.Navigation.ShowPopupAsync(popup);
if (baseViewModel.ApproveButtonClicked)
var popup = new TwoButtonPopup(AppResources.NavigationPopupTitle, alertText, alertCancel, alertConfirmation, false);
var popupResult = await Application.Current.MainPage.Navigation.ShowPopupAsync(popup);
if (popup.HasApproved(popupResult))
{
var browserDestination = new Uri(destination);
await OpenBrowser(browserDestination);
Expand Down
Loading

0 comments on commit e0ed712

Please sign in to comment.