|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:io'; |
| 3 | + |
| 4 | +import 'package:badgemagic/bademagic_module/utils/byte_array_utils.dart'; |
| 5 | +import 'package:path_provider/path_provider.dart'; |
| 6 | + |
| 7 | +/// A utility class to store and retrieve the original text of badges |
| 8 | +class BadgeTextStorage { |
| 9 | + static const String TEXT_STORAGE_FILENAME = 'badge_original_texts.json'; |
| 10 | + |
| 11 | + /// Save the original text for a badge |
| 12 | + static Future<void> saveOriginalText( |
| 13 | + String badgeFilename, String originalText) async { |
| 14 | + try { |
| 15 | + // Get the existing text storage or create a new one |
| 16 | + Map<String, String> textStorage = await _getTextStorage(); |
| 17 | + |
| 18 | + // Store the original text with the badge filename as the key |
| 19 | + textStorage[badgeFilename] = originalText; |
| 20 | + |
| 21 | + // Save the updated storage |
| 22 | + await _saveTextStorage(textStorage); |
| 23 | + |
| 24 | + logger.d('Saved original text for badge: $badgeFilename'); |
| 25 | + } catch (e) { |
| 26 | + logger.e('Error saving original text: $e'); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /// Get the original text for a badge |
| 31 | + static Future<String> getOriginalText(String badgeFilename) async { |
| 32 | + try { |
| 33 | + // Get the existing text storage |
| 34 | + Map<String, String> textStorage = await _getTextStorage(); |
| 35 | + |
| 36 | + // Return the original text if it exists, otherwise return empty string |
| 37 | + return textStorage[badgeFilename] ?? ''; |
| 38 | + } catch (e) { |
| 39 | + logger.e('Error getting original text: $e'); |
| 40 | + return ''; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /// Move the original text mapping from oldFilename to newFilename |
| 45 | + static Future<void> moveOriginalText( |
| 46 | + String oldFilename, String newFilename) async { |
| 47 | + try { |
| 48 | + Map<String, String> textStorage = await _getTextStorage(); |
| 49 | + if (textStorage.containsKey(oldFilename)) { |
| 50 | + textStorage[newFilename] = textStorage[oldFilename]!; |
| 51 | + textStorage.remove(oldFilename); |
| 52 | + await _saveTextStorage(textStorage); |
| 53 | + logger.d('Moved original text from: $oldFilename to $newFilename'); |
| 54 | + } |
| 55 | + } catch (e) { |
| 56 | + logger.e('Error moving original text: $e'); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /// Delete the original text for a badge |
| 61 | + static Future<void> deleteOriginalText(String badgeFilename) async { |
| 62 | + try { |
| 63 | + // Get the existing text storage |
| 64 | + Map<String, String> textStorage = await _getTextStorage(); |
| 65 | + |
| 66 | + // Remove the entry for the badge |
| 67 | + textStorage.remove(badgeFilename); |
| 68 | + |
| 69 | + // Save the updated storage |
| 70 | + await _saveTextStorage(textStorage); |
| 71 | + |
| 72 | + logger.d('Deleted original text for badge: $badgeFilename'); |
| 73 | + } catch (e) { |
| 74 | + logger.e('Error deleting original text: $e'); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /// Get the text storage file |
| 79 | + static Future<Map<String, String>> _getTextStorage() async { |
| 80 | + try { |
| 81 | + final directory = await getApplicationDocumentsDirectory(); |
| 82 | + final file = File('${directory.path}/$TEXT_STORAGE_FILENAME'); |
| 83 | + |
| 84 | + // Create the file if it doesn't exist |
| 85 | + if (!await file.exists()) { |
| 86 | + await file.create(); |
| 87 | + await file.writeAsString('{}'); |
| 88 | + return {}; |
| 89 | + } |
| 90 | + |
| 91 | + // Read the file and parse the JSON |
| 92 | + final jsonString = await file.readAsString(); |
| 93 | + if (jsonString.isEmpty) { |
| 94 | + return {}; |
| 95 | + } |
| 96 | + |
| 97 | + final Map<String, dynamic> jsonData = jsonDecode(jsonString); |
| 98 | + |
| 99 | + // Convert dynamic values to String |
| 100 | + final Map<String, String> textStorage = {}; |
| 101 | + jsonData.forEach((key, value) { |
| 102 | + textStorage[key] = value.toString(); |
| 103 | + }); |
| 104 | + |
| 105 | + return textStorage; |
| 106 | + } catch (e) { |
| 107 | + logger.e('Error getting text storage: $e'); |
| 108 | + return {}; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /// Save the text storage to file |
| 113 | + static Future<void> _saveTextStorage(Map<String, String> textStorage) async { |
| 114 | + try { |
| 115 | + final directory = await getApplicationDocumentsDirectory(); |
| 116 | + final file = File('${directory.path}/$TEXT_STORAGE_FILENAME'); |
| 117 | + |
| 118 | + // Convert the map to JSON and save it |
| 119 | + final jsonString = jsonEncode(textStorage); |
| 120 | + await file.writeAsString(jsonString); |
| 121 | + } catch (e) { |
| 122 | + logger.e('Error saving text storage: $e'); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments