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

pull/1384/head
ggurdin 1 year ago
parent f69ab79c9d
commit 6ab62e2eff

@ -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/representation_content_model.dart';
import 'package:fluffychat/pangea/models/space_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/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/any_state_holder.dart';
import 'package:fluffychat/pangea/utils/error_handler.dart'; import 'package:fluffychat/pangea/utils/error_handler.dart';
import 'package:fluffychat/pangea/utils/overlay.dart'; import 'package:fluffychat/pangea/utils/overlay.dart';
@ -515,7 +514,8 @@ class Choreographer {
chatController.room, chatController.room,
); );
bool get itAutoPlayEnabled => MatrixProfile.itAutoPlay; bool get itAutoPlayEnabled =>
pangeaController.userController.matrixProfile.itAutoPlay;
bool get definitionsEnabled => bool get definitionsEnabled =>
pangeaController.permissionsController.isToolEnabled( pangeaController.permissionsController.isToolEnabled(

@ -32,7 +32,8 @@ class PermissionsController extends BaseController {
/// Returns false if user is null /// Returns false if user is null
bool isUser18() => bool isUser18() =>
MatrixProfile.dateOfBirth?.isAtLeastYearsOld( _pangeaController.userController.matrixProfile.dateOfBirth
?.isAtLeastYearsOld(
AgeLimits.toAccessFeatures, AgeLimits.toAccessFeatures,
) ?? ) ??
false; false;
@ -99,15 +100,17 @@ class PermissionsController extends BaseController {
bool userToolSetting(MatrixProfileEnum setting) { bool userToolSetting(MatrixProfileEnum setting) {
switch (setting.asToolSetting) { switch (setting.asToolSetting) {
case ToolSetting.interactiveTranslator: case ToolSetting.interactiveTranslator:
return MatrixProfile.interactiveTranslator; return _pangeaController
.userController.matrixProfile.interactiveTranslator;
case ToolSetting.interactiveGrammar: case ToolSetting.interactiveGrammar:
return MatrixProfile.interactiveGrammar; return _pangeaController
.userController.matrixProfile.interactiveGrammar;
case ToolSetting.immersionMode: case ToolSetting.immersionMode:
return MatrixProfile.immersionMode; return _pangeaController.userController.matrixProfile.immersionMode;
case ToolSetting.definitions: case ToolSetting.definitions:
return MatrixProfile.definitions; return _pangeaController.userController.matrixProfile.definitions;
case ToolSetting.autoIGC: case ToolSetting.autoIGC:
return MatrixProfile.autoIGC; return _pangeaController.userController.matrixProfile.autoIGC;
default: default:
return false; return false;
} }

@ -181,10 +181,10 @@ class SubscriptionController extends BaseController {
bool get _activatedNewUserTrial => bool get _activatedNewUserTrial =>
_pangeaController.userController.inTrialWindow && _pangeaController.userController.inTrialWindow &&
MatrixProfile.activatedFreeTrial; _pangeaController.userController.matrixProfile.activatedFreeTrial;
void activateNewUserTrial() { void activateNewUserTrial() {
MatrixProfile.saveProfileData({ _pangeaController.userController.matrixProfile.saveProfileData({
MatrixProfileEnum.activatedFreeTrial.title: true, MatrixProfileEnum.activatedFreeTrial.title: true,
}).then((_) { }).then((_) {
setNewUserTrial(); setNewUserTrial();

@ -27,6 +27,11 @@ class UserController extends BaseController {
String? get _matrixAccessToken => String? get _matrixAccessToken =>
_pangeaController.matrixState.client.accessToken; _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. /// Returns the [PUserModel] object representing the current user.
/// ///
/// This method retrieves the user data from the local storage using the [PLocalKey.user] key. /// 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!, matrixAccessToken: _matrixAccessToken!,
); );
newUserModel.save(_pangeaController); newUserModel.save(_pangeaController);
await MatrixProfile.saveProfileData( await matrixProfile.saveProfileData(
{MatrixProfileEnum.dateOfBirth.title: dob}, {MatrixProfileEnum.dateOfBirth.title: dob},
waitForDataInSync: true, waitForDataInSync: true,
); );
@ -161,7 +166,7 @@ class UserController extends BaseController {
final Profile? pangeaProfile = userModel?.profile; final Profile? pangeaProfile = userModel?.profile;
for (final field in MatrixProfile.pangeaProfileFields) { for (final field in MatrixProfile.pangeaProfileFields) {
final dynamic matrixValue = MatrixProfile.getProfileData(field); final dynamic matrixValue = matrixProfile.getProfileData(field);
dynamic pangeaValue; dynamic pangeaValue;
switch (field) { switch (field) {
case MatrixProfileEnum.dateOfBirth: case MatrixProfileEnum.dateOfBirth:
@ -194,7 +199,7 @@ class UserController extends BaseController {
if (profileUpdates.containsKey(value.title)) continue; if (profileUpdates.containsKey(value.title)) continue;
final dynamic localValue = final dynamic localValue =
_pangeaController.pStoreService.read(value.title); _pangeaController.pStoreService.read(value.title);
final dynamic matrixValue = MatrixProfile.getProfileData(value); final dynamic matrixValue = matrixProfile.getProfileData(value);
final dynamic unmigratedValue = final dynamic unmigratedValue =
localValue != null && matrixValue == null ? localValue : null; localValue != null && matrixValue == null ? localValue : null;
if (unmigratedValue != null) { if (unmigratedValue != null) {
@ -202,7 +207,7 @@ class UserController extends BaseController {
} }
} }
await MatrixProfile.saveProfileData( await matrixProfile.saveProfileData(
profileUpdates, profileUpdates,
waitForDataInSync: true, waitForDataInSync: true,
); );
@ -271,7 +276,7 @@ class UserController extends BaseController {
profile: updatedUserProfile, profile: updatedUserProfile,
).save(_pangeaController); ).save(_pangeaController);
MatrixProfile.saveProfileData({ matrixProfile.saveProfileData({
MatrixProfileEnum.dateOfBirth.title: dateOfBirth, MatrixProfileEnum.dateOfBirth.title: dateOfBirth,
MatrixProfileEnum.targetLanguage.title: targetLanguage, MatrixProfileEnum.targetLanguage.title: targetLanguage,
MatrixProfileEnum.sourceLanguage.title: sourceLanguage, MatrixProfileEnum.sourceLanguage.title: sourceLanguage,
@ -340,7 +345,7 @@ class UserController extends BaseController {
// the function fetchUserModel() uses a completer, so it shouldn't // the function fetchUserModel() uses a completer, so it shouldn't
// re-call the endpoint if it has already been called // re-call the endpoint if it has already been called
await fetchUserModel(); await fetchUserModel();
return MatrixProfile.dateOfBirth != null; return matrixProfile.dateOfBirth != null;
} catch (err, s) { } catch (err, s) {
ErrorHandler.logError(e: err, s: s); ErrorHandler.logError(e: err, s: s);
return false; return false;
@ -387,9 +392,7 @@ class UserController extends BaseController {
} }
/// Returns a boolean value indicating whether the user's profile is public. /// Returns a boolean value indicating whether the user's profile is public.
bool get isPublic => bool get isPublic => userModel?.profile?.publicProfile ?? false;
_pangeaController.userController.userModel?.profile?.publicProfile ??
false;
/// Retrieves the user's email address. /// Retrieves the user's email address.
/// ///

@ -136,15 +136,37 @@ extension MatrixProfileEnumExtension on MatrixProfileEnum {
/// So this class is more of a helper class to make it easier to /// So this class is more of a helper class to make it easier to
/// access and save the data. /// access and save the data.
class MatrixProfile { class MatrixProfile {
/// Convenience function get get user's account data from the client
Map<String, BasicEvent> get accountData =>
MatrixState.pangeaController.matrixState.client.accountData;
/// Returns the profile of the user. /// 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<String, dynamic>` object /// using the key `ModelKey.userProfile`. It returns a `Map<String, dynamic>` object
/// representing the user's profile information. /// representing the user's profile information.
static Map<String, dynamic>? get profile => MatrixState.pangeaController Map<String, dynamic>? get profile =>
.matrixState.client.accountData[ModelKey.userProfile]?.content; 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. /// 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 [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 /// 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. /// data has been sent but is not in the client's account data, as the sync update has not yet been received.
static Future<void> saveProfileData( Future<void> saveProfileData(
Map<String, dynamic> updates, { Map<String, dynamic> updates, {
waitForDataInSync = false, waitForDataInSync = false,
}) async { }) async {
@ -165,7 +187,7 @@ class MatrixProfile {
if (entry.value == null) continue; if (entry.value == null) continue;
currentProfile[entry.key] = entry.value; currentProfile[entry.key] = entry.value;
} }
if (mapEquals(MatrixProfile.toJson(), currentProfile)) return; if (mapEquals(toJson(), currentProfile)) return;
final PangeaController pangeaController = MatrixState.pangeaController; final PangeaController pangeaController = MatrixState.pangeaController;
final Client client = pangeaController.matrixState.client; final Client client = pangeaController.matrixState.client;
@ -192,7 +214,7 @@ class MatrixProfile {
} }
/// Converts the Matrix Profile to a JSON representation. /// Converts the Matrix Profile to a JSON representation.
static Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
final Map<String, dynamic> json = {}; final Map<String, dynamic> json = {};
for (final value in MatrixProfileEnum.values) { for (final value in MatrixProfileEnum.values) {
json[value.title] = getProfileData(value); json[value.title] = getProfileData(value);
@ -205,26 +227,24 @@ class MatrixProfile {
// need for repeating the same code (like parsing DateTimes or // need for repeating the same code (like parsing DateTimes or
// assigning default values to null booleans) when accessing specific values. // assigning default values to null booleans) when accessing specific values.
static DateTime? get dateOfBirth { DateTime? get dateOfBirth {
final dob = getProfileData(MatrixProfileEnum.dateOfBirth); final dob = getProfileData(MatrixProfileEnum.dateOfBirth);
return dob != null ? DateTime.parse(dob) : null; return dob != null ? DateTime.parse(dob) : null;
} }
static bool get autoPlayMessages => bool get autoPlayMessages =>
getProfileData(MatrixProfileEnum.autoPlayMessages) ?? false; getProfileData(MatrixProfileEnum.autoPlayMessages) ?? false;
static bool get itAutoPlay => bool get itAutoPlay => getProfileData(MatrixProfileEnum.itAutoPlay) ?? false;
getProfileData(MatrixProfileEnum.itAutoPlay) ?? false; bool get activatedFreeTrial =>
static bool get activatedFreeTrial =>
getProfileData(MatrixProfileEnum.activatedFreeTrial) ?? false; getProfileData(MatrixProfileEnum.activatedFreeTrial) ?? false;
static bool get interactiveTranslator => bool get interactiveTranslator =>
getProfileData(MatrixProfileEnum.interactiveTranslator) ?? true; getProfileData(MatrixProfileEnum.interactiveTranslator) ?? true;
static bool get interactiveGrammar => bool get interactiveGrammar =>
getProfileData(MatrixProfileEnum.interactiveGrammar) ?? true; getProfileData(MatrixProfileEnum.interactiveGrammar) ?? true;
static bool get immersionMode => bool get immersionMode =>
getProfileData(MatrixProfileEnum.immersionMode) ?? false; getProfileData(MatrixProfileEnum.immersionMode) ?? false;
static bool get definitions => bool get definitions => getProfileData(MatrixProfileEnum.definitions) ?? true;
getProfileData(MatrixProfileEnum.definitions) ?? true; bool get autoIGC => getProfileData(MatrixProfileEnum.autoIGC) ?? false;
static bool get autoIGC => getProfileData(MatrixProfileEnum.autoIGC) ?? false;
/// A list of all the fields in MatrixProfileEnum that correspond to tool settings /// A list of all the fields in MatrixProfileEnum that correspond to tool settings
static List<MatrixProfileEnum> get toolSettings => [ static List<MatrixProfileEnum> get toolSettings => [

@ -65,14 +65,16 @@ class SettingsLearningView extends StatelessWidget {
) )
: const SizedBox(), : const SizedBox(),
ProfileSettingsSwitchListTile.adaptive( ProfileSettingsSwitchListTile.adaptive(
defaultValue: MatrixProfile.itAutoPlay, defaultValue: controller
.pangeaController.userController.matrixProfile.itAutoPlay,
title: title:
L10n.of(context)!.interactiveTranslatorAutoPlaySliderHeader, L10n.of(context)!.interactiveTranslatorAutoPlaySliderHeader,
subtitle: L10n.of(context)!.interactiveTranslatorAutoPlayDesc, subtitle: L10n.of(context)!.interactiveTranslatorAutoPlayDesc,
profileKey: MatrixProfileEnum.itAutoPlay, profileKey: MatrixProfileEnum.itAutoPlay,
), ),
ProfileSettingsSwitchListTile.adaptive( ProfileSettingsSwitchListTile.adaptive(
defaultValue: MatrixProfile.autoPlayMessages, defaultValue: controller.pangeaController.userController
.matrixProfile.autoPlayMessages,
title: L10n.of(context)!.autoPlayTitle, title: L10n.of(context)!.autoPlayTitle,
subtitle: L10n.of(context)!.autoPlayDesc, subtitle: L10n.of(context)!.autoPlayDesc,
profileKey: MatrixProfileEnum.autoPlayMessages, profileKey: MatrixProfileEnum.autoPlayMessages,

@ -5,7 +5,6 @@ import 'package:fluffychat/config/themes.dart';
import 'package:fluffychat/pages/chat/chat.dart'; import 'package:fluffychat/pages/chat/chat.dart';
import 'package:fluffychat/pangea/enum/message_mode_enum.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/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/any_state_holder.dart';
import 'package:fluffychat/pangea/utils/error_handler.dart'; import 'package:fluffychat/pangea/utils/error_handler.dart';
import 'package:fluffychat/pangea/utils/overlay.dart'; import 'package:fluffychat/pangea/utils/overlay.dart';
@ -334,7 +333,7 @@ class MessageToolbarState extends State<MessageToolbar> {
return; return;
} }
MatrixProfile.autoPlayMessages MatrixState.pangeaController.userController.matrixProfile.autoPlayMessages
? updateMode(MessageMode.textToSpeech) ? updateMode(MessageMode.textToSpeech)
: updateMode(MessageMode.translation); : updateMode(MessageMode.translation);
}); });

@ -508,7 +508,8 @@ class DontShowSwitchListTileState extends State<DontShowSwitchListTile> {
title: Text(L10n.of(context)!.interactiveTranslatorAutoPlaySliderHeader), title: Text(L10n.of(context)!.interactiveTranslatorAutoPlaySliderHeader),
value: switchValue, value: switchValue,
onChanged: (value) { onChanged: (value) {
MatrixProfile.saveProfileData( MatrixState.pangeaController.userController.matrixProfile
.saveProfileData(
{MatrixProfileEnum.itAutoPlay.title: value}, {MatrixProfileEnum.itAutoPlay.title: value},
); );
setState(() => switchValue = value); setState(() => switchValue = value);

@ -1,6 +1,7 @@
import 'package:fluffychat/config/app_config.dart'; import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/pangea/models/user_model.dart'; import 'package:fluffychat/pangea/models/user_model.dart';
import 'package:fluffychat/pangea/utils/error_handler.dart'; import 'package:fluffychat/pangea/utils/error_handler.dart';
import 'package:fluffychat/widgets/matrix.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
class ProfileSettingsSwitchListTile extends StatefulWidget { class ProfileSettingsSwitchListTile extends StatefulWidget {
@ -27,7 +28,8 @@ class PSettingsSwitchListTileState
@override @override
void initState() { void initState() {
currentValue = MatrixProfile.getProfileData( currentValue = MatrixState.pangeaController.userController.matrixProfile
.getProfileData(
widget.profileKey, widget.profileKey,
) ?? ) ??
widget.defaultValue; widget.defaultValue;
@ -43,7 +45,8 @@ class PSettingsSwitchListTileState
subtitle: widget.subtitle != null ? Text(widget.subtitle!) : null, subtitle: widget.subtitle != null ? Text(widget.subtitle!) : null,
onChanged: (bool newValue) async { onChanged: (bool newValue) async {
try { try {
MatrixProfile.saveProfileData({ MatrixState.pangeaController.userController.matrixProfile
.saveProfileData({
widget.profileKey.title: newValue, widget.profileKey.title: newValue,
}); });
setState(() => currentValue = newValue); setState(() => currentValue = newValue);

Loading…
Cancel
Save