Skip to content

Commit

Permalink
Merge pull request #138 from RightToAskOrg/IndividualParticipant-refa…
Browse files Browse the repository at this point in the history
…ctoring

Individual participant refactoring
  • Loading branch information
vteague authored Dec 10, 2022
2 parents e0ed712 + 9d35c43 commit c6346c3
Show file tree
Hide file tree
Showing 32 changed files with 760 additions and 378 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text.Json;
using System.Diagnostics;
using RightToAskClient.CryptoUtils;
using RightToAskClient.Helpers;
using RightToAskClient.Models.ServerCommsData;
using RightToAskClient.Views;

Expand Down Expand Up @@ -51,9 +52,9 @@ protected override async void OnStart()
// Order is important here: the Filters need to be (re-)initialised after we've read MP and Committee data.
GlobalFilterChoices.InitSelectableLists();

IndividualParticipant.Init();
IndividualParticipant.getInstance().Init();

if(IndividualParticipant.ElectoratesKnown)
if(IndividualParticipant.getInstance().ProfileData.RegistrationInfo.ElectoratesKnown)
{
GlobalFilterChoices.UpdateMyMPLists();
}
Expand Down Expand Up @@ -105,7 +106,7 @@ private void SetTheStyles()
private void ResetAppData()
{
// clear the preferences, which holds the user's account registration info
Preferences.Clear();
XamarinPreferences.shared.Clear();
// TODO: wipe the crypto/signing key
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,47 @@ namespace RightToAskClient.Helpers
{
public static class NavigationUtils
{
public static async Task PushMyAnsweringMPsExploringPage()
public static async Task PushMyAnsweringMPsExploringPage(bool electoratesKnown)
{
var message = "These are your MPs. Select the one(s) who should answer the question";

//TODO** Seems unnecessary if our MPs are not initialized.
// Below, don't make the pages that are never used. The code is (somewhat redundant but)
// correct but names are confusing.
var mpsSelectableListPage = new SelectableListPage(App.GlobalFilterChoices.AnsweringMPsListsMine, message);
var nextPage = ListMPsFindFirstIfNotAlreadyKnown(mpsSelectableListPage);
var nextPage = ListMPsFindFirstIfNotAlreadyKnown(mpsSelectableListPage, electoratesKnown);
await Application.Current.MainPage.Navigation.PushAsync(nextPage);
}

public static async Task PushMyAskingMPsExploringPage()
public static async Task PushMyAskingMPsExploringPage(bool electoratesKnown)
{
var message = "These are your MPs. Select the one(s) who should raise the question in Parliament";

var mpsSelectableListPage = new SelectableListPage(App.GlobalFilterChoices.AskingMPsListsMine, message);
await LaunchMPFindingAndSelectingPages(mpsSelectableListPage);
await LaunchMPFindingAndSelectingPages(mpsSelectableListPage, electoratesKnown);
}

//
private static async Task LaunchMPFindingAndSelectingPages(SelectableListPage mpsListPage)
private static async Task LaunchMPFindingAndSelectingPages(SelectableListPage mpsListPage, bool electoratesKnown)
{
var nextPage = ListMPsFindFirstIfNotAlreadyKnown(mpsListPage);
var nextPage = ListMPsFindFirstIfNotAlreadyKnown(mpsListPage, electoratesKnown);
await Application.Current.MainPage.Navigation.PushAsync(nextPage);
}

/*
* Either push the list of selectable MPs directly, or push a registration page,
* instructed to push the MPs selection page after.
*/
public static Page ListMPsFindFirstIfNotAlreadyKnown(SelectableListPage mpsListPage)
public static Page ListMPsFindFirstIfNotAlreadyKnown(SelectableListPage mpsListPage, bool electoratesKnown)
{
if (!IndividualParticipant.ElectoratesKnown)
if (electoratesKnown)
{
var registrationPage = new RegisterPage2();
return registrationPage;
return mpsListPage;
}
else
{
return mpsListPage;
var registrationPage = new FindMPsPage();
return registrationPage;
}
}

Expand All @@ -84,16 +84,19 @@ var committeeSelectableListPage
await Application.Current.MainPage.Navigation.PushAsync(committeeSelectableListPage);
}

public static async Task DoRegistrationCheck(BaseViewModel vm)
public static async Task DoRegistrationCheck(Registration registration, string cancelMessage)
{
if (!IndividualParticipant.IsRegistered)
var popup = new TwoButtonPopup(
AppResources.MakeAccountQuestionText,
AppResources.CreateAccountPopUpText,
cancelMessage,
AppResources.OKText,
false);
var popupResult = await App.Current.MainPage.Navigation.ShowPopupAsync(popup);
if (popup.HasApproved(popupResult))
{
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)}");
}
var registerAccountPage = new RegisterAccountPage(registration);
await Application.Current.MainPage.Navigation.PushAsync(registerAccountPage);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
using System;
using System.Collections.Generic;
using Xamarin.Essentials;

namespace RightToAskClient.Helpers
{
public class XamarinPreferences
{
private Dictionary<string, object> storage = new Dictionary<string, object>();

public void Clear()
{
if (DeviceInfo.Platform == DevicePlatform.Unknown)
{
storage.Clear();
}
else
{
Preferences.Clear();
}
}

public bool ContainsKey(string key)
{
if (DeviceInfo.Platform == DevicePlatform.Unknown)
{
return storage.ContainsKey(key);
}
else
{
return Preferences.ContainsKey(key);
}
}

public string Get(string key, string defaultValue, string? sharedName = null)
{
if (DeviceInfo.Platform == DevicePlatform.Unknown)
{
try
{
return (string)storage[key];
}
catch (KeyNotFoundException)
{
return defaultValue;
}
}
else
{
if (sharedName == null)
{
return Preferences.Get(key, defaultValue);
}
else
{
return Preferences.Get(key, defaultValue, sharedName);
}
}
}
public bool Get(string key, bool defaultValue, string? sharedName = null)
{
if (DeviceInfo.Platform == DevicePlatform.Unknown)
{
try
{
return (bool)storage[key];
}
catch (KeyNotFoundException)
{
return defaultValue;
}
}
else
{
if (sharedName == null)
{
return Preferences.Get(key, defaultValue);
}
else
{
return Preferences.Get(key, defaultValue, sharedName);
}
}
}

public void Set(string key, string value, string? sharedName = null)
{
if (DeviceInfo.Platform == DevicePlatform.Unknown)
{
if (storage.ContainsKey(key))
{
storage[key] = value;
}
else
{
storage.Add(key, value);
}
}
else
{
if (sharedName == null)
{
Preferences.Set(key, value);
}
else
{
Preferences.Set(key, value, sharedName);
}
}
}

public void Set(string key, bool value, string? sharedName = null)
{
if (DeviceInfo.Platform == DevicePlatform.Unknown)
{
if (storage.ContainsKey(key))
{
storage[key] = value;
}
else
{
storage.Add(key, value);
}
}
else
{
if (sharedName == null)
{
Preferences.Set(key, value);
}
else
{
Preferences.Set(key, value, sharedName);
}
}
}

// XamarinPrefernces.shared.
// Preferences.
public static XamarinPreferences shared = new XamarinPreferences();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,9 @@ public static async Task<JOSResult<string>> RegisterNewUser(Registration newReg)
return await SendDataToServerVerifySignedResponse(newUserForServer, "user", RegUrl);
}

public static async Task<JOSResult<string>> UpdateExistingUser(ServerUser existingReg)
public static async Task<JOSResult<string>> UpdateExistingUser(ServerUser existingReg, string uid)
{
Debug.Assert(IndividualParticipant.IsRegistered);
return await SignAndSendDataToServer(existingReg, "user", EditUserUrl,
AppResources.AccountUpdateSigningError);
return await SignAndSendDataToServer(existingReg, "user", EditUserUrl, AppResources.AccountUpdateSigningError, uid);
}


Expand Down Expand Up @@ -147,19 +145,18 @@ public static async Task<JOSResult<List<string>>> GetQuestionsByWriterId(string
var getQuestionByWriterUrl = GetQuestionByWriterUrl + Uri.EscapeDataString(userId);
return await Client.DoGetResultRequest<List<string>>(getQuestionByWriterUrl);
}
public static async Task<JOSResult<string>> RegisterNewQuestion(QuestionSendToServer newQuestion)
public static async Task<JOSResult<string>> RegisterNewQuestion(QuestionSendToServer newQuestion, string uid)
{
return await SignAndSendDataToServer(newQuestion, AppResources.QuestionErrorTypeDescription, QnUrl,"Error publishing New Question");
return await SignAndSendDataToServer(newQuestion, AppResources.QuestionErrorTypeDescription, QnUrl,"Error publishing New Question", uid);
}

public static async Task<JOSResult<string>> UpdateExistingQuestion(QuestionSendToServer existingQuestion)
public static async Task<JOSResult<string>> UpdateExistingQuestion(QuestionSendToServer existingQuestion, string uid)
{
return await SignAndSendDataToServer(existingQuestion, AppResources.QuestionErrorTypeDescription, EditQnUrl, "Error editing question");
return await SignAndSendDataToServer(existingQuestion, AppResources.QuestionErrorTypeDescription, EditQnUrl, "Error editing question", uid);
}

public static async Task<JOSResult<string>> RequestEmailValidation(RequestEmailValidationMessage msg, string email)
public static async Task<JOSResult<string>> RequestEmailValidation(ClientSignedUnparsed signedMsg, string email)
{
var signedMsg = IndividualParticipant.SignMessage(msg);
var serverSend = new RequestEmailValidationAPICall()
{
email = email,
Expand All @@ -171,16 +168,16 @@ public static async Task<JOSResult<string>> RequestEmailValidation(RequestEmailV

}

public static async Task<JOSResult<string>> SendEmailValidationPin(EmailValidationPIN msg)
public static async Task<JOSResult<string>> SendEmailValidationPin(EmailValidationPIN msg, string uid)
{
return await SignAndSendDataToServer(msg, "Sending PIN", EmailValidationPinUrl, "Signing PIN");
return await SignAndSendDataToServer(msg, "Sending PIN", EmailValidationPinUrl, "Signing PIN", uid);
}

// Sign a message (data) with this user's key, then upload to the specified url.
// "description" and "error string" are for reporting errors in upload and signing resp.
private static async Task<JOSResult<string>> SignAndSendDataToServer<T>(T data, string description, string url, string errorString)
private static async Task<JOSResult<string>> SignAndSendDataToServer<T>(T data, string description, string url, string errorString, string uid)
{
var signedUserMessage = IndividualParticipant.SignMessage(data);
var signedUserMessage = ClientSignatureGenerationService.SignMessage(data, uid);
if (!string.IsNullOrEmpty(signedUserMessage.signature))
{
return await SendDataToServerVerifySignedResponse(signedUserMessage, description, url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public string StreetNumberAndName
var changed = SetProperty(ref _streetNumberAndName, value);
if (changed)
{
IndividualParticipant.AddressUpdated = true;
IndividualParticipant.getInstance().AddressUpdated = true;
}
}
else
Expand All @@ -35,7 +35,7 @@ public string CityOrSuburb
var changed = SetProperty(ref _cityOrSuburb, value);
if (changed)
{
IndividualParticipant.AddressUpdated = true;
IndividualParticipant.getInstance().AddressUpdated = true;
}
}
else
Expand All @@ -55,7 +55,7 @@ public string Postcode
var changed = SetProperty(ref _postCode, value);
if (changed)
{
IndividualParticipant.AddressUpdated = true;
IndividualParticipant.getInstance().AddressUpdated = true;
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void RemoveAllSelections()
// it's still possible for the question to go to 'my MP' when that person is their previous MP.
public void UpdateMyMPLists()
{
AnsweringMPsListsMine.AllEntities = ParliamentData.FindAllMPsGivenElectorates(IndividualParticipant.ProfileData.RegistrationInfo.Electorates.ToList());
AnsweringMPsListsMine.AllEntities = ParliamentData.FindAllMPsGivenElectorates(IndividualParticipant.getInstance().ProfileData.RegistrationInfo.Electorates.ToList());
AskingMPsListsMine.AllEntities = AnsweringMPsListsMine.AllEntities;
}

Expand Down
Loading

0 comments on commit c6346c3

Please sign in to comment.