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