working error handling for theme sharing

This commit is contained in:
Kima 2024-10-10 20:48:21 +02:00
parent f1ba5230fc
commit 939761695f
4 changed files with 72 additions and 27 deletions

View File

@ -271,7 +271,9 @@ class FilcAPI {
// throw "HTTP ${res.statusCode}: ${res.body}"; // 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; return res.statusCode;
} on Exception catch (error, stacktrace) { } on Exception catch (error, stacktrace) {
@ -311,8 +313,7 @@ class FilcAPI {
return null; return null;
} }
static Future<void> addSharedGradeColors( static Future<int> addSharedGradeColors(SharedGradeColors gradeColors) async {
SharedGradeColors gradeColors) async {
try { try {
gradeColors.json.remove('json'); gradeColors.json.remove('json');
gradeColors.json['is_public'] = gradeColors.isPublic.toString(); gradeColors.json['is_public'] = gradeColors.isPublic.toString();
@ -328,13 +329,19 @@ class FilcAPI {
headers: {'Content-Type': 'application/x-www-form-urlencoded'}, headers: {'Content-Type': 'application/x-www-form-urlencoded'},
); );
if (res.statusCode != 201) { // if (res.statusCode != 201) {
throw "HTTP ${res.statusCode}: ${res.body}"; // 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) { } on Exception catch (error, stacktrace) {
log("ERROR: FilcAPI.addSharedGradeColors: $error $stacktrace"); log("ERROR: FilcAPI.addSharedGradeColors: $error $stacktrace");
return 696;
} }
} }

View File

@ -19,7 +19,7 @@ class ShareProvider extends ChangeNotifier {
// } // }
// themes // themes
Future<SharedTheme?> shareCurrentTheme( Future<(SharedTheme?, int)> shareCurrentTheme(
BuildContext context, { BuildContext context, {
bool isPublic = false, bool isPublic = false,
bool shareNick = true, bool shareNick = true,
@ -58,10 +58,10 @@ class ShareProvider extends ChangeNotifier {
SharedTheme theme = SharedTheme.fromJson(themeJson, gradeColors); SharedTheme theme = SharedTheme.fromJson(themeJson, gradeColors);
int shareResult = await FilcAPI.addSharedTheme(theme); int shareResult = await FilcAPI.addSharedTheme(theme);
if (shareResult == 200) { if (shareResult == 201) {
return theme; return (theme, 201);
} else { } else {
return null; return (null, shareResult);
} }
} }
@ -146,7 +146,7 @@ class ShareProvider extends ChangeNotifier {
} }
// grade colors // grade colors
Future<SharedGradeColors> shareCurrentGradeColors( Future<(SharedGradeColors?, int)> shareCurrentGradeColors(
BuildContext context, { BuildContext context, {
bool isPublic = false, bool isPublic = false,
bool shareNick = true, bool shareNick = true,
@ -166,9 +166,13 @@ class ShareProvider extends ChangeNotifier {
}; };
SharedGradeColors gradeColors = SharedGradeColors.fromJson(gradeColorsJson); 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<SharedGradeColors?> getGradeColorsById(BuildContext context, Future<SharedGradeColors?> getGradeColorsById(BuildContext context,

View File

@ -133,16 +133,19 @@ class ShareThemeDialogState extends State<ShareThemeDialog> {
), ),
onPressed: () async { onPressed: () async {
// share the fucking theme // share the fucking theme
SharedGradeColors gradeColors = var (gradeColors, gradeColorsStatus) =
await shareProvider.shareCurrentGradeColors(context); 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( ScaffoldMessenger.of(context).showSnackBar(CustomSnackBar(
content: Text("theme_share_failed".i18n, content: Text("theme_share_failed".i18n,
style: const TextStyle(color: Colors.white)), style: const TextStyle(color: Colors.white)),
@ -153,6 +156,36 @@ class ShareThemeDialogState extends State<ShareThemeDialog> {
return; 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 // save theme id in settings
// Provider.of<SettingsProvider>(context, listen: false) // Provider.of<SettingsProvider>(context, listen: false)
// .update(currentThemeId: theme.id); // .update(currentThemeId: theme.id);
@ -162,7 +195,7 @@ class ShareThemeDialogState extends State<ShareThemeDialog> {
// show the share popup // show the share popup
Share.share( Share.share(
theme.id, theme!.id,
subject: 'share_subj_theme'.i18n, subject: 'share_subj_theme'.i18n,
); );
}, },

View File

@ -30,8 +30,8 @@ extension SettingsLocalization on String {
"share_disclaimer": "share_disclaimer":
"By sharing the theme, you agree that the nickname you set and all settings of the theme will be shared publicly.", "By sharing the theme, you agree that the nickname you set and all settings of the theme will be shared publicly.",
"understand": "I understand", "understand": "I understand",
"theme_share_failed": "theme_share_failed": "An error occurred while sharing the theme.",
"An error occurred while sharing the theme. Wait 1 minute and try again.", "theme_share_ratelimit": "You can only share 1 theme per minute.",
}, },
"hu_hu": { "hu_hu": {
"general": "Általános", "general": "Általános",
@ -60,8 +60,8 @@ extension SettingsLocalization on String {
"share_disclaimer": "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.", "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", "understand": "Értem",
"theme_share_failed": "theme_share_failed": "Hiba történt a téma megosztása közben.",
"Hiba történt a téma megosztása közben. Várj 1 percet, majd próbáld újra.", "theme_share_ratelimit": "Csak 1 témát oszthatsz meg percenként.",
}, },
"de_de": { "de_de": {
"general": "Allgemeine", "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.", "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", "understand": "Ich verstehe",
"theme_share_failed": "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.",
}, },
}; };