Skip to content
Snippets Groups Projects
Commit b5f9c860 authored by Yuki's avatar Yuki
Browse files

1. Add checking for data, only load data from API if the list load from database is empty

2. Add testing code in Database.save() and disable testing code in main
parent 6b26b577
No related branches found
No related tags found
No related merge requests found
......@@ -14,37 +14,49 @@ class Database {
List<Character> get storedCharacters {
//TODO: load the characters from a real database.
//*** Must be replaced
//Currently, the characters are not loaded from the database, but simply created and returned afterwards
final List<Character> characters = [
Character(
name: "Jon Snow",
gender: "Male",
aliases: [
"Lord Snow",
"Ned Stark's Bastard",
"The Snow of Winterfell",
"The Crow-Come-Over",
"The 998th Lord Commander of the Night's Watch",
"The Bastard of Winterfell",
"The Black Bastard of the Wall",
"Lord Crow"
],
),
Character(
name: "Brandon Stark",
gender: "Male",
aliases: ["Bran", "Bran the Broken", "The Winged Wolf"],
),
];
List<Character> characters = [];
// //*** Must be replaced
// //Currently, the characters are not loaded from the database, but simply created and returned afterwards
// characters = [
// Character(
// name: "Jon Snow",
// gender: "Male",
// aliases: [
// "Lord Snow",
// "Ned Stark's Bastard",
// "The Snow of Winterfell",
// "The Crow-Come-Over",
// "The 998th Lord Commander of the Night's Watch",
// "The Bastard of Winterfell",
// "The Black Bastard of the Wall",
// "Lord Crow"
// ],
// ),
// Character(
// name: "Brandon Stark",
// gender: "Male",
// aliases: ["Bran", "Bran the Broken", "The Winged Wolf"],
// ),
// ];
return characters;
//*** Replace up to here
}
//The method is to store a list of GoT characters in a database
void save({required List<Character> characters}) {
static void save({required List<Character> characters}) async {
if (characters.isNotEmpty) {
//TODO: save `characters` into a real database.
var box = await Hive.openBox('testBox');
var person = Character(
name: 'Dave',
gender: 'Female',
aliases: ['Linda', 'Marc', 'Anne'],
);
await box.put('dave', person);
print("//// TEST // : ${box.get('dave').name} got "); // Dave
}
}
}
......@@ -4,7 +4,7 @@ import 'package:flutter/material.dart';
import 'package:assignment_1_base_project/db/character.dart';
import 'package:hive_flutter/hive_flutter.dart';
///// HIVE : Example /////
// ///// HIVE : Example /////
// part 'main.g.dart';
// @HiveType(typeId: 1)
......@@ -25,25 +25,20 @@ import 'package:hive_flutter/hive_flutter.dart';
// }
void main() async {
print("============ MAIN START =================");
// print("============ MAIN START =================");
//// Database Init
await Hive.initFlutter(); // Initializing database
Hive.registerAdapter(CharacterAdapter()); //add TypeAdapater
// var path = Directory.current.path;
// Hive
// ..init(path)
// ..registerAdapter(CharacterAdapter());
///// HIVE : Testing /////
var box = await Hive.openBox('testBox');
var person = Character(
name: 'Dave',
gender: 'Female',
aliases: ['Linda', 'Marc', 'Anne'],
);
await box.put('dave', person);
print(box.get('dave').name); // Dave
// ///// HIVE : Testing /////
// var box = await Hive.openBox('testBox');
// var person = Character(
// name: 'Dave',
// gender: 'Female',
// aliases: ['Linda', 'Marc', 'Anne'],
// );
// await box.put('dave', person);
// print("//// TEST // : ${box.get('dave').name} got "); // Dave
print("============ APP START =================");
runApp(
......
......@@ -4,6 +4,7 @@ import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:assignment_1_base_project/db/database.dart';
import 'package:assignment_1_base_project/db/character.dart';
class API {
......@@ -80,6 +81,7 @@ class API {
// print("//// DEBUG --- API Result --- // _characters:${_characters}");
print(
"//// DEBUG --- API Result --- // _characters.length: ${_characters.length} - number of valid data saved"); // Query records number
Database.save(characters: _characters);
return _characters;
}
......
......@@ -18,13 +18,17 @@ class _CharactersListScreenState extends State<CharactersListScreen> {
// first the characters are loaded from the database and displayed
characters = Database().storedCharacters;
// then the characters are loaded from the API and displayed as soon as they are there
API().fetchRemoteGoTCharacters().then((characters) {
setState(() {
// the setState method re-renders the UI
this.characters = characters;
//// Check if characters empty (or set to false if null), then load data from API
if (characters?.isEmpty ?? false) {
// then the characters are loaded from the API and displayed as soon as they are there
API().fetchRemoteGoTCharacters().then((characters) {
setState(() {
// the setState method re-renders the UI
this.characters = characters;
});
});
});
}
super.initState();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment