Skip to content
This repository has been archived by the owner on Jul 20, 2021. It is now read-only.

Commit

Permalink
add game in list create & view for game in list
Browse files Browse the repository at this point in the history
  • Loading branch information
IceArrow256 committed Feb 14, 2021
1 parent 20fdf59 commit 2ce139b
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 133 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
## [0.9.0] - 2020-02-14
### Added
- Make migration from 2 to 3 version.
- User can add game to own game list in game view.

### Changed
- Make database object global for all app.
- New way to fetch data for search tab.
- Game tab show user's game list instead behave like search tab.

## [0.8.0] - 2020-02-13
### Added
Expand Down
24 changes: 13 additions & 11 deletions lib/db/dao/game_dao.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import 'package:floor/floor.dart';

import 'package:game_list/db/model/game.dart';

@dao
abstract class GameDao {
@delete
Future<void> deleteGame(Game game);

@delete
Future<int> deleteGames(List<Game> games);

@Query('SELECT * FROM Game ORDER BY name ASC')
Future<List<Game>> findAllGames();
Stream<List<Game>> findAllAsStream();

@Query('SELECT * FROM Game WHERE name LIKE :name ORDER BY name ASC')
Future<List<Game>> findGamesByName(String name);
Stream<List<Game>> findAllAsStreamByName(String name);

@Query('SELECT * FROM Game ORDER BY name ASC')
Future<List<Game>> findAllGames();

@Query('SELECT * FROM Game WHERE id = :id')
Stream<Game> findGameById(int id);
Future<Game> findById(int id);

@insert
Future<void> insertGame(Game game);
Future<void> insertObject(Game game);

@Update(onConflict: OnConflictStrategy.replace)
Future<void> updateGame(Game game);

@delete
Future<void> deleteGame(Game game);

@delete
Future<int> deleteGames(List<Game> games);
}
21 changes: 10 additions & 11 deletions lib/db/dao/game_in_list_dao.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import 'package:floor/floor.dart';

import 'package:game_list/db/model/game_in_list.dart';

@dao
abstract class GameInListDao {
@delete
Future<int> deleteGamesInList(List<GameInList> gamesInList);

@delete
Future<void> deleteObject(GameInList gameInList);

@Query('SELECT * FROM GameInList')
Future<List<GameInList>> findAllGamesInList();
Future<List<GameInList>> findAll();

@Query('SELECT * FROM GameInList WHERE id = :id')
Stream<GameInList> findGameById(int id);
@Query('SELECT * FROM GameInList WHERE game_id = :id')
Future<GameInList> findByGameId(int id);

@insert
Future<void> insertGameInList(GameInList gameInList);
Future<void> insertObject(GameInList gameInList);

@Update(onConflict: OnConflictStrategy.replace)
Future<void> updateGameInList(GameInList gameInList);

@delete
Future<void> deleteGameInList(GameInList gameInList);

@delete
Future<int> deleteGamesInList(List<GameInList> gamesInList);
}
9 changes: 8 additions & 1 deletion lib/db/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ final migration1to2 = Migration(1, 2, (database) async {
await database.execute('ALTER TABLE game ADD COLUMN cover_url TEXT');
});

final migration2to3 = Migration(2, 3, (database) async {
await database.execute(
'CREATE TABLE IF NOT EXISTS `GameInList` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `game_id` INTEGER NOT NULL, `date_added` INTEGER NOT NULL, FOREIGN KEY (`game_id`) REFERENCES `Game` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION)');
await database.execute(
'CREATE UNIQUE INDEX `index_GameInList_game_id` ON `GameInList` (`game_id`)');
});

@TypeConverters([DateTimeConverter])
@Database(version: 2, entities: [Game, GameInList])
@Database(version: 3, entities: [Game, GameInList])
abstract class AppDatabase extends FloorDatabase {
GameDao get gameDao;
GameInListDao get gameInListDao;
Expand Down
54 changes: 30 additions & 24 deletions lib/db/database.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 3 additions & 13 deletions lib/db/model/game_in_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import 'package:floor/floor.dart';
import 'package:game_list/db/model/game.dart';

@Entity(tableName: 'GameInList', foreignKeys: [
ForeignKey(childColumns: ['game_id'], parentColumns: ['id'], entity: Game)
ForeignKey(childColumns: ['game_id'], parentColumns: ['id'], entity: Game),
], indices: [
Index(value: ['game_id'], unique: true)
])
class GameInList {
@PrimaryKey(autoGenerate: true)
Expand All @@ -16,15 +18,3 @@ class GameInList {

GameInList(this.id, this.gameId, this.dateAdded);
}

// class Dog {
// @PrimaryKey()
// final int id;

// final String name;

// @ColumnInfo(name: 'owner_id')
// final int ownerId;

// Dog(this.id, this.name, this.ownerId);
// }
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void main() async {
WidgetsFlutterBinding.ensureInitialized();
final database = await $FloorAppDatabase
.databaseBuilder('game_list.db')
.addMigrations([migration1to2]).build();
.addMigrations([migration1to2, migration2to3]).build();
runApp(App(database: database));
}

Expand All @@ -40,7 +40,7 @@ class _AppState extends State<App> with WidgetsBindingObserver {
database: widget.database,
isDarkTheme: _isDarkTheme,
updateTheme: _updateTheme),
GameView.routeName: (context) => GameView(),
GameView.routeName: (context) => GameView(database: widget.database),
GameAdd.routeName: (context) => GameAdd(),
GameEdit.routeName: (context) => GameEdit(),
},
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/game/game_add.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ class _GameAddState extends State<GameAdd> {
void _createGame() async {
final database = await _database;
var game = Game(null, _name, _coverUrl);
database.gameDao.insertGame(game);
database.gameDao.insertObject(game);
}
}
41 changes: 38 additions & 3 deletions lib/pages/game/game_view.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import 'package:floor/floor.dart';
import 'package:flutter/material.dart';
import 'package:game_list/db/dao/game_in_list_dao.dart';
import 'package:game_list/db/database.dart';
import 'package:game_list/db/model/game.dart';
import 'package:game_list/db/model/game_in_list.dart';
import 'package:game_list/pages/game/game_edit.dart';

class GameView extends StatefulWidget {
static const routeName = '/gameView';
final AppDatabase database;

const GameView({Key key, @required this.database}) : super(key: key);

@override
_GameViewState createState() => _GameViewState();
}

class _GameViewState extends State<GameView> {
Game _game;
GameInListDao _gameInListDao;

@override
Widget build(BuildContext context) {
Expand All @@ -19,7 +27,7 @@ class _GameViewState extends State<GameView> {
appBar: AppBar(
actions: [
IconButton(
icon: const Icon(Icons.edit),
icon: Icon(Icons.edit),
onPressed: () async {
var _result = (await Navigator.pushNamed(
context, GameEdit.routeName,
Expand All @@ -41,8 +49,29 @@ class _GameViewState extends State<GameView> {
overflow: TextOverflow.fade,
),
),
floatingActionButton:
FloatingActionButton(child: Icon(Icons.add), onPressed: () {}),
floatingActionButton: FloatingActionButton(
child: FutureBuilder<GameInList>(
future: _gameInListDao.findByGameId(game.id),
builder: (_, snapshot) {
if (snapshot.hasData) {
return Icon(Icons.delete);
} else {
return Icon(Icons.add);
}
},
),
onPressed: () async {
var gameInList = await _gameInListDao.findByGameId(game.id);
if (gameInList == null) {
var now = DateTime.now();
var gameInList = GameInList(null, game.id, now);
await _gameInListDao.insertObject(gameInList);
setState(() {});
} else {
await _gameInListDao.deleteObject(gameInList);
setState(() {});
}
}),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
Expand Down Expand Up @@ -75,4 +104,10 @@ class _GameViewState extends State<GameView> {
),
);
}

@override
void initState() {
_gameInListDao = widget.database.gameInListDao;
super.initState();
}
}
Loading

0 comments on commit 2ce139b

Please sign in to comment.