From 7e87c3154c3d1943a1cd59f0fddcea9ea174e8df Mon Sep 17 00:00:00 2001 From: ggurdin Date: Tue, 16 Jul 2024 09:38:54 -0400 Subject: [PATCH] fix for orange box error in learning settings, updated instructions to base their toggle status on matrix profile --- lib/pangea/enum/instructions_enum.dart | 18 +- .../settings_learning/settings_learning.dart | 25 +-- .../settings_learning_view.dart | 157 ++++++++---------- lib/pangea/utils/instructions.dart | 37 +++-- .../user_settings/country_picker_tile.dart | 36 ++-- .../user_settings/p_language_dialog.dart | 5 +- 6 files changed, 136 insertions(+), 142 deletions(-) diff --git a/lib/pangea/enum/instructions_enum.dart b/lib/pangea/enum/instructions_enum.dart index 376d004b5..4e12c12b8 100644 --- a/lib/pangea/enum/instructions_enum.dart +++ b/lib/pangea/enum/instructions_enum.dart @@ -1,4 +1,5 @@ import 'package:fluffychat/utils/platform_infos.dart'; +import 'package:fluffychat/widgets/matrix.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; @@ -9,7 +10,7 @@ enum InstructionsEnum { tooltipInstructions, } -extension Copy on InstructionsEnum { +extension InstructionsEnumExtension on InstructionsEnum { String title(BuildContext context) { switch (this) { case InstructionsEnum.itInstructions: @@ -37,6 +38,21 @@ extension Copy on InstructionsEnum { : L10n.of(context)!.tooltipInstructionsBrowserBody; } } + + bool get toggledOff { + final instructionSettings = + MatrixState.pangeaController.userController.profile.instructionSettings; + switch (this) { + case InstructionsEnum.itInstructions: + return instructionSettings.showedItInstructions; + case InstructionsEnum.clickMessage: + return instructionSettings.showedClickMessage; + case InstructionsEnum.blurMeansTranslate: + return instructionSettings.showedBlurMeansTranslate; + case InstructionsEnum.tooltipInstructions: + return instructionSettings.showedTooltipInstructions; + } + } } enum InlineInstructions { diff --git a/lib/pangea/pages/settings_learning/settings_learning.dart b/lib/pangea/pages/settings_learning/settings_learning.dart index 03e5f635a..3e0a11e4c 100644 --- a/lib/pangea/pages/settings_learning/settings_learning.dart +++ b/lib/pangea/pages/settings_learning/settings_learning.dart @@ -1,5 +1,3 @@ -import 'dart:async'; - import 'package:country_picker/country_picker.dart'; import 'package:fluffychat/pangea/controllers/pangea_controller.dart'; import 'package:fluffychat/pangea/models/space_model.dart'; @@ -17,25 +15,26 @@ class SettingsLearning extends StatefulWidget { } class SettingsLearningController extends State { - late StreamSubscription _userSubscription; PangeaController pangeaController = MatrixState.pangeaController; - Future changeLanguage() async { - await pLanguageDialog(context, () {}); - } - - Future setPublicProfile(bool isPublic) async { + setPublicProfile(bool isPublic) { pangeaController.userController.updateProfile((profile) { profile.userSettings.publicProfile = isPublic; return profile; }); + setState(() {}); } - Future changeCountry(Country country) async { - pangeaController.userController.updateProfile((profile) { + void changeLanguage() { + pLanguageDialog(context, () {}).then((_) => setState(() {})); + } + + void changeCountry(Country country) { + pangeaController.userController.updateProfile((Profile profile) { profile.userSettings.country = country.displayNameNoCountryCode; return profile; }); + setState(() {}); } void updateToolSetting(ToolSetting toolSetting, bool value) { @@ -71,12 +70,6 @@ class SettingsLearningController extends State { } } - @override - void dispose() { - super.dispose(); - _userSubscription.cancel(); - } - @override Widget build(BuildContext context) { return SettingsLearningView(this); diff --git a/lib/pangea/pages/settings_learning/settings_learning_view.dart b/lib/pangea/pages/settings_learning/settings_learning_view.dart index 5422ebbe5..4182ab2d6 100644 --- a/lib/pangea/pages/settings_learning/settings_learning_view.dart +++ b/lib/pangea/pages/settings_learning/settings_learning_view.dart @@ -1,15 +1,12 @@ +import 'package:fluffychat/config/app_config.dart'; import 'package:fluffychat/pangea/models/space_model.dart'; import 'package:fluffychat/pangea/pages/settings_learning/settings_learning.dart'; -import 'package:fluffychat/pangea/utils/error_handler.dart'; import 'package:fluffychat/pangea/widgets/user_settings/country_picker_tile.dart'; import 'package:fluffychat/pangea/widgets/user_settings/language_tile.dart'; import 'package:fluffychat/pangea/widgets/user_settings/p_settings_switch_list_tile.dart'; import 'package:fluffychat/widgets/layouts/max_width_body.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; -import 'package:future_loading_dialog/future_loading_dialog.dart'; - -import '../../../config/app_config.dart'; class SettingsLearningView extends StatelessWidget { final SettingsLearningController controller; @@ -17,97 +14,75 @@ class SettingsLearningView extends StatelessWidget { @override Widget build(BuildContext context) { - // rebuild this page each time a sync comes through with new account data - // this prevents having to call setState each time an individual setting is changed - return StreamBuilder( - stream: - controller.pangeaController.matrixState.client.onSync.stream.where( - (update) => update.accountData != null, + return Scaffold( + appBar: AppBar( + centerTitle: true, + title: Text( + L10n.of(context)!.learningSettings, + ), ), - builder: (context, snapshot) { - return Scaffold( - appBar: AppBar( - centerTitle: true, - title: Text( - L10n.of(context)!.learningSettings, - ), - ), - body: ListTileTheme( - iconColor: Theme.of(context).textTheme.bodyLarge!.color, - child: MaxWidthBody( - withScrolling: true, - child: Column( - children: [ - LanguageTile(controller), - CountryPickerTile(controller), - const SizedBox(height: 8), - const Divider(height: 1), - const SizedBox(height: 8), - if (controller.pangeaController.permissionsController - .isUser18()) - SwitchListTile.adaptive( - activeColor: AppConfig.activeToggleColor, - title: Text(L10n.of(context)!.publicProfileTitle), - subtitle: Text(L10n.of(context)!.publicProfileDesc), - value: - controller.pangeaController.userController.isPublic, - onChanged: (bool isPublicProfile) => - showFutureLoadingDialog( - context: context, - future: () => - controller.setPublicProfile(isPublicProfile), - onError: (err) => ErrorHandler.logError( - e: err, - s: StackTrace.current, - ), - ), - ), - ListTile( - subtitle: - Text(L10n.of(context)!.toggleToolSettingsDescription), - ), - for (final toolSetting in ToolSetting.values) - ProfileSettingsSwitchListTile.adaptive( - defaultValue: controller.getToolSetting(toolSetting), - title: toolSetting.toolName(context), - subtitle: toolSetting.toolDescription(context), - onChange: (bool value) => controller.updateToolSetting( - toolSetting, - value, - ), - ), - ProfileSettingsSwitchListTile.adaptive( - defaultValue: controller.pangeaController.userController - .profile.userSettings.itAutoPlay, - title: L10n.of(context)! - .interactiveTranslatorAutoPlaySliderHeader, - subtitle: - L10n.of(context)!.interactiveTranslatorAutoPlayDesc, - onChange: (bool value) => controller - .pangeaController.userController - .updateProfile((profile) { - profile.userSettings.itAutoPlay = value; - return profile; - }), - ), - ProfileSettingsSwitchListTile.adaptive( - defaultValue: controller.pangeaController.userController - .profile.userSettings.autoPlayMessages, - title: L10n.of(context)!.autoPlayTitle, - subtitle: L10n.of(context)!.autoPlayDesc, - onChange: (bool value) => controller - .pangeaController.userController - .updateProfile((profile) { - profile.userSettings.autoPlayMessages = value; - return profile; - }), + body: ListTileTheme( + iconColor: Theme.of(context).textTheme.bodyLarge!.color, + child: MaxWidthBody( + withScrolling: true, + child: Column( + children: [ + LanguageTile(controller), + CountryPickerTile(controller), + const SizedBox(height: 8), + const Divider(height: 1), + const SizedBox(height: 8), + if (controller.pangeaController.permissionsController.isUser18()) + SwitchListTile.adaptive( + activeColor: AppConfig.activeToggleColor, + title: Text(L10n.of(context)!.publicProfileTitle), + subtitle: Text(L10n.of(context)!.publicProfileDesc), + value: controller.pangeaController.userController.isPublic, + onChanged: (bool isPublicProfile) => + controller.setPublicProfile(isPublicProfile), + ), + ListTile( + subtitle: Text(L10n.of(context)!.toggleToolSettingsDescription), + ), + for (final toolSetting in ToolSetting.values) + ProfileSettingsSwitchListTile.adaptive( + defaultValue: controller.getToolSetting(toolSetting), + title: toolSetting.toolName(context), + subtitle: toolSetting.toolDescription(context), + onChange: (bool value) => controller.updateToolSetting( + toolSetting, + value, ), - ], + ), + ProfileSettingsSwitchListTile.adaptive( + defaultValue: controller.pangeaController.userController.profile + .userSettings.itAutoPlay, + title: + L10n.of(context)!.interactiveTranslatorAutoPlaySliderHeader, + subtitle: L10n.of(context)!.interactiveTranslatorAutoPlayDesc, + onChange: (bool value) => controller + .pangeaController.userController + .updateProfile((profile) { + profile.userSettings.itAutoPlay = value; + return profile; + }), + ), + ProfileSettingsSwitchListTile.adaptive( + defaultValue: controller.pangeaController.userController.profile + .userSettings.autoPlayMessages, + title: L10n.of(context)!.autoPlayTitle, + subtitle: L10n.of(context)!.autoPlayDesc, + onChange: (bool value) => controller + .pangeaController.userController + .updateProfile((profile) { + profile.userSettings.autoPlayMessages = value; + return profile; + }), ), - ), + ], ), - ); - }, + ), + ), ); } } diff --git a/lib/pangea/utils/instructions.dart b/lib/pangea/utils/instructions.dart index 948d6a1e4..165bfa460 100644 --- a/lib/pangea/utils/instructions.dart +++ b/lib/pangea/utils/instructions.dart @@ -1,3 +1,4 @@ +import 'package:collection/collection.dart'; import 'package:fluffychat/pangea/enum/instructions_enum.dart'; import 'package:fluffychat/pangea/utils/inline_tooltip.dart'; import 'package:flutter/material.dart'; @@ -24,8 +25,9 @@ class InstructionsController { final Map _instructionsShown = {}; /// Returns true if the user requested this popup not be shown again - bool? toggledOff(String key) => - _pangeaController.pStoreService.read(key.toString()); + bool? toggledOff(String key) => InstructionsEnum.values + .firstWhereOrNull((value) => value.toString() == key) + ?.toggledOff; InstructionsController(PangeaController pangeaController) { _pangeaController = pangeaController; @@ -33,19 +35,32 @@ class InstructionsController { /// Returns true if the instructions were closed /// or turned off by the user via the toggle switch - bool wereInstructionsTurnedOff(String key) => - toggledOff(key) ?? _instructionsClosed[key] ?? false; + bool wereInstructionsTurnedOff(String key) { + return toggledOff(key) ?? _instructionsClosed[key] ?? false; + } void turnOffInstruction(String key) => _instructionsClosed[key] = true; - Future updateEnableInstructions( + void updateEnableInstructions( String key, bool value, - ) async => - await _pangeaController.pStoreService.save( - key, - value, - ); + ) { + _pangeaController.userController.updateProfile((profile) { + if (key == InstructionsEnum.itInstructions.toString()) { + profile.instructionSettings.showedItInstructions = value; + } + if (key == InstructionsEnum.clickMessage.toString()) { + profile.instructionSettings.showedClickMessage = value; + } + if (key == InstructionsEnum.blurMeansTranslate.toString()) { + profile.instructionSettings.showedBlurMeansTranslate = value; + } + if (key == InstructionsEnum.tooltipInstructions.toString()) { + profile.instructionSettings.showedTooltipInstructions = value; + } + return profile; + }); + } /// Instruction Card gives users tips on /// how to use Pangea Chat's features @@ -170,7 +185,7 @@ class InstructionsToggleState extends State { widget.instructionsKey.toString(), ), onChanged: ((value) async { - await pangeaController.instructions.updateEnableInstructions( + pangeaController.instructions.updateEnableInstructions( widget.instructionsKey.toString(), value, ); diff --git a/lib/pangea/widgets/user_settings/country_picker_tile.dart b/lib/pangea/widgets/user_settings/country_picker_tile.dart index df292a837..c4441573d 100644 --- a/lib/pangea/widgets/user_settings/country_picker_tile.dart +++ b/lib/pangea/widgets/user_settings/country_picker_tile.dart @@ -1,14 +1,10 @@ -import 'dart:developer'; - import 'package:country_picker/country_picker.dart'; import 'package:fluffychat/pangea/controllers/pangea_controller.dart'; import 'package:fluffychat/pangea/pages/settings_learning/settings_learning.dart'; import 'package:fluffychat/pangea/utils/country_display.dart'; import 'package:fluffychat/widgets/matrix.dart'; -import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; -import 'package:future_loading_dialog/future_loading_dialog.dart'; import '../../models/user_model.dart'; @@ -21,30 +17,26 @@ class CountryPickerTile extends StatelessWidget { @override Widget build(BuildContext context) { final Profile profile = pangeaController.userController.profile; + + final String displayName = CountryDisplayUtil.countryDisplayName( + profile.userSettings.country, + context, + ) ?? + ''; + + final String flag = CountryDisplayUtil.flagEmoji( + profile.userSettings.country, + ); + return ListTile( title: Text( - "${L10n.of(context)!.countryInformation}: ${CountryDisplayUtil.countryDisplayName( - profile.userSettings.country, - context, - ) ?? ''} ${CountryDisplayUtil.flagEmoji(profile.userSettings.country)}", + "${L10n.of(context)!.countryInformation}: $displayName $flag", ), trailing: const Icon(Icons.edit_outlined), onTap: () => showCountryPicker( context: context, - showPhoneCode: - false, // optional. Shows phone code before the country name. - onSelect: (Country country) async { - showFutureLoadingDialog( - context: context, - future: () async { - try { - learningController.changeCountry(country); - } catch (err) { - debugger(when: kDebugMode); - } - }, - ); - }, + showPhoneCode: false, + onSelect: learningController.changeCountry, ), ); } diff --git a/lib/pangea/widgets/user_settings/p_language_dialog.dart b/lib/pangea/widgets/user_settings/p_language_dialog.dart index 8635170a0..bfc85528b 100644 --- a/lib/pangea/widgets/user_settings/p_language_dialog.dart +++ b/lib/pangea/widgets/user_settings/p_language_dialog.dart @@ -15,7 +15,10 @@ import '../../../widgets/matrix.dart'; import 'p_language_dropdown.dart'; import 'p_question_container.dart'; -pLanguageDialog(BuildContext parentContext, Function callback) async { +Future pLanguageDialog( + BuildContext parentContext, + Function callback, +) async { final PangeaController pangeaController = MatrixState.pangeaController; //PTODO: if source language not set by user, default to languge from device settings final LanguageModel? userL1 = pangeaController.languageController.userL1;