From 939761695fdab5f8c6cbaae8e5321c511b2a6135 Mon Sep 17 00:00:00 2001 From: Kima Date: Thu, 10 Oct 2024 20:48:21 +0200 Subject: [PATCH] working error handling for theme sharing --- refilc/lib/api/client.dart | 19 ++++--- .../lib/providers/share_provider.dart | 18 ++++--- .../settings/submenu/share_theme_popup.dart | 51 +++++++++++++++---- .../settings/submenu/submenu_screen.i18n.dart | 11 ++-- 4 files changed, 72 insertions(+), 27 deletions(-) diff --git a/refilc/lib/api/client.dart b/refilc/lib/api/client.dart index 9c76de8..ed02aa1 100644 --- a/refilc/lib/api/client.dart +++ b/refilc/lib/api/client.dart @@ -271,7 +271,9 @@ class FilcAPI { // throw "HTTP ${res.statusCode}: ${res.body}"; // } - log('Shared theme successfully with ID: ${theme.id}'); + if (res.statusCode == 201) { + log('Shared theme successfully with ID: ${theme.id}'); + } return res.statusCode; } on Exception catch (error, stacktrace) { @@ -311,8 +313,7 @@ class FilcAPI { return null; } - static Future addSharedGradeColors( - SharedGradeColors gradeColors) async { + static Future addSharedGradeColors(SharedGradeColors gradeColors) async { try { gradeColors.json.remove('json'); gradeColors.json['is_public'] = gradeColors.isPublic.toString(); @@ -328,13 +329,19 @@ class FilcAPI { headers: {'Content-Type': 'application/x-www-form-urlencoded'}, ); - if (res.statusCode != 201) { - throw "HTTP ${res.statusCode}: ${res.body}"; + // if (res.statusCode != 201) { + // throw "HTTP ${res.statusCode}: ${res.body}"; + // } + + if (res.statusCode == 201) { + log('Shared grade colors successfully with ID: ${gradeColors.id}'); } - log('Shared grade colors successfully with ID: ${gradeColors.id}'); + return res.statusCode; } on Exception catch (error, stacktrace) { log("ERROR: FilcAPI.addSharedGradeColors: $error $stacktrace"); + + return 696; } } diff --git a/refilc_kreta_api/lib/providers/share_provider.dart b/refilc_kreta_api/lib/providers/share_provider.dart index 5e01fb0..c45c228 100644 --- a/refilc_kreta_api/lib/providers/share_provider.dart +++ b/refilc_kreta_api/lib/providers/share_provider.dart @@ -19,7 +19,7 @@ class ShareProvider extends ChangeNotifier { // } // themes - Future shareCurrentTheme( + Future<(SharedTheme?, int)> shareCurrentTheme( BuildContext context, { bool isPublic = false, bool shareNick = true, @@ -58,10 +58,10 @@ class ShareProvider extends ChangeNotifier { SharedTheme theme = SharedTheme.fromJson(themeJson, gradeColors); int shareResult = await FilcAPI.addSharedTheme(theme); - if (shareResult == 200) { - return theme; + if (shareResult == 201) { + return (theme, 201); } else { - return null; + return (null, shareResult); } } @@ -146,7 +146,7 @@ class ShareProvider extends ChangeNotifier { } // grade colors - Future shareCurrentGradeColors( + Future<(SharedGradeColors?, int)> shareCurrentGradeColors( BuildContext context, { bool isPublic = false, bool shareNick = true, @@ -166,9 +166,13 @@ class ShareProvider extends ChangeNotifier { }; SharedGradeColors gradeColors = SharedGradeColors.fromJson(gradeColorsJson); - FilcAPI.addSharedGradeColors(gradeColors); + int shareResult = await FilcAPI.addSharedGradeColors(gradeColors); - return gradeColors; + if (shareResult == 201) { + return (gradeColors, 201); + } else { + return (null, shareResult); + } } Future getGradeColorsById(BuildContext context, diff --git a/refilc_mobile_ui/lib/screens/settings/submenu/share_theme_popup.dart b/refilc_mobile_ui/lib/screens/settings/submenu/share_theme_popup.dart index abe3e10..9c6b50b 100644 --- a/refilc_mobile_ui/lib/screens/settings/submenu/share_theme_popup.dart +++ b/refilc_mobile_ui/lib/screens/settings/submenu/share_theme_popup.dart @@ -133,16 +133,19 @@ class ShareThemeDialogState extends State { ), onPressed: () async { // share the fucking theme - SharedGradeColors gradeColors = + var (gradeColors, gradeColorsStatus) = await shareProvider.shareCurrentGradeColors(context); - SharedTheme? theme = await shareProvider.shareCurrentTheme( - context, - gradeColors: gradeColors, - isPublic: isPublic, - displayName: _title.text, - ); - if (theme == null) { + if (gradeColorsStatus == 429) { + ScaffoldMessenger.of(context).showSnackBar(CustomSnackBar( + content: Text("theme_share_ratelimit".i18n, + style: const TextStyle(color: Colors.white)), + backgroundColor: AppColors.of(context).red, + context: context, + )); + + return; + } else if (gradeColorsStatus != 201) { ScaffoldMessenger.of(context).showSnackBar(CustomSnackBar( content: Text("theme_share_failed".i18n, style: const TextStyle(color: Colors.white)), @@ -153,6 +156,36 @@ class ShareThemeDialogState extends State { return; } + var (theme, themeStatus) = await shareProvider.shareCurrentTheme( + context, + gradeColors: gradeColors!, + isPublic: isPublic, + displayName: _title.text, + ); + + if (themeStatus == 429) { + ScaffoldMessenger.of(context).showSnackBar(CustomSnackBar( + content: Text("theme_share_ratelimit".i18n, + style: const TextStyle(color: Colors.white)), + backgroundColor: AppColors.of(context).red, + context: context, + )); + + return; + } else if (themeStatus != 201) { + ScaffoldMessenger.of(context).showSnackBar(CustomSnackBar( + content: Text("theme_share_failed".i18n, + style: const TextStyle(color: Colors.white)), + backgroundColor: AppColors.of(context).red, + context: context, + )); + + return; + } + + print(theme); + print(themeStatus); + // save theme id in settings // Provider.of(context, listen: false) // .update(currentThemeId: theme.id); @@ -162,7 +195,7 @@ class ShareThemeDialogState extends State { // show the share popup Share.share( - theme.id, + theme!.id, subject: 'share_subj_theme'.i18n, ); }, diff --git a/refilc_mobile_ui/lib/screens/settings/submenu/submenu_screen.i18n.dart b/refilc_mobile_ui/lib/screens/settings/submenu/submenu_screen.i18n.dart index d90bb5a..35c63b1 100644 --- a/refilc_mobile_ui/lib/screens/settings/submenu/submenu_screen.i18n.dart +++ b/refilc_mobile_ui/lib/screens/settings/submenu/submenu_screen.i18n.dart @@ -30,8 +30,8 @@ extension SettingsLocalization on String { "share_disclaimer": "By sharing the theme, you agree that the nickname you set and all settings of the theme will be shared publicly.", "understand": "I understand", - "theme_share_failed": - "An error occurred while sharing the theme. Wait 1 minute and try again.", + "theme_share_failed": "An error occurred while sharing the theme.", + "theme_share_ratelimit": "You can only share 1 theme per minute.", }, "hu_hu": { "general": "Általános", @@ -60,8 +60,8 @@ extension SettingsLocalization on String { "share_disclaimer": "A téma megosztásával elfogadod, hogy az általad beállított becenév és a téma minden beállítása nyilvánosan megosztásra kerüljön.", "understand": "Értem", - "theme_share_failed": - "Hiba történt a téma megosztása közben. Várj 1 percet, majd próbáld újra.", + "theme_share_failed": "Hiba történt a téma megosztása közben.", + "theme_share_ratelimit": "Csak 1 témát oszthatsz meg percenként.", }, "de_de": { "general": "Allgemeine", @@ -91,7 +91,8 @@ extension SettingsLocalization on String { "Durch das Teilen des Themes erklären Sie sich damit einverstanden, dass der von Ihnen festgelegte Spitzname und alle Einstellungen des Themes öffentlich geteilt werden.", "understand": "Ich verstehe", "theme_share_failed": - "Beim Teilen des Themas ist ein Fehler aufgetreten. Warten Sie 1 Minute und versuchen Sie es erneut.", + "Beim Teilen des Themas ist ein Fehler aufgetreten.", + "theme_share_ratelimit": "Sie können nur 1 Thema pro Minute teilen.", }, };