-
Notifications
You must be signed in to change notification settings - Fork 243
feat: added interactive games (fixes #1312) #1316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nope3472
wants to merge
10
commits into
fossasia:development
Choose a base branch
from
nope3472:feature-add-games
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3a2c414
Make saved badges editable
738f36c
Merge branch 'flutter_app' into recover-savedbadges
nope3472 99c7e3a
fixet the common buld error
e618804
Merge branch 'flutter_app' into recover-savedbadges
nope3472 91c1803
Add game providers and UI screens for Snake and Tetris games
5aa6a0b
fixed formatting
feca623
Tried adding live sync
a1eb467
Tried adding live sync
af8f8da
resolved conflict
adcfc83
resolved conflict
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:badgemagic/bademagic_module/utils/byte_array_utils.dart'; | ||
import 'package:path_provider/path_provider.dart'; | ||
|
||
/// A utility class to store and retrieve the original text of badges | ||
class BadgeTextStorage { | ||
static const String TEXT_STORAGE_FILENAME = 'badge_original_texts.json'; | ||
|
||
/// Save the original text for a badge | ||
static Future<void> saveOriginalText( | ||
String badgeFilename, String originalText) async { | ||
try { | ||
// Get the existing text storage or create a new one | ||
Map<String, String> textStorage = await _getTextStorage(); | ||
|
||
// Store the original text with the badge filename as the key | ||
textStorage[badgeFilename] = originalText; | ||
|
||
// Save the updated storage | ||
await _saveTextStorage(textStorage); | ||
|
||
logger.d('Saved original text for badge: $badgeFilename'); | ||
} catch (e) { | ||
logger.e('Error saving original text: $e'); | ||
} | ||
} | ||
|
||
/// Get the original text for a badge | ||
static Future<String> getOriginalText(String badgeFilename) async { | ||
try { | ||
// Get the existing text storage | ||
Map<String, String> textStorage = await _getTextStorage(); | ||
|
||
// Return the original text if it exists, otherwise return empty string | ||
return textStorage[badgeFilename] ?? ''; | ||
} catch (e) { | ||
logger.e('Error getting original text: $e'); | ||
return ''; | ||
} | ||
} | ||
|
||
/// Delete the original text for a badge | ||
static Future<void> deleteOriginalText(String badgeFilename) async { | ||
try { | ||
// Get the existing text storage | ||
Map<String, String> textStorage = await _getTextStorage(); | ||
|
||
// Remove the entry for the badge | ||
textStorage.remove(badgeFilename); | ||
|
||
// Save the updated storage | ||
await _saveTextStorage(textStorage); | ||
|
||
logger.d('Deleted original text for badge: $badgeFilename'); | ||
} catch (e) { | ||
logger.e('Error deleting original text: $e'); | ||
} | ||
} | ||
|
||
/// Get the text storage file | ||
static Future<Map<String, String>> _getTextStorage() async { | ||
try { | ||
final directory = await getApplicationDocumentsDirectory(); | ||
final file = File('${directory.path}/$TEXT_STORAGE_FILENAME'); | ||
|
||
// Create the file if it doesn't exist | ||
if (!await file.exists()) { | ||
await file.create(); | ||
await file.writeAsString('{}'); | ||
return {}; | ||
} | ||
|
||
// Read the file and parse the JSON | ||
final jsonString = await file.readAsString(); | ||
if (jsonString.isEmpty) { | ||
return {}; | ||
} | ||
|
||
final Map<String, dynamic> jsonData = jsonDecode(jsonString); | ||
|
||
// Convert dynamic values to String | ||
final Map<String, String> textStorage = {}; | ||
jsonData.forEach((key, value) { | ||
textStorage[key] = value.toString(); | ||
}); | ||
|
||
return textStorage; | ||
} catch (e) { | ||
logger.e('Error getting text storage: $e'); | ||
return {}; | ||
} | ||
} | ||
|
||
/// Save the text storage to file | ||
static Future<void> _saveTextStorage(Map<String, String> textStorage) async { | ||
try { | ||
final directory = await getApplicationDocumentsDirectory(); | ||
final file = File('${directory.path}/$TEXT_STORAGE_FILENAME'); | ||
|
||
// Convert the map to JSON and save it | ||
final jsonString = jsonEncode(textStorage); | ||
await file.writeAsString(jsonString); | ||
} catch (e) { | ||
logger.e('Error saving text storage: $e'); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -278,8 +278,22 @@ class FileHelper { | |
// Save JSON string to the file | ||
File file = File(filePath); | ||
await file.writeAsString(jsonString); | ||
imageCacheProvider.savedBadgeCache | ||
.add(MapEntry("$filename.json", jsonData)); | ||
|
||
// Update the cache properly - check if the badge already exists in the cache | ||
final cacheKey = "$filename.json"; | ||
final cache = imageCacheProvider.savedBadgeCache; | ||
final existingIndex = cache.indexWhere((entry) => entry.key == cacheKey); | ||
|
||
if (existingIndex >= 0) { | ||
// Replace the existing entry in the cache | ||
logger.i('Updating existing badge in cache: $cacheKey'); | ||
cache[existingIndex] = MapEntry(cacheKey, jsonData); | ||
} else { | ||
// Add as a new entry if it doesn't exist | ||
logger.i('Adding new badge to cache: $cacheKey'); | ||
cache.add(MapEntry(cacheKey, jsonData)); | ||
} | ||
|
||
logger.i('Data saved to $filePath'); | ||
} catch (e) { | ||
logger.i('Error saving data: $e'); | ||
|
@@ -315,9 +329,36 @@ class FileHelper { | |
|
||
//function that takes JsonSData and returns the Data object | ||
Data jsonToData(Map<String, dynamic> jsonData) { | ||
// Convert JSON data to Data object | ||
Data data = Data.fromJson(jsonData); | ||
return data; | ||
try { | ||
// Convert JSON data to Data object | ||
Data data = Data.fromJson(jsonData); | ||
return data; | ||
} catch (e) { | ||
// If there's an error with the 'messages' key missing, add it with default values | ||
if (e.toString().contains("Missing \"messages\" key")) { | ||
logger.w('Fixing missing "messages" key in badge data'); | ||
|
||
// Create a default message structure if missing | ||
Comment on lines
+332
to
+341
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (bug_risk): jsonToData may mask data issues by auto-fixing missing keys. Log a warning or error when default values are used, and notify the UI or user about potentially incomplete badge data. |
||
Map<String, dynamic> fixedJsonData = | ||
Map<String, dynamic>.from(jsonData); | ||
fixedJsonData['messages'] = [ | ||
{ | ||
'text': jsonData['text'] ?? ['00'], | ||
'flash': jsonData['flash'] ?? false, | ||
'marquee': jsonData['marquee'] ?? false, | ||
'speed': jsonData['speed'] ?? '0x70', // Default to Speed.one | ||
'mode': jsonData['mode'] ?? '0x00', // Default to Mode.left | ||
'invert': jsonData['invert'] ?? false | ||
} | ||
]; | ||
|
||
return Data.fromJson(fixedJsonData); | ||
} else { | ||
// For other errors, rethrow | ||
logger.e('Error parsing badge data: $e'); | ||
rethrow; | ||
} | ||
} | ||
} | ||
|
||
Future<void> shareBadgeData(String filename) async { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Fragile error message string matching in jsonToData
Instead of matching error strings, check if 'messages' exists in jsonData before calling Data.fromJson, or catch a more specific exception if possible.