From f1ba5230fc01ff7ed801023ca83d8d4c60c2142d Mon Sep 17 00:00:00 2001 From: Kima Date: Thu, 10 Oct 2024 18:11:41 +0200 Subject: [PATCH] added theme share error handling for ratelimit response --- refilc/lib/api/client.dart | 12 ++++++++---- .../lib/providers/share_provider.dart | 10 +++++++--- .../settings/submenu/share_theme_popup.dart | 15 ++++++++++++++- .../settings/submenu/submenu_screen.i18n.dart | 6 ++++++ 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/refilc/lib/api/client.dart b/refilc/lib/api/client.dart index db1ae6f..9c76de8 100644 --- a/refilc/lib/api/client.dart +++ b/refilc/lib/api/client.dart @@ -235,7 +235,7 @@ class FilcAPI { } // sharing - static Future addSharedTheme(SharedTheme theme) async { + static Future addSharedTheme(SharedTheme theme) async { try { theme.json.remove('json'); theme.json['is_public'] = theme.isPublic.toString(); @@ -267,13 +267,17 @@ 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}"; + // } log('Shared theme successfully with ID: ${theme.id}'); + + return res.statusCode; } on Exception catch (error, stacktrace) { log("ERROR: FilcAPI.addSharedTheme: $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 1df105c..5e01fb0 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 shareCurrentTheme( BuildContext context, { bool isPublic = false, bool shareNick = true, @@ -56,9 +56,13 @@ class ShareProvider extends ChangeNotifier { }; SharedTheme theme = SharedTheme.fromJson(themeJson, gradeColors); - FilcAPI.addSharedTheme(theme); + int shareResult = await FilcAPI.addSharedTheme(theme); - return theme; + if (shareResult == 200) { + return theme; + } else { + return null; + } } Future getThemeById(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 92da234..abe3e10 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 @@ -5,8 +5,10 @@ import 'package:flutter_feather_icons/flutter_feather_icons.dart'; import 'package:provider/provider.dart'; // import 'package:refilc/models/settings.dart'; import 'package:refilc/models/shared_theme.dart'; +import 'package:refilc/theme/colors/colors.dart'; import 'package:refilc_kreta_api/providers/share_provider.dart'; import 'package:refilc_mobile_ui/common/action_button.dart'; +import 'package:refilc_mobile_ui/common/custom_snack_bar.dart'; import 'package:refilc_mobile_ui/common/splitted_panel/splitted_panel.dart'; import 'package:share_plus/share_plus.dart'; import 'submenu_screen.i18n.dart'; @@ -133,13 +135,24 @@ class ShareThemeDialogState extends State { // share the fucking theme SharedGradeColors gradeColors = await shareProvider.shareCurrentGradeColors(context); - SharedTheme theme = await shareProvider.shareCurrentTheme( + SharedTheme? theme = await shareProvider.shareCurrentTheme( context, gradeColors: gradeColors, isPublic: isPublic, displayName: _title.text, ); + if (theme == null) { + 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; + } + // save theme id in settings // Provider.of(context, listen: false) // .update(currentThemeId: theme.id); 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 ce76ee7..d90bb5a 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,6 +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.", }, "hu_hu": { "general": "Általános", @@ -58,6 +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.", }, "de_de": { "general": "Allgemeine", @@ -86,6 +90,8 @@ extension SettingsLocalization on String { "share_disclaimer": "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.", }, };