saving refactorings
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
import 'package:tensordex_mobile/db/tables/poke_db.dart';
|
||||
|
||||
abstract class PokeStorageService {
|
||||
|
||||
Future<void> savePoke(Poke user);
|
||||
|
||||
Future<Poke> getPoke(String pokeId);
|
||||
|
||||
void addNonExistantPokes(List<Poke> pokes);
|
||||
|
||||
void loadDefaultPokes();
|
||||
|
||||
void stop();
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:tensordex_mobile/db/tables/poke_db.dart';
|
||||
import 'package:tensordex_mobile/db/poke_storage_service.dart';
|
||||
|
||||
import 'package:tensordex_mobile/hive.dart';
|
||||
|
||||
import '../utils/logger.dart';
|
||||
|
||||
/// Implementation of StorageService using Hive Key-Value DB
|
||||
class PokeStorageServiceImpl implements PokeStorageService {
|
||||
PokeStorageServiceImpl();
|
||||
|
||||
@override
|
||||
Future savePoke(Poke poke) async {
|
||||
final pokeBox = await Hive.openBox(pokeBoxName);
|
||||
await pokeBox.put(poke.id, poke);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Poke> getPoke(String id) async {
|
||||
final pokeBox = await Hive.openBox(pokeBoxName);
|
||||
final poke = await pokeBox.get(id);
|
||||
return poke;
|
||||
}
|
||||
|
||||
@override
|
||||
Future addNonExistantPokes(List<Poke> pokes) async {
|
||||
final pokeBox = await Hive.openBox(pokeBoxName);
|
||||
|
||||
Future.forEach(pokes, (Poke poke) async {
|
||||
if (!pokeBox.containsKey(poke.id)) {
|
||||
await pokeBox.put(poke.id, poke);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> loadDefaultPokes() async {
|
||||
var allPokeJson = await _loadPokeJson();
|
||||
List<Poke> pokes = [];
|
||||
for (var pokeJson in allPokeJson) {
|
||||
pokes.add(Poke(
|
||||
id: pokeJson['id'].toString(),
|
||||
name: pokeJson['name']['english'].toString(),
|
||||
seen: false,
|
||||
images: []));
|
||||
}
|
||||
await addNonExistantPokes(pokes);
|
||||
}
|
||||
|
||||
Future<List<dynamic>> _loadPokeJson() async {
|
||||
return await jsonDecode(await rootBundle.loadString('assets/pokemon.json'));
|
||||
}
|
||||
|
||||
@override
|
||||
void stop() {
|
||||
Hive.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
part 'poke_db.g.dart';
|
||||
|
||||
@HiveType(typeId: 1)
|
||||
class Poke {
|
||||
Poke(
|
||||
{required this.id,
|
||||
required this.name,
|
||||
required this.seen,
|
||||
required this.images});
|
||||
|
||||
@HiveField(0)
|
||||
String id;
|
||||
|
||||
@HiveField(1)
|
||||
String name;
|
||||
|
||||
@HiveField(2)
|
||||
bool seen;
|
||||
|
||||
@HiveField(3)
|
||||
List<String> images;
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return '$id: $name | seen?:$seen | images: $images';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'poke_db.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// TypeAdapterGenerator
|
||||
// **************************************************************************
|
||||
|
||||
class PokeAdapter extends TypeAdapter<Poke> {
|
||||
@override
|
||||
final int typeId = 1;
|
||||
|
||||
@override
|
||||
Poke read(BinaryReader reader) {
|
||||
final numOfFields = reader.readByte();
|
||||
final fields = <int, dynamic>{
|
||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||
};
|
||||
return Poke(
|
||||
id: fields[0] as String,
|
||||
name: fields[1] as String,
|
||||
seen: fields[2] as bool,
|
||||
images: (fields[3] as List).cast<String>(),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void write(BinaryWriter writer, Poke obj) {
|
||||
writer
|
||||
..writeByte(4)
|
||||
..writeByte(0)
|
||||
..write(obj.id)
|
||||
..writeByte(1)
|
||||
..write(obj.name)
|
||||
..writeByte(2)
|
||||
..write(obj.seen)
|
||||
..writeByte(3)
|
||||
..write(obj.images);
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => typeId.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is PokeAdapter &&
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
Reference in New Issue
Block a user