Skip to content

Commit

Permalink
Fix db (#74)
Browse files Browse the repository at this point in the history
* - type of amiibo is now not null

* update info package singleton instance

* change insertAll Dao query from upsert to replace when android version < 30 (sqlite < 3.24.0)

* update db scheme to fix string to bool (should be int to bool)

* update version
  • Loading branch information
EdwynZN authored May 13, 2024
1 parent 66ff72c commit 879e2df
Show file tree
Hide file tree
Showing 17 changed files with 84 additions and 45 deletions.
10 changes: 5 additions & 5 deletions lib/data/drift_sqlite/model/drift_joined_amiibo_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class AmiiboDriftModel with _$AmiiboDriftModel {
const factory AmiiboDriftModel({
@JsonKey(required: true, name: 'amiibo.key') required int key,
@JsonKey(includeIfNull: true, name: 'amiibo.id') String? id,
@Default('') @JsonKey(name: 'amiibo.amiiboSeries') String amiiboSeries,
@Default('') @JsonKey(name: 'amiibo.character') String character,
@Default('') @JsonKey(name: 'amiibo.gameSeries') String gameSeries,
@Default('') @JsonKey(name: 'amiibo.name') String name,
@JsonKey(name: 'amiibo.amiiboSeries', required: true) required String amiiboSeries,
@JsonKey(name: 'amiibo.character', required: true) required String character,
@JsonKey(name: 'amiibo.gameSeries', required: true) required String gameSeries,
@JsonKey(name: 'amiibo.name', required: true) required String name,
@JsonKey(includeIfNull: true, name: 'amiibo.au') String? au,
@JsonKey(includeIfNull: true, name: 'amiibo.eu') String? eu,
@JsonKey(includeIfNull: true, name: 'amiibo.jp') String? jp,
@JsonKey(includeIfNull: true, name: 'amiibo.na') String? na,
@JsonKey(includeIfNull: true, name: 'amiibo.type') String? type,
@JsonKey(required: true, name: 'amiibo.type') required String type,
@JsonKey(name: 'amiibo.cardNumber') int? cardNumber,
@Default(0) @JsonKey(name: 'amiibo_user_preferences.boxed') final int boxed,
@Default(0) @JsonKey(name: 'amiibo_user_preferences.opened') final int opened,
Expand Down
2 changes: 1 addition & 1 deletion lib/data/drift_sqlite/model/map_converter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ AmiiboTable dataFromDomain(d.Amiibo amiibo) {
character: details.character,
gameSeries: details.gameSeries,
amiiboSeries: details.amiiboSeries,
type: details.type!,
type: details.type,
na: details.na,
au: details.au,
eu: details.eu,
Expand Down
11 changes: 10 additions & 1 deletion lib/data/drift_sqlite/source/amiibo_dao.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:amiibo_network/enum/sort_enum.dart' as s;
import 'package:amiibo_network/model/amiibo.dart' hide Amiibo;
import 'package:amiibo_network/model/search_result.dart';
import 'package:amiibo_network/model/update_amiibo_user_attributes.dart';
import 'package:amiibo_network/service/info_package.dart';
import 'package:drift/drift.dart';

part 'amiibo_dao.g.dart';
Expand Down Expand Up @@ -77,7 +78,15 @@ class AmiiboDao extends DatabaseAccessor<AppDatabase>
required List<AmiiboUserPreferencesCompanion> preferences,
}) async {
await batch((batch) {
batch.insertAllOnConflictUpdate(amiibo, amiibosData);
if (InfoPackage.instance.isUpsertFeatureAvailable) {
batch.insertAllOnConflictUpdate(amiibo, amiibosData);
} else {
batch.insertAll(
amiibo,
amiibosData,
mode: InsertMode.insertOrReplace,
);
}
batch.insertAll(
amiiboUserPreferences,
preferences,
Expand Down
2 changes: 1 addition & 1 deletion lib/data/drift_sqlite/source/amiibo_tables.drift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ CREATE TABLE IF NOT EXISTS amiibo_user_preferences(
amiibo_key INTEGER NOT NULL,
boxed INTEGER NOT NULL DEFAULT 0,
opened INTEGER NOT NULL DEFAULT 0,
wishlist BOOLEAN NOT NULL DEFAULT false,
wishlist BOOLEAN NOT NULL DEFAULT 0,
FOREIGN KEY(amiibo_key) REFERENCES amiibo("key"),
UNIQUE (amiibo_key)
) AS AmiiboUserPreferenceTable;
31 changes: 26 additions & 5 deletions lib/data/drift_sqlite/source/drift_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AppDatabase extends _$AppDatabase {
AppDatabase() : super(_openExecuter(AppDatabase._databaseName));

@override
int get schemaVersion => 5;
int get schemaVersion => 6;

@override
MigrationStrategy get migration {
Expand All @@ -33,10 +33,10 @@ class AppDatabase extends _$AppDatabase {
beforeOpen: (details) async {
await customStatement('PRAGMA foreign_keys = ON');
if (kDebugMode) {
// This check pulls in a fair amount of code that's not needed
// anywhere else, so we recommend only doing it in debug builds.
await validateDatabaseSchema();
}
// This check pulls in a fair amount of code that's not needed
// anywhere else, so we recommend only doing it in debug builds.
await validateDatabaseSchema();
}
},
onUpgrade: (Migrator m, int from, int to) async {
await customStatement('PRAGMA foreign_keys = OFF');
Expand Down Expand Up @@ -138,6 +138,27 @@ class AppDatabase extends _$AppDatabase {
await customStatement('DROP TABLE _amiibo_old;');
});
}
if (from == 5 && to == 6) {
await transaction(() async {
await customStatement(
'ALTER TABLE amiibo_user_preferences RENAME TO _amiibo_user_preferences_old;');
await m.createAll();
await customStatement('''INSERT OR REPLACE INTO
amiibo_user_preferences(key, amiibo_key, opened, boxed, wishlist)
SELECT
key,
amiibo_key,
opened,
boxed,
CASE
WHEN wishlist = '1' OR wishlist = 1 THEN 1
ELSE 0
END AS wishlist
FROM _amiibo_user_preferences_old ORDER BY amiibo_key;
''');
await customStatement('DROP TABLE _amiibo_user_preferences_old;');
});
}
},
);
}
Expand Down
10 changes: 5 additions & 5 deletions lib/data/local_file_source/model/amiibo_local_json_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class AmiiboLocalFile with _$AmiiboLocalFile {
const factory AmiiboLocalFile({
required int key,
@JsonKey(includeIfNull: true) String? id,
@Default('') String amiiboSeries,
@Default('') String character,
@Default('') String gameSeries,
@Default('') String name,
@JsonKey(required: true) required String amiiboSeries,
@JsonKey(required: true) required String character,
@JsonKey(required: true) required String gameSeries,
@JsonKey(required: true) required String name,
@JsonKey(includeIfNull: true) String? au,
@JsonKey(includeIfNull: true) String? eu,
@JsonKey(includeIfNull: true) String? jp,
@JsonKey(includeIfNull: true) String? na,
@JsonKey(includeIfNull: true) String? type,
@JsonKey(required: true) required String type,
int? cardNumber,
}) = _AmiiboLocalFile;

Expand Down
10 changes: 5 additions & 5 deletions lib/data/sqlite/model/amiibo_sqlite_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class AmiiboSqlite with _$AmiiboSqlite {
const factory AmiiboSqlite({
required int key,
@JsonKey(includeIfNull: true) String? id,
@Default('') String amiiboSeries,
@Default('') String character,
@Default('') String gameSeries,
@Default('') String name,
@JsonKey(required: true) required String amiiboSeries,
@JsonKey(required: true) required String character,
@JsonKey(required: true) required String gameSeries,
@JsonKey(required: true) required String name,
@JsonKey(includeIfNull: true) String? au,
@JsonKey(includeIfNull: true) String? eu,
@JsonKey(includeIfNull: true) String? jp,
@JsonKey(includeIfNull: true) String? na,
@JsonKey(includeIfNull: true) String? type,
@JsonKey(required: true) required String type,
int? cardNumber,
@Default(0) final int boxed,
@Default(0) final int opened,
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Future<void> main() async {

/// Check Android version
if (!kIsWeb && defaultTargetPlatform == TargetPlatform.android) {
await InfoPackage.versionCode();
await InfoPackage.instance.versionCode();
}

final preferences = await SharedPreferences.getInstance();
Expand Down
10 changes: 5 additions & 5 deletions lib/model/amiibo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ class Amiibo with _$Amiibo {
class AmiiboDetails with _$AmiiboDetails {
const factory AmiiboDetails({
@JsonKey(includeIfNull: true) String? id,
@Default('') String amiiboSeries,
@Default('') String character,
@Default('') String gameSeries,
@Default('') String name,
@JsonKey(required: true) required String amiiboSeries,
@JsonKey(required: true) required String character,
@JsonKey(required: true) required String gameSeries,
@JsonKey(required: true) required String name,
@JsonKey(includeIfNull: true) String? au,
@JsonKey(includeIfNull: true) String? eu,
@JsonKey(includeIfNull: true) String? jp,
@JsonKey(includeIfNull: true) String? na,
@JsonKey(includeIfNull: true) String? type,
@JsonKey(required: true) required String type,
int? cardNumber,
}) = _AmiiboDetails;

Expand Down
2 changes: 1 addition & 1 deletion lib/screen/detail_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class _AmiiboCard extends ConsumerWidget {
final text = ref.watch(detailAmiiboProvider(id)).maybeWhen(
data: (amiibo) {
if (amiibo == null) return '';
return translate.types(amiibo.details.type!);
return translate.types(amiibo.details.type);
},
orElse: () => '...',
);
Expand Down
25 changes: 17 additions & 8 deletions lib/service/info_package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,26 @@ enum AndroidCode {
}
}

// Font feature is supported in Android 5 and above
bool get isFontFeatureEnable =>
InfoPackage.androidVersionCode.code >= AndroidCode.Lollipop.code;

class InfoPackage {
static const _channel =
const MethodChannel("com.dartz.amiibo_network/info_package");
static AndroidCode _version = AndroidCode.Unknown;
static AndroidCode get androidVersionCode => _version;
MethodChannel("com.dartz.amiibo_network/info_package");
AndroidCode _version = AndroidCode.Unknown;
AndroidCode get androidVersionCode => _version;

InfoPackage._();
static final InfoPackage _instance = InfoPackage._();
static InfoPackage get instance => _instance;

// Font feature is supported in Android 5 and above
bool get isFontFeatureEnable =>
androidVersionCode.code >= AndroidCode.Lollipop.code;

// upsert feature is supported in Android API 30 (SQLite >= 3.24.0) and above
bool get isUpsertFeatureAvailable =>
androidVersionCode == AndroidCode.Unknown ||
androidVersionCode.code >= AndroidCode.R.code;

static Future<void> versionCode() async {
Future<void> versionCode() async {
//TODO IOS Platform Channel
try {
final version = await _channel.invokeMethod<int?>('version');
Expand Down
4 changes: 2 additions & 2 deletions lib/service/screenshot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class Screenshot {

final String? longestWord = owned!.length > wished!.length ? owned : wished;
final bool fontFeatureStyle =
!userPreferences.usePercentage && isFontFeatureEnable;
!userPreferences.usePercentage && InfoPackage.instance.isFontFeatureEnable;

/// Activate fontFeature only if StatMode is Ratio and isFontFeatureEnable is true for this device
TextSpan longestParagraphTest = TextSpan(
Expand Down Expand Up @@ -452,7 +452,7 @@ class Screenshot {
final double maxX = size.width;
final double maxY = size.height;
final usePercentage = userPreferences.usePercentage;
final bool fontFeatureStyle = !usePercentage && isFontFeatureEnable;
final bool fontFeatureStyle = !usePercentage && InfoPackage.instance.isFontFeatureEnable;

/// Activate fontFeature only if StatMode is Ratio and isFontFeatureEnable is true for this device
Expand Down
2 changes: 1 addition & 1 deletion lib/service/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ String get dateTaken {
/// to unlock it
Future<bool> permissionGranted(ScaffoldMessengerState? scaffoldState) async {
S translate = S.current;
final versionCode = InfoPackage.androidVersionCode;
final versionCode = InfoPackage.instance.androidVersionCode;
if (versionCode == AndroidCode.Unknown)
return false;
else if (versionCode.code < AndroidCode.Q.code &&
Expand Down
2 changes: 1 addition & 1 deletion lib/widget/amiibo_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AmiiboDetailInfo extends ConsumerWidget {
if (amiibo.amiiboSeries != amiibo.gameSeries)
TextCardDetail(text: translate.game(amiibo.gameSeries)),
TextCardDetail(
text: translate.types(amiibo.type!),
text: translate.types(amiibo.type),
),
if (amiibo.au != null)
RegionDetail(amiibo.au!, NetworkIcons.au, translate.au),
Expand Down
2 changes: 1 addition & 1 deletion lib/widget/detail/amiibo_header_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class AmiiboCard extends ConsumerWidget {
if (amiibo == null) return '';
final details = amiibo.details;
final title = StringBuffer(
translate.types(details.type ?? ''),
translate.types(details.type),
);
if (details.cardNumber != null) {
title.write(' #${details.cardNumber}');
Expand Down
2 changes: 1 addition & 1 deletion lib/widget/stat_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class StatWidget extends StatelessWidget {
ref.watch(personalProvider.select((p) => p.usePercentage));
final String myStat = StatUtils.parseStat(numerator, den,
usePercentage: isPercentage);
final bool fontFeatureStyle = !isPercentage && isFontFeatureEnable;
final bool fontFeatureStyle = !isPercentage && InfoPackage.instance.isFontFeatureEnable;

/// Activate fontFeature only if StatMode is Ratio and isFontFeatureEnable is true for this device
return RichText(
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.91+81
version: 2.1.91+82

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

0 comments on commit 879e2df

Please sign in to comment.