diff --git a/filcnaplo/lib/api/client.dart b/filcnaplo/lib/api/client.dart index 0a78545..36ef6e9 100644 --- a/filcnaplo/lib/api/client.dart +++ b/filcnaplo/lib/api/client.dart @@ -40,6 +40,11 @@ class FilcAPI { static const allThemes = "$themeGet/all"; static const themeByID = "$themeGet/"; + static const gradeColorsShare = "$baseUrl/v2/shared/theme/add"; + static const gradeColorsGet = "$baseUrl/v2/shared/theme/get"; + static const allGradeColors = "$gradeColorsGet/all"; + static const gradeColorsByID = "$gradeColorsGet/"; + static Future checkConnectivity() async => (await Connectivity().checkConnectivity()) != ConnectivityResult.none; @@ -235,7 +240,49 @@ class FilcAPI { throw "HTTP ${res.statusCode}: ${res.body}"; } } on Exception catch (error, stacktrace) { - log("ERROR: FilcAPI.addSharedTheme: $error $stacktrace"); + log("ERROR: FilcAPI.getSharedTheme: $error $stacktrace"); + } + return null; + } + + static Future addSharedGradeColors( + SharedGradeColors gradeColors) async { + try { + gradeColors.json.remove('json'); + gradeColors.json['is_public'] = gradeColors.isPublic.toString(); + gradeColors.json['five_color'] = gradeColors.fiveColor.value.toString(); + gradeColors.json['four_color'] = gradeColors.fourColor.value.toString(); + gradeColors.json['three_color'] = gradeColors.threeColor.value.toString(); + gradeColors.json['two_color'] = gradeColors.twoColor.value.toString(); + gradeColors.json['one_color'] = gradeColors.oneColor.value.toString(); + + http.Response res = await http.post( + Uri.parse(gradeColorsShare), + body: gradeColors.json, + headers: {'Content-Type': 'application/x-www-form-urlencoded'}, + ); + + if (res.statusCode != 201) { + throw "HTTP ${res.statusCode}: ${res.body}"; + } + + log('Shared grade colors successfully with ID: ${gradeColors.id}'); + } on Exception catch (error, stacktrace) { + log("ERROR: FilcAPI.addSharedGradeColors: $error $stacktrace"); + } + } + + static Future getSharedGradeColors(String id) async { + try { + http.Response res = await http.get(Uri.parse(gradeColorsByID + id)); + + if (res.statusCode == 200) { + return (jsonDecode(res.body) as Map); + } else { + throw "HTTP ${res.statusCode}: ${res.body}"; + } + } on Exception catch (error, stacktrace) { + log("ERROR: FilcAPI.getSharedGradeColors: $error $stacktrace"); } return null; } diff --git a/filcnaplo/lib/models/shared_theme.dart b/filcnaplo/lib/models/shared_theme.dart index 5c3486c..0784fcc 100644 --- a/filcnaplo/lib/models/shared_theme.dart +++ b/filcnaplo/lib/models/shared_theme.dart @@ -8,6 +8,7 @@ class SharedTheme { Color backgroundColor; Color panelsColor; Color accentColor; + SharedGradeColors gradeColors; SharedTheme({ required this.json, @@ -17,9 +18,10 @@ class SharedTheme { required this.backgroundColor, required this.panelsColor, required this.accentColor, + required this.gradeColors, }); - factory SharedTheme.fromJson(Map json) { + factory SharedTheme.fromJson(Map json, Map gradeColorsJson) { return SharedTheme( json: json, id: json['public_id'], @@ -28,6 +30,48 @@ class SharedTheme { backgroundColor: Color(json['background_color']), panelsColor: Color(json['panels_color']), accentColor: Color(json['accent_color']), + gradeColors: SharedGradeColors.fromJson(gradeColorsJson), + ); + } +} + +class SharedGradeColors { + Map json; + String id; + bool isPublic; + String nickname; + Color fiveColor; + Color fourColor; + Color threeColor; + Color twoColor; + Color oneColor; + String linkedThemeId; + + SharedGradeColors({ + required this.json, + required this.id, + this.isPublic = false, + this.nickname = 'Anonymous', + required this.fiveColor, + required this.fourColor, + required this.threeColor, + required this.twoColor, + required this.oneColor, + required this.linkedThemeId, + }); + + factory SharedGradeColors.fromJson(Map json) { + return SharedGradeColors( + json: json, + id: json['public_id'], + isPublic: json['is_public'] ?? false, + nickname: json['nickname'] ?? 'Anonymous', + fiveColor: Color(json['five_color']), + fourColor: Color(json['four_color']), + threeColor: Color(json['three_color']), + twoColor: Color(json['two_color']), + oneColor: Color(json['one_color']), + linkedThemeId: json['linked_theme_id'], ); } } diff --git a/filcnaplo_mobile_ui/lib/screens/settings/settings_helper.dart b/filcnaplo_mobile_ui/lib/screens/settings/settings_helper.dart index 44b1334..e4c459f 100755 --- a/filcnaplo_mobile_ui/lib/screens/settings/settings_helper.dart +++ b/filcnaplo_mobile_ui/lib/screens/settings/settings_helper.dart @@ -623,7 +623,7 @@ class LiveActivityColorSetting extends StatefulWidget { _LiveActivityColorSettingState(); } -class _LiveActivityColorSettingState extends State { +class _LiveActivityColorSettingState extends State { late SettingsProvider settings; Color currentColor = const Color(0x00000000); @@ -648,8 +648,8 @@ class _LiveActivityColorSettingState extends State { setState(() { currentColor = k as Color; settings.update( - liveActivityColor: currentColor.withAlpha(255)); - Navigator.of(context).maybePop(); + liveActivityColor: currentColor.withAlpha(255)); + Navigator.of(context).maybePop(); }); }, elevation: 0, diff --git a/filcnaplo_premium/lib/providers/share_provider.dart b/filcnaplo_premium/lib/providers/share_provider.dart index 8407e43..1fbf2c2 100644 --- a/filcnaplo_premium/lib/providers/share_provider.dart +++ b/filcnaplo_premium/lib/providers/share_provider.dart @@ -17,8 +17,12 @@ class ShareProvider extends ChangeNotifier { // Future shareTheme({required SharedTheme theme}) async { // } + + // themes Future shareCurrentTheme(BuildContext context, - {bool isPublic = false, bool shareNick = true}) async { + {bool isPublic = false, + bool shareNick = true, + required SharedGradeColors gradeColors}) async { final SettingsProvider settings = Provider.of(context, listen: false); @@ -38,7 +42,7 @@ class ShareProvider extends ChangeNotifier { const Color(0xFF3D7BF4).value, }; - SharedTheme theme = SharedTheme.fromJson(themeJson); + SharedTheme theme = SharedTheme.fromJson(themeJson, gradeColors.json); FilcAPI.addSharedTheme(theme); return theme; @@ -49,8 +53,52 @@ class ShareProvider extends ChangeNotifier { Map? themeJson = await FilcAPI.getSharedTheme(id); if (themeJson != null) { - SharedTheme theme = SharedTheme.fromJson(themeJson); - return theme; + Map? gradeColorsJson = + await FilcAPI.getSharedGradeColors(themeJson['grade_colors_id']); + + if (gradeColorsJson != null) { + SharedTheme theme = SharedTheme.fromJson(themeJson, gradeColorsJson); + return theme; + } + } + + return null; + } + + // grade colors + Future shareCurrentGradeColors( + BuildContext context, { + bool isPublic = false, + bool shareNick = true, + }) async { + final SettingsProvider settings = + Provider.of(context, listen: false); + + Map gradeColorsJson = { + 'public_id': const Uuid().v4(), + 'is_public': isPublic, + 'nickname': shareNick ? _user.nickname : 'Anonymous', + 'five_color': settings.gradeColors[4].value, + 'four_color': settings.gradeColors[3].value, + 'three_color': settings.gradeColors[2].value, + 'two_color': settings.gradeColors[1].value, + 'one_color': settings.gradeColors[0].value, + }; + + SharedGradeColors gradeColors = SharedGradeColors.fromJson(gradeColorsJson); + FilcAPI.addSharedGradeColors(gradeColors); + + return gradeColors; + } + + Future getGradeColorsById(BuildContext context, + {required String id}) async { + Map? gradeColorsJson = await FilcAPI.getSharedGradeColors(id); + + if (gradeColorsJson != null) { + SharedGradeColors gradeColors = + SharedGradeColors.fromJson(gradeColorsJson); + return gradeColors; } return null; diff --git a/filcnaplo_premium/lib/ui/mobile/settings/theme.dart b/filcnaplo_premium/lib/ui/mobile/settings/theme.dart index bcd0468..cbe4d40 100644 --- a/filcnaplo_premium/lib/ui/mobile/settings/theme.dart +++ b/filcnaplo_premium/lib/ui/mobile/settings/theme.dart @@ -283,8 +283,16 @@ class _PremiumCustomAccentColorSettingState label: "understand".i18n, onTap: () async { Navigator.of(context).pop(); - SharedTheme theme = await shareProvider - .shareCurrentTheme(context); + + SharedGradeColors gradeColors = + await shareProvider + .shareCurrentGradeColors(context); + SharedTheme theme = + await shareProvider.shareCurrentTheme( + context, + gradeColors: gradeColors, + ); + Share.share( theme.id, subject: 'share_subj_theme'.i18n, @@ -804,6 +812,18 @@ class _PremiumCustomAccentColorSettingState setTheme(settings.theme, true); }, onThemeIdProvided: (theme) { + // changing grade colors + List colors = [ + theme.gradeColors.oneColor, + theme.gradeColors.twoColor, + theme.gradeColors.threeColor, + theme.gradeColors.fourColor, + theme.gradeColors.fiveColor, + ]; + settings.update( + gradeColors: colors); + + // changing theme setState(() { updateCustomColor( null,