Skip to content

Commit

Permalink
Small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
maniman303 committed Aug 3, 2022
1 parent 4c543c5 commit 80d36cd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 72 deletions.
67 changes: 3 additions & 64 deletions mobile/CampaignKeeper_mobile/lib/managers/base_manager.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import 'dart:async';
import 'dart:convert';

import 'package:campaign_keeper_mobile/entities/base_entity.dart';
import 'package:campaign_keeper_mobile/services/cache_util.dart';
import 'package:campaign_keeper_mobile/services/helpers/database_helper.dart';
import 'package:campaign_keeper_mobile/types/entity_types.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -89,38 +86,9 @@ class BaseManager<T extends BaseEntity> extends ChangeNotifier {
return res;
}

Future<void> cacheList(List<T> list, String key) async {
var data = list.map((e) => e.toMap()).toList();

CacheUtil().add(key, json.encode(data));
}

Future<void> cacheMap(Map<int, List<T>> map, String key, List<int> filter) async {
var data = [];

map.forEach(
(mapKey, list) {
if (filter.isEmpty || filter.contains(mapKey)) {
data.addAll(list.map((e) => e.toMap()));
}
},
);

CacheUtil().add(key, json.encode(data));
}

Future<void> cacheListToDb(String tableName, List<T> entities, {List<String>? excludedColumns}) async {
List<Map<String, Object?>> maps = entities.map((e) {
var map = e.toMap();

if (excludedColumns != null && excludedColumns.isNotEmpty) {
excludedColumns.forEach((column) {
map.remove(column);
});
}

return map;
}).toList();
// Caches list of entities ensuring there's no leftovers.
Future<void> cacheListToDb(String tableName, List<T> entities) async {
List<Map<String, Object?>> maps = entities.map((e) => e.toMap()).toList();

await _db.delete(tableName);
await _db.insertList(tableName, maps);
Expand All @@ -129,33 +97,4 @@ class BaseManager<T extends BaseEntity> extends ChangeNotifier {
Future<List<Map>> getListFromDb(String tableName, {String? where, List<Object?>? whereArgs}) async {
return await _db.get(tableName, where: where, whereArgs: whereArgs);
}

Future<List<Map>> getValues<T>({required String entityTable, String entityType = ''}) async {
String valueTable = '';

if (T == String) {
valueTable = 'strings';
} else if (T == int) {
valueTable = 'integers';
} else {
return [];
}

return await _db
.get(valueTable, where: 'entityTable = ? and entityType = ?', whereArgs: [entityTable, entityType]);
}

List<Map<String, Object>> listToValueMaps(List<Object> data,
{required int entityId, required String entityTable, String entityType = ''}) {
var valueMaps = data
.map((e) => {
'entityTable': entityTable,
'entityType': entityType,
'entityId': entityId,
'value': e,
})
.toList();

return valueMaps;
}
}
14 changes: 8 additions & 6 deletions mobile/CampaignKeeper_mobile/lib/managers/event_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ class EventManager extends BaseManager<EventEntity> {
where: 'entityTable = ? and entityType = ?',
whereArgs: [tableName, 'description'],
);
List<Map> parentsMaps = await getValues<int>(entityTable: tableName, entityType: 'parents');
List<Map> childrenMaps = await getValues<int>(entityTable: tableName, entityType: 'children');
List<Map> parentsMaps =
await DatabaseHelper().getValues<int>(entityTable: tableName, entityType: 'parents');
List<Map> childrenMaps =
await DatabaseHelper().getValues<int>(entityTable: tableName, entityType: 'children');

var characterDict = charactersMaps.groupListsBy((e) => e['entityId']);
var placesDict = placesMaps.groupListsBy((e) => e['entityId']);
Expand Down Expand Up @@ -199,10 +201,10 @@ class EventManager extends BaseManager<EventEntity> {
e.placeValues.map((f) => f.toMap(entityTable: tableName, entityId: e.id, entityType: 'places'));
var description = e.descriptionValues
.map((f) => f.toMap(entityTable: tableName, entityId: e.id, entityType: 'description'));
var parents =
listToValueMaps(e.parentIds, entityId: e.id, entityTable: tableName, entityType: 'parents');
var children =
listToValueMaps(e.childrenIds, entityId: e.id, entityTable: tableName, entityType: 'children');
var parents = DatabaseHelper.listToValueMaps(e.parentIds,
entityId: e.id, entityTable: tableName, entityType: 'parents');
var children = DatabaseHelper.listToValueMaps(e.childrenIds,
entityId: e.id, entityTable: tableName, entityType: 'children');

fieldsMaps.addAll(characters);
fieldsMaps.addAll(places);
Expand Down
4 changes: 2 additions & 2 deletions mobile/CampaignKeeper_mobile/lib/managers/schema_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class SchemaManager extends BaseManager<SchemaEntity> {
Future<List<Map>> _getCache() async {
List<Map> resMaps = [];
List<Map> entMaps = await getListFromDb(tableName);
List<Map> fieldsMaps = await getValues<String>(entityTable: tableName);
List<Map> fieldsMaps = await DatabaseHelper().getValues<String>(entityTable: tableName);

var dict = fieldsMaps.groupListsBy((e) => e['entityId']);

Expand Down Expand Up @@ -161,7 +161,7 @@ class SchemaManager extends BaseManager<SchemaEntity> {
List<Map<String, Object>> fieldsMaps = [];

entities.forEach((e) {
var fields = listToValueMaps(e.fields, entityId: e.id, entityTable: tableName);
var fields = DatabaseHelper.listToValueMaps(e.fields, entityId: e.id, entityTable: tableName);

if (fields.isNotEmpty) {
fieldsMaps.addAll(fields);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,24 @@ import 'package:flutter/widgets.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';

// Helper that gives a layer of abstraction and configuration
// to the sqlite database operations.
class DatabaseHelper {
// Maps an list of objects into a list of maps ready to be cached.
static List<Map<String, Object>> listToValueMaps(List<Object> data,
{required int entityId, required String entityTable, String entityType = ''}) {
var valueMaps = data
.map((e) => {
'entityTable': entityTable,
'entityType': entityType,
'entityId': entityId,
'value': e,
})
.toList();

return valueMaps;
}

DatabaseHelper._internal();

static final DatabaseHelper _dbHelper = DatabaseHelper._internal();
Expand Down Expand Up @@ -76,6 +93,22 @@ class DatabaseHelper {
return await _database.query(tableName, where: where, whereArgs: whereArgs);
}

// Gets values from either 'strings' or 'integers' table.
Future<List<Map>> getValues<T>({required String entityTable, String entityType = ''}) async {
String valueTable = '';

if (T == String) {
valueTable = 'strings';
} else if (T == int) {
valueTable = 'integers';
} else {
return [];
}

return await get(valueTable,
where: 'entityTable = ? and entityType = ?', whereArgs: [entityTable, entityType]);
}

Future<void> delete(String tableName, {String? where, List<Object?>? whereArgs}) async {
await _database.delete(tableName, where: where, whereArgs: whereArgs);
}
Expand Down

0 comments on commit 80d36cd

Please sign in to comment.