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/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(

@ -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;
}

@ -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();

@ -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.
///

@ -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<String, BasicEvent> 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<String, dynamic>` object
/// representing the user's profile information.
static Map<String, dynamic>? get profile => MatrixState.pangeaController
.matrixState.client.accountData[ModelKey.userProfile]?.content;
Map<String, dynamic>? 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<void> saveProfileData(
Future<void> saveProfileData(
Map<String, dynamic> 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<String, dynamic> toJson() {
Map<String, dynamic> toJson() {
final Map<String, dynamic> 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<MatrixProfileEnum> get toolSettings => [

@ -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,

@ -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<MessageToolbar> {
return;
}
MatrixProfile.autoPlayMessages
MatrixState.pangeaController.userController.matrixProfile.autoPlayMessages
? updateMode(MessageMode.textToSpeech)
: updateMode(MessageMode.translation);
});

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

@ -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);

Loading…
Cancel
Save