Skip to content

Commit

Permalink
null safety migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Leptopoda committed Oct 14, 2022
1 parent 61c5175 commit 7f5ae45
Show file tree
Hide file tree
Showing 164 changed files with 1,623 additions and 1,693 deletions.
4 changes: 2 additions & 2 deletions lib/common/appstart/localization_initialize.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import 'package:kiwi/kiwi.dart';
/// correct language
///
class LocalizationInitialize {
PreferencesProvider _preferencesProvider;
String _languageCode;
PreferencesProvider? _preferencesProvider;
String? _languageCode;

///
/// Initialize the localization using the provided language code
Expand Down
40 changes: 19 additions & 21 deletions lib/common/data/database_access.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ import 'package:sqflite/sqflite.dart';

class DatabaseAccess {
static const String _databaseName = "Database.db";
static Database _databaseInstance;
static Database? _databaseInstance;

static const String idColumnName = "id";

Future<Database> get _database async {
if (_databaseInstance != null) return _databaseInstance;

_databaseInstance = await _initDatabase();
return _databaseInstance;
return _databaseInstance ??= await _initDatabase();
}

Future<Database> _initDatabase() async {
Expand Down Expand Up @@ -60,19 +57,20 @@ class DatabaseAccess {
}

Future<List<Map<String, dynamic>>> queryRows(String table,
{bool distinct,
List<String> columns,
String where,
List<dynamic> whereArgs,
String groupBy,
String having,
String orderBy,
int limit,
int offset}) async {
{bool? distinct,
List<String>? columns,
String? where,
List<dynamic>? whereArgs,
String? groupBy,
String? having,
String? orderBy,
int? limit,
int? offset}) async {
Database db = await _database;

// TODO: [Leptopoda] is there a reason this is done? or at maybe use whereArgs.removeWhere()
for (int i = 0; i < (whereArgs?.length ?? 0); i++) {
whereArgs[i] = whereArgs[i] ?? "";
whereArgs![i] = whereArgs[i] ?? "";
}

return await db.query(
Expand All @@ -89,33 +87,33 @@ class DatabaseAccess {
);
}

Future<int> queryRowCount(String table) async {
Future<int?> queryRowCount(String table) async {
Database db = await _database;
return Sqflite.firstIntValue(
await db.rawQuery('SELECT COUNT(*) FROM $table'));
}

Future<int> queryAggregator(String query, List<dynamic> arguments) async {
Future<int?> queryAggregator(String query, List<dynamic> arguments) async {
Database db = await _database;
return Sqflite.firstIntValue(await db.rawQuery(query, arguments));
}

Future<int> update(String table, Map<String, dynamic> row) async {
Database db = await _database;
int id = row[idColumnName];
int? id = row[idColumnName];
return await db
.update(table, row, where: '$idColumnName = ?', whereArgs: [id]);
}

Future<int> delete(String table, int id) async {
Future<int> delete(String table, int? id) async {
Database db = await _database;
return await db.delete(table, where: '$idColumnName = ?', whereArgs: [id]);
}

Future<int> deleteWhere(
String table, {
String where,
List<dynamic> whereArgs,
String? where,
List<dynamic>? whereArgs,
}) async {
Database db = await _database;
return await db.delete(
Expand Down
2 changes: 1 addition & 1 deletion lib/common/data/database_entity.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
abstract class DatabaseEntity {
Map<String, dynamic> toMap();
void fromMap(Map<String, dynamic> map);
fromMap(Map<String, dynamic> map);
}
5 changes: 3 additions & 2 deletions lib/common/data/database_path_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ Future<String> _getiOSDatabasePathAndMigrate(
// it must be saved in a group shared between the app module and the widget
// module. "Migration" means that the database at the old path gets
// copied to the new path
assert(Platform.isIOS);

var groupDirectory = await AppGroupDirectory.getAppGroupDirectory(
Directory? groupDirectory = await AppGroupDirectory.getAppGroupDirectory(
'group.de.bennik2000.dhbwstudentapp',
);

var newPath = join(groupDirectory.path, databaseName);
var newPath = join(groupDirectory!.path, databaseName);

var migrateSuccess = await _migrateOldDatabase(oldPath, newPath);

Expand Down
7 changes: 1 addition & 6 deletions lib/common/data/preferences/app_theme_enum.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@

///
/// Enum which holds the possible themes the app can be displayed in
///
enum AppTheme {
Dark,
Light,
System
}
enum AppTheme { Dark, Light, System }
24 changes: 9 additions & 15 deletions lib/common/data/preferences/preferences_access.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,26 @@ class PreferencesAccess {
case int:
await prefs.setInt(key, value as int);
return;
default:
throw InvalidValueTypeException(T);
}

throw InvalidValueTypeException(T);
}

Future<T> get<T>(String key) async {
Future<T?> get<T>(String key) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();

T value;

switch (T) {
case bool:
value = prefs.getBool(key) as T;
break;
return prefs.getBool(key) as T?;
case String:
value = prefs.getString(key) as T;
break;
return prefs.getString(key) as T?;
case double:
value = prefs.getDouble(key) as T;
break;
return prefs.getDouble(key) as T?;
case int:
value = prefs.getInt(key) as T;
break;
return prefs.getInt(key) as T?;
default:
return null;
}

return value;
}
}

Expand Down
Loading

0 comments on commit 7f5ae45

Please sign in to comment.