From 6ab62e2effa7a371b6e88acdaf86f0edd4cd5df8 Mon Sep 17 00:00:00 2001 From: ggurdin Date: Thu, 11 Jul 2024 10:02:53 -0400 Subject: [PATCH] added functionality to look for data in the only matrix format if it's missing in the new format, make matrixProfile an instance member of userController --- .../controllers/choreographer.dart | 4 +- .../controllers/permissions_controller.dart | 15 +++-- .../controllers/subscription_controller.dart | 4 +- lib/pangea/controllers/user_controller.dart | 21 ++++--- lib/pangea/models/user_model.dart | 56 +++++++++++++------ .../settings_learning_view.dart | 6 +- lib/pangea/widgets/chat/message_toolbar.dart | 3 +- lib/pangea/widgets/igc/span_card.dart | 3 +- .../p_settings_switch_list_tile.dart | 7 ++- 9 files changed, 75 insertions(+), 44 deletions(-) diff --git a/lib/pangea/choreographer/controllers/choreographer.dart b/lib/pangea/choreographer/controllers/choreographer.dart index db317cd4e..973a89747 100644 --- a/lib/pangea/choreographer/controllers/choreographer.dart +++ b/lib/pangea/choreographer/controllers/choreographer.dart @@ -14,7 +14,6 @@ import 'package:fluffychat/pangea/models/it_step.dart'; import 'package:fluffychat/pangea/models/representation_content_model.dart'; import 'package:fluffychat/pangea/models/space_model.dart'; import 'package:fluffychat/pangea/models/tokens_event_content_model.dart'; -import 'package:fluffychat/pangea/models/user_model.dart'; import 'package:fluffychat/pangea/utils/any_state_holder.dart'; import 'package:fluffychat/pangea/utils/error_handler.dart'; import 'package:fluffychat/pangea/utils/overlay.dart'; @@ -515,7 +514,8 @@ class Choreographer { chatController.room, ); - bool get itAutoPlayEnabled => MatrixProfile.itAutoPlay; + bool get itAutoPlayEnabled => + pangeaController.userController.matrixProfile.itAutoPlay; bool get definitionsEnabled => pangeaController.permissionsController.isToolEnabled( diff --git a/lib/pangea/controllers/permissions_controller.dart b/lib/pangea/controllers/permissions_controller.dart index 3932eb7d2..463a8c9a3 100644 --- a/lib/pangea/controllers/permissions_controller.dart +++ b/lib/pangea/controllers/permissions_controller.dart @@ -32,7 +32,8 @@ class PermissionsController extends BaseController { /// Returns false if user is null bool isUser18() => - MatrixProfile.dateOfBirth?.isAtLeastYearsOld( + _pangeaController.userController.matrixProfile.dateOfBirth + ?.isAtLeastYearsOld( AgeLimits.toAccessFeatures, ) ?? false; @@ -99,15 +100,17 @@ class PermissionsController extends BaseController { bool userToolSetting(MatrixProfileEnum setting) { switch (setting.asToolSetting) { case ToolSetting.interactiveTranslator: - return MatrixProfile.interactiveTranslator; + return _pangeaController + .userController.matrixProfile.interactiveTranslator; case ToolSetting.interactiveGrammar: - return MatrixProfile.interactiveGrammar; + return _pangeaController + .userController.matrixProfile.interactiveGrammar; case ToolSetting.immersionMode: - return MatrixProfile.immersionMode; + return _pangeaController.userController.matrixProfile.immersionMode; case ToolSetting.definitions: - return MatrixProfile.definitions; + return _pangeaController.userController.matrixProfile.definitions; case ToolSetting.autoIGC: - return MatrixProfile.autoIGC; + return _pangeaController.userController.matrixProfile.autoIGC; default: return false; } diff --git a/lib/pangea/controllers/subscription_controller.dart b/lib/pangea/controllers/subscription_controller.dart index 9e5d59b05..e825a7126 100644 --- a/lib/pangea/controllers/subscription_controller.dart +++ b/lib/pangea/controllers/subscription_controller.dart @@ -181,10 +181,10 @@ class SubscriptionController extends BaseController { bool get _activatedNewUserTrial => _pangeaController.userController.inTrialWindow && - MatrixProfile.activatedFreeTrial; + _pangeaController.userController.matrixProfile.activatedFreeTrial; void activateNewUserTrial() { - MatrixProfile.saveProfileData({ + _pangeaController.userController.matrixProfile.saveProfileData({ MatrixProfileEnum.activatedFreeTrial.title: true, }).then((_) { setNewUserTrial(); diff --git a/lib/pangea/controllers/user_controller.dart b/lib/pangea/controllers/user_controller.dart index fa72e6b58..541371e34 100644 --- a/lib/pangea/controllers/user_controller.dart +++ b/lib/pangea/controllers/user_controller.dart @@ -27,6 +27,11 @@ class UserController extends BaseController { String? get _matrixAccessToken => _pangeaController.matrixState.client.accessToken; + /// An instance of matrix profile. Used to update and access info from the user's matrix profile. + /// No information needs to be passing in the constructor as the matrix + /// profile get all of it's internal data the accountData stored in the client. + MatrixProfile matrixProfile = MatrixProfile(); + /// Returns the [PUserModel] object representing the current user. /// /// This method retrieves the user data from the local storage using the [PLocalKey.user] key. @@ -58,7 +63,7 @@ class UserController extends BaseController { matrixAccessToken: _matrixAccessToken!, ); newUserModel.save(_pangeaController); - await MatrixProfile.saveProfileData( + await matrixProfile.saveProfileData( {MatrixProfileEnum.dateOfBirth.title: dob}, waitForDataInSync: true, ); @@ -161,7 +166,7 @@ class UserController extends BaseController { final Profile? pangeaProfile = userModel?.profile; for (final field in MatrixProfile.pangeaProfileFields) { - final dynamic matrixValue = MatrixProfile.getProfileData(field); + final dynamic matrixValue = matrixProfile.getProfileData(field); dynamic pangeaValue; switch (field) { case MatrixProfileEnum.dateOfBirth: @@ -194,7 +199,7 @@ class UserController extends BaseController { if (profileUpdates.containsKey(value.title)) continue; final dynamic localValue = _pangeaController.pStoreService.read(value.title); - final dynamic matrixValue = MatrixProfile.getProfileData(value); + final dynamic matrixValue = matrixProfile.getProfileData(value); final dynamic unmigratedValue = localValue != null && matrixValue == null ? localValue : null; if (unmigratedValue != null) { @@ -202,7 +207,7 @@ class UserController extends BaseController { } } - await MatrixProfile.saveProfileData( + await matrixProfile.saveProfileData( profileUpdates, waitForDataInSync: true, ); @@ -271,7 +276,7 @@ class UserController extends BaseController { profile: updatedUserProfile, ).save(_pangeaController); - MatrixProfile.saveProfileData({ + matrixProfile.saveProfileData({ MatrixProfileEnum.dateOfBirth.title: dateOfBirth, MatrixProfileEnum.targetLanguage.title: targetLanguage, MatrixProfileEnum.sourceLanguage.title: sourceLanguage, @@ -340,7 +345,7 @@ class UserController extends BaseController { // the function fetchUserModel() uses a completer, so it shouldn't // re-call the endpoint if it has already been called await fetchUserModel(); - return MatrixProfile.dateOfBirth != null; + return matrixProfile.dateOfBirth != null; } catch (err, s) { ErrorHandler.logError(e: err, s: s); return false; @@ -387,9 +392,7 @@ class UserController extends BaseController { } /// Returns a boolean value indicating whether the user's profile is public. - bool get isPublic => - _pangeaController.userController.userModel?.profile?.publicProfile ?? - false; + bool get isPublic => userModel?.profile?.publicProfile ?? false; /// Retrieves the user's email address. /// diff --git a/lib/pangea/models/user_model.dart b/lib/pangea/models/user_model.dart index af7de1ee5..af085bdc5 100644 --- a/lib/pangea/models/user_model.dart +++ b/lib/pangea/models/user_model.dart @@ -136,15 +136,37 @@ extension MatrixProfileEnumExtension on MatrixProfileEnum { /// So this class is more of a helper class to make it easier to /// access and save the data. class MatrixProfile { + /// Convenience function get get user's account data from the client + Map get accountData => + MatrixState.pangeaController.matrixState.client.accountData; + /// Returns the profile of the user. /// - /// The profile is retrieved from the `MatrixState.pangeaController.matrixState.client.accountData` + /// The profile is retrieved from the user's account data /// using the key `ModelKey.userProfile`. It returns a `Map` object /// representing the user's profile information. - static Map? get profile => MatrixState.pangeaController - .matrixState.client.accountData[ModelKey.userProfile]?.content; + Map? get profile => + accountData[ModelKey.userProfile]?.content; - static dynamic getProfileData(MatrixProfileEnum key) => profile?[key.title]; + /// Retrieves the profile data for the given [key]. + /// + /// This method first tries to get the data from the new profile format. If the data is found, + /// it is returned. If not, it checks if the data is stored in the old format. If it is, the data + /// is saved to the new format and returned. + dynamic getProfileData(MatrixProfileEnum key) { + // try to get the data from the new profile format + if (profile?[key.title] != null) { + return profile?[key.title]; + } + + // check if the data is stored in the old format + // and if so, save it to the new format + final prevFormatData = accountData[key.title]?.content[key.title]; + if (prevFormatData != null) { + saveProfileData({key.title: prevFormatData}); + return prevFormatData; + } + } /// Saves the profile data by updating the current user's profile with the provided updates. /// @@ -156,7 +178,7 @@ class MatrixProfile { /// If [waitForDataInSync] is true, the function will wait for the updated data in a sync update /// If this is set to false, after this function completes there may be a gap where the /// data has been sent but is not in the client's account data, as the sync update has not yet been received. - static Future saveProfileData( + Future saveProfileData( Map updates, { waitForDataInSync = false, }) async { @@ -165,7 +187,7 @@ class MatrixProfile { if (entry.value == null) continue; currentProfile[entry.key] = entry.value; } - if (mapEquals(MatrixProfile.toJson(), currentProfile)) return; + if (mapEquals(toJson(), currentProfile)) return; final PangeaController pangeaController = MatrixState.pangeaController; final Client client = pangeaController.matrixState.client; @@ -192,7 +214,7 @@ class MatrixProfile { } /// Converts the Matrix Profile to a JSON representation. - static Map toJson() { + Map toJson() { final Map json = {}; for (final value in MatrixProfileEnum.values) { json[value.title] = getProfileData(value); @@ -205,26 +227,24 @@ class MatrixProfile { // need for repeating the same code (like parsing DateTimes or // assigning default values to null booleans) when accessing specific values. - static DateTime? get dateOfBirth { + DateTime? get dateOfBirth { final dob = getProfileData(MatrixProfileEnum.dateOfBirth); return dob != null ? DateTime.parse(dob) : null; } - static bool get autoPlayMessages => + bool get autoPlayMessages => getProfileData(MatrixProfileEnum.autoPlayMessages) ?? false; - static bool get itAutoPlay => - getProfileData(MatrixProfileEnum.itAutoPlay) ?? false; - static bool get activatedFreeTrial => + bool get itAutoPlay => getProfileData(MatrixProfileEnum.itAutoPlay) ?? false; + bool get activatedFreeTrial => getProfileData(MatrixProfileEnum.activatedFreeTrial) ?? false; - static bool get interactiveTranslator => + bool get interactiveTranslator => getProfileData(MatrixProfileEnum.interactiveTranslator) ?? true; - static bool get interactiveGrammar => + bool get interactiveGrammar => getProfileData(MatrixProfileEnum.interactiveGrammar) ?? true; - static bool get immersionMode => + bool get immersionMode => getProfileData(MatrixProfileEnum.immersionMode) ?? false; - static bool get definitions => - getProfileData(MatrixProfileEnum.definitions) ?? true; - static bool get autoIGC => getProfileData(MatrixProfileEnum.autoIGC) ?? false; + bool get definitions => getProfileData(MatrixProfileEnum.definitions) ?? true; + bool get autoIGC => getProfileData(MatrixProfileEnum.autoIGC) ?? false; /// A list of all the fields in MatrixProfileEnum that correspond to tool settings static List get toolSettings => [ diff --git a/lib/pangea/pages/settings_learning/settings_learning_view.dart b/lib/pangea/pages/settings_learning/settings_learning_view.dart index 8198e70dc..06136cd7e 100644 --- a/lib/pangea/pages/settings_learning/settings_learning_view.dart +++ b/lib/pangea/pages/settings_learning/settings_learning_view.dart @@ -65,14 +65,16 @@ class SettingsLearningView extends StatelessWidget { ) : const SizedBox(), ProfileSettingsSwitchListTile.adaptive( - defaultValue: MatrixProfile.itAutoPlay, + defaultValue: controller + .pangeaController.userController.matrixProfile.itAutoPlay, title: L10n.of(context)!.interactiveTranslatorAutoPlaySliderHeader, subtitle: L10n.of(context)!.interactiveTranslatorAutoPlayDesc, profileKey: MatrixProfileEnum.itAutoPlay, ), ProfileSettingsSwitchListTile.adaptive( - defaultValue: MatrixProfile.autoPlayMessages, + defaultValue: controller.pangeaController.userController + .matrixProfile.autoPlayMessages, title: L10n.of(context)!.autoPlayTitle, subtitle: L10n.of(context)!.autoPlayDesc, profileKey: MatrixProfileEnum.autoPlayMessages, diff --git a/lib/pangea/widgets/chat/message_toolbar.dart b/lib/pangea/widgets/chat/message_toolbar.dart index ad38e688d..b547efa7a 100644 --- a/lib/pangea/widgets/chat/message_toolbar.dart +++ b/lib/pangea/widgets/chat/message_toolbar.dart @@ -5,7 +5,6 @@ import 'package:fluffychat/config/themes.dart'; import 'package:fluffychat/pages/chat/chat.dart'; import 'package:fluffychat/pangea/enum/message_mode_enum.dart'; import 'package:fluffychat/pangea/matrix_event_wrappers/pangea_message_event.dart'; -import 'package:fluffychat/pangea/models/user_model.dart'; import 'package:fluffychat/pangea/utils/any_state_holder.dart'; import 'package:fluffychat/pangea/utils/error_handler.dart'; import 'package:fluffychat/pangea/utils/overlay.dart'; @@ -334,7 +333,7 @@ class MessageToolbarState extends State { return; } - MatrixProfile.autoPlayMessages + MatrixState.pangeaController.userController.matrixProfile.autoPlayMessages ? updateMode(MessageMode.textToSpeech) : updateMode(MessageMode.translation); }); diff --git a/lib/pangea/widgets/igc/span_card.dart b/lib/pangea/widgets/igc/span_card.dart index 823de6914..c758ea213 100644 --- a/lib/pangea/widgets/igc/span_card.dart +++ b/lib/pangea/widgets/igc/span_card.dart @@ -508,7 +508,8 @@ class DontShowSwitchListTileState extends State { title: Text(L10n.of(context)!.interactiveTranslatorAutoPlaySliderHeader), value: switchValue, onChanged: (value) { - MatrixProfile.saveProfileData( + MatrixState.pangeaController.userController.matrixProfile + .saveProfileData( {MatrixProfileEnum.itAutoPlay.title: value}, ); setState(() => switchValue = value); diff --git a/lib/pangea/widgets/user_settings/p_settings_switch_list_tile.dart b/lib/pangea/widgets/user_settings/p_settings_switch_list_tile.dart index 85dbb7e36..3f04c61fd 100644 --- a/lib/pangea/widgets/user_settings/p_settings_switch_list_tile.dart +++ b/lib/pangea/widgets/user_settings/p_settings_switch_list_tile.dart @@ -1,6 +1,7 @@ import 'package:fluffychat/config/app_config.dart'; import 'package:fluffychat/pangea/models/user_model.dart'; import 'package:fluffychat/pangea/utils/error_handler.dart'; +import 'package:fluffychat/widgets/matrix.dart'; import 'package:flutter/material.dart'; class ProfileSettingsSwitchListTile extends StatefulWidget { @@ -27,7 +28,8 @@ class PSettingsSwitchListTileState @override void initState() { - currentValue = MatrixProfile.getProfileData( + currentValue = MatrixState.pangeaController.userController.matrixProfile + .getProfileData( widget.profileKey, ) ?? widget.defaultValue; @@ -43,7 +45,8 @@ class PSettingsSwitchListTileState subtitle: widget.subtitle != null ? Text(widget.subtitle!) : null, onChanged: (bool newValue) async { try { - MatrixProfile.saveProfileData({ + MatrixState.pangeaController.userController.matrixProfile + .saveProfileData({ widget.profileKey.title: newValue, }); setState(() => currentValue = newValue);