Skip to content

Commit

Permalink
DEV-41079
Browse files Browse the repository at this point in the history
  • Loading branch information
DCrow committed Apr 15, 2022
1 parent a465ca4 commit 36ce2c1
Show file tree
Hide file tree
Showing 23 changed files with 520 additions and 222 deletions.
2 changes: 1 addition & 1 deletion ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: aafe91acc616949ddb318b77800a7f51bffa2a4c

COCOAPODS: 1.10.1
COCOAPODS: 1.11.3
30 changes: 30 additions & 0 deletions lib/app/app.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:drift/drift.dart' show Value;
import 'package:f_logs/f_logs.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
Expand Down Expand Up @@ -31,6 +32,8 @@ class App {

static App? _instance;

static App? get instance => _instance;

static Future<App> init() async {
if (_instance != null) return _instance!;

Expand Down Expand Up @@ -92,6 +95,33 @@ class App {
}
}

Future<void> login(String url, String login, String password) async {
try {
await Api(storage: storage).login(url: url, login: login, password: password);
} on ApiException catch(e) {
throw AppError(e.errorMsg);
} catch(e, trace) {
await reportError(e, trace);
throw AppError(Strings.genericErrorMsg);
}

await loadUserData();
await storage.updatePref(PrefsCompanion(lastLogin: Value(DateTime.now())));
}

Future<void> logout() async {
try {
await Api(storage: storage).logout();
} on ApiException catch(e) {
throw AppError(e.errorMsg);
} catch(e, trace) {
await reportError(e, trace);
throw AppError(Strings.genericErrorMsg);
}

await storage.clearData();
}

static void _intFlogs({
required bool isDebug
}) {
Expand Down
15 changes: 3 additions & 12 deletions lib/app/data/api_credentials_dao.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,10 @@ class ApiCredentialsDao extends DatabaseAccessor<AppStorage> with _$ApiCredentia
ApiCredentialsDao(AppStorage db) : super(db);

Future<ApiCredential> getApiCredential() async {
return await (select(apiCredentials).getSingleOrNull()) ?? ApiCredential(
id: AppStorage.kSingleRecordId,
refreshToken: '',
url: '',
accessToken: ''
);
return select(apiCredentials).getSingle();
}

Future<int> updateApiCredential(ApiCredential apiCredential) {
return into(apiCredentials).insertOnConflictUpdate(apiCredential);
}

Future<int> deleteApiCredential() {
return delete(apiCredentials).go();
Future<int> updateApiCredential(ApiCredentialsCompanion apiCredential) {
return update(apiCredentials).write(apiCredential);
}
}
26 changes: 21 additions & 5 deletions lib/app/data/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ part 'users_dao.dart';
Orders,
OrderLines,
OrderStorages,
ApiCredentials
ApiCredentials,
Prefs
],
daos: [
ApiCredentialsDao,
Expand All @@ -31,12 +32,19 @@ part 'users_dao.dart';
]
)
class AppStorage extends _$AppStorage {
static const int kSingleRecordId = 0;

AppStorage({
required bool logStatements
}) : super(_openConnection(logStatements));


Future<Pref> getPref() async {
return select(prefs).getSingle();
}

Future<int> updatePref(PrefsCompanion pref) {
return update(prefs).write(pref);
}

Future<void> clearData() async {
await transaction(() async {
await _clearData();
Expand All @@ -51,6 +59,7 @@ class AppStorage extends _$AppStorage {
batch.deleteWhere(orderLines, (row) => const Constant(true));
batch.deleteWhere(orderStorages, (row) => const Constant(true));
batch.deleteWhere(apiCredentials, (row) => const Constant(true));
batch.deleteWhere(prefs, (row) => const Constant(true));
});
}

Expand All @@ -63,13 +72,20 @@ class AppStorage extends _$AppStorage {
name: '',
storageName: '',
roles: [],
version: '0.0.0'
version: '0.0.0',
total: 0
));
batch.insert(apiCredentials, ApiCredential(
refreshToken: '',
url: '',
accessToken: ''
));
batch.insert(prefs, Pref());
});
}

@override
int get schemaVersion => 2;
int get schemaVersion => 3;

@override
MigrationStrategy get migration => MigrationStrategy(
Expand Down
Loading

0 comments on commit 36ce2c1

Please sign in to comment.