Skip to content

Commit

Permalink
Fix import (#71)
Browse files Browse the repository at this point in the history
* use JsonUtf8Encoder to export amiibos

* fix malformed json by replacing them with a space before decoding

* update version
  • Loading branch information
EdwynZN authored Apr 19, 2024
1 parent 4eaeb19 commit ae048af
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 23 deletions.
24 changes: 16 additions & 8 deletions lib/screen/settings_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:amiibo_network/riverpod/theme_provider.dart';
import 'package:amiibo_network/service/screenshot.dart';
import 'package:amiibo_network/enum/amiibo_category_enum.dart';
import 'package:amiibo_network/utils/format_color_on_theme.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand All @@ -22,7 +23,6 @@ import 'package:amiibo_network/utils/urls_constants.dart';
import 'package:amiibo_network/service/notification_service.dart';
import 'package:amiibo_network/model/search_result.dart';
import 'package:amiibo_network/widget/selected_chip.dart';
import 'package:amiibo_network/model/amiibo.dart';

class SettingsPage extends ConsumerWidget {
const SettingsPage({Key? key}) : super(key: key);
Expand Down Expand Up @@ -558,19 +558,27 @@ class _BottomBarState extends ConsumerState<BottomBar> {
else if (file?.files.single.extension != 'json') {
openSnackBar(translate.errorImporting);
} else {
Map<String, dynamic>? map = await compute(readFile, _path);
if (map == null)
//final Uint8List data = file!.files.single.bytes!;
final AmiiboFile amiiboFile = await compute(readFile, _path);
if (amiiboFile is AmiiboFileError) {
FirebaseCrashlytics.instance.recordError(
amiiboFile.error,
amiiboFile.stackTrace,
);
openSnackBar(translate.errorImporting);
else {
List<Amiibo> amiibos = await compute(entityFromMap, map);
await service.update(amiibos);
openSnackBar(translate.successImport);
return;
}
await service.update((amiiboFile as AmiiboFileData).amiibos);
openSnackBar(translate.successImport);
}
await FilePicker.platform.clearTemporaryFiles();
} on PlatformException catch (e) {
} on PlatformException catch (e, s) {
FirebaseCrashlytics.instance.recordError(e, s);
debugPrint(e.message);
openSnackBar(translate.storagePermission('denied'));
} catch (e, s) {
FirebaseCrashlytics.instance.recordError(e, s);
openSnackBar(translate.errorImporting);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/service/notification_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ class NotificationService {
required String name,
required List<Amiibo> amiibos,
}) async {
final map = json.encode(amiibos);
final encoder = JsonUtf8Encoder();
final Map<String, dynamic> args = <String, dynamic>{
'title': title,
'actionTitle': actionNotificationTitle,
'id': 9,
'buffer': Uint8List.fromList(map.codeUnits),
'buffer': encoder.convert(amiibos),// Uint8List.fromList(map.codeUnits),
'name': '${name}_$dateTaken',
};
//TODO IOS Platform Channel
Expand Down
52 changes: 40 additions & 12 deletions lib/service/storage.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import 'dart:io';
import 'dart:convert';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'dart:io';

import 'package:amiibo_network/generated/l10n.dart';
import 'package:amiibo_network/model/amiibo.dart';
import 'package:amiibo_network/service/info_package.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:amiibo_network/service/info_package.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:amiibo_network/generated/l10n.dart';

sealed class AmiiboFile {
const AmiiboFile();
}

class AmiiboFileData extends AmiiboFile {
final List<Amiibo> amiibos;

const AmiiboFileData(this.amiibos);
}

class AmiiboFileError extends AmiiboFile {
final Object error;
final StackTrace stackTrace;

const AmiiboFileError(this.error, this.stackTrace);
}

/// Date String in format year.month.day_hour.minute.second
String get dateTaken {
Expand Down Expand Up @@ -64,20 +82,30 @@ Future<File> createFile(
return file;
}

Map<String, dynamic>? readFile(String? path) {
AmiiboFile readFile(String path) {
try {
final file = File(path!);
String data = file.readAsStringSync();
final file = File(path);
final uintList = file.readAsBytesSync();
/// some malformed json comes with a < 32 ASCII characters, we use this to
/// fix it by replacing them with 32 (space)
String data = utf8.decode(
uintList.map((x) => x < 32 ? 32 : x).toList(),
allowMalformed: true,
);
final jResult = jsonDecode(data);
if (jResult is Map && jResult.containsKey('amiibo')) {
return jResult as Map<String, dynamic>;
final data = entityFromMap(jResult as Map<String, dynamic>);
return AmiiboFileData(data);
} else if (jResult is List<dynamic>) {
return {'amiibo': jResult};
final data = entityFromMap({'amiibo': jResult});
return AmiiboFileData(data);
}
return null;
return AmiiboFileError(
Exception('wrong format: ${jResult.runtimeType}'),
StackTrace.current,
);
} catch (e, s) {
FirebaseCrashlytics.instance.recordError(e, s);
return null;
return AmiiboFileError(e, s);
}
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ description: An Amiibo collection app designed to allow you to keep track of whi
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 2.1.82+78
version: 2.1.83+79

environment:
sdk: '>=3.3.0 <4.0.0'
Expand Down

0 comments on commit ae048af

Please sign in to comment.