Skip to content

Commit

Permalink
(#42) Code refactoring after initial release
Browse files Browse the repository at this point in the history
- (#47) solved this issue too
  • Loading branch information
giantsol committed Oct 10, 2019
1 parent ef2177b commit 84e1f7f
Show file tree
Hide file tree
Showing 19 changed files with 476 additions and 370 deletions.
2 changes: 1 addition & 1 deletion lib/Dependencies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final AppPreferences _prefs = AppPreferences();
final DrawerRepository _drawerRepository = DrawerRepositoryImpl();
final MemoRepository _memoRepository = MemoRepositoryImpl(_database);
final DateRepository _dateRepository = DateRepositoryImpl();
final ToDoRepository _toDoRepository = TodoRepositoryImpl(_database);
final ToDoRepository _toDoRepository = ToDoRepositoryImpl(_database);
final LockRepository _lockRepository = LockRepositoryImpl(_database);
final PrefsRepository _prefsRepository = PrefsRepositoryImpl(_prefs);
final CategoryRepository _categoryRepository = CategoryRepositoryImpl(_database);
Expand Down
100 changes: 61 additions & 39 deletions lib/data/AppDatabase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
import 'package:path/path.dart';
import 'package:rxdart/rxdart.dart';
import 'package:sqflite/sqflite.dart';
import 'package:todo_app/data/datasource/CategoryDataSource.dart';
import 'package:todo_app/data/datasource/LockDataSource.dart';
import 'package:todo_app/data/datasource/MemoDataSource.dart';
import 'package:todo_app/data/datasource/ToDoDataSource.dart';
import 'package:todo_app/domain/entity/Category.dart';
import 'package:todo_app/domain/entity/CheckPoint.dart';
import 'package:todo_app/domain/entity/DateInWeek.dart';
import 'package:todo_app/domain/entity/DayMemo.dart';
import 'package:todo_app/domain/entity/Lock.dart';
import 'package:todo_app/domain/entity/ToDo.dart';

class AppDatabase {
class AppDatabase implements ToDoDataSource,
MemoDataSource,
LockDataSource,
CategoryDataSource {
static const String TABLE_CHECK_POINTS = 'checkpoints';
static const String TABLE_TODOS = 'todos';
static const String TABLE_LOCKS = 'locks';
Expand Down Expand Up @@ -110,6 +117,7 @@ class AppDatabase {
);
}

@override
Future<List<ToDo>> getToDos(DateTime date) async {
final db = await _database.first;
final List<Map<String, dynamic>> maps = await db.query(
Expand All @@ -126,26 +134,27 @@ class AppDatabase {
return result;
}

Future<void> setDayMemo(DayMemo dayMemo) async {
@override
Future<void> setToDo(ToDo toDo) async {
final db = await _database.first;
await db.insert(
TABLE_DAY_MEMOS,
dayMemo.toDatabase(),
TABLE_TODOS,
toDo.toDatabase(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}

Future<DayMemo> getDayMemo(DateTime date) async {
@override
Future<void> removeToDo(ToDo toDo) async {
final db = await _database.first;
final Map<String, dynamic> map = await db.query(
TABLE_DAY_MEMOS,
where: DayMemo.createWhereQuery(),
whereArgs: DayMemo.createWhereArgs(date),
).then((l) => l.isEmpty ? null : l[0]);
return map != null ? DayMemo.fromDatabase(map)
: DayMemo(year: date.year, month: date.month, day: date.day);
await db.delete(
TABLE_TODOS,
where: ToDo.createWhereQueryForToDo(),
whereArgs: ToDo.createWhereArgsForToDo(toDo),
);
}

@override
Future<List<CheckPoint>> getCheckPoints(DateTime date) async {
final db = await _database.first;
final dateInWeek = DateInWeek.fromDate(date);
Expand All @@ -167,6 +176,39 @@ class AppDatabase {
return checkPoints;
}

@override
Future<void> setCheckPoint(CheckPoint checkPoint) async {
final db = await _database.first;
await db.insert(
TABLE_CHECK_POINTS,
checkPoint.toDatabase(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}

@override
Future<DayMemo> getDayMemo(DateTime date) async {
final db = await _database.first;
final Map<String, dynamic> map = await db.query(
TABLE_DAY_MEMOS,
where: DayMemo.createWhereQuery(),
whereArgs: DayMemo.createWhereArgs(date),
).then((l) => l.isEmpty ? null : l[0]);
return map != null ? DayMemo.fromDatabase(map)
: DayMemo(year: date.year, month: date.month, day: date.day);
}

@override
Future<void> setDayMemo(DayMemo dayMemo) async {
final db = await _database.first;
await db.insert(
TABLE_DAY_MEMOS,
dayMemo.toDatabase(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}

@override
Future<bool> getIsCheckPointsLocked(DateTime date, bool defaultValue) async {
final db = await _database.first;
final Map<String, dynamic> map = await db.query(
Expand All @@ -177,6 +219,7 @@ class AppDatabase {
return map != null ? Lock.fromDatabase(map).isLocked : defaultValue;
}

@override
Future<void> setIsCheckPointsLocked(DateInWeek dateInWeek, bool value) async {
final db = await _database.first;
final map = {
Expand All @@ -190,6 +233,7 @@ class AppDatabase {
);
}

@override
Future<void> setIsDayRecordLocked(DateTime date, bool value) async {
final db = await _database.first;
final map = {
Expand All @@ -203,15 +247,7 @@ class AppDatabase {
);
}

Future<void> setCheckPoint(CheckPoint checkPoint) async {
final db = await _database.first;
await db.insert(
TABLE_CHECK_POINTS,
checkPoint.toDatabase(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}

@override
Future<bool> getIsDayRecordLocked(DateTime date, bool defaultValue) async {
final db = await _database.first;
final Map<String, dynamic> map = await db.query(
Expand All @@ -222,24 +258,7 @@ class AppDatabase {
return map != null ? Lock.fromDatabase(map).isLocked : defaultValue;
}

Future<void> setToDo(ToDo toDo) async {
final db = await _database.first;
await db.insert(
TABLE_TODOS,
toDo.toDatabase(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}

Future<void> removeToDo(ToDo toDo) async {
final db = await _database.first;
await db.delete(
TABLE_TODOS,
where: ToDo.createWhereQueryForToDo(),
whereArgs: ToDo.createWhereArgsForToDo(toDo),
);
}

@override
Future<Category> getCategory(int id) async {
final db = await _database.first;
Map<String, dynamic> map = await db.query(
Expand All @@ -250,6 +269,7 @@ class AppDatabase {
return map != null ? Category.fromDatabase(map) : Category();
}

@override
Future<List<Category>> getAllCategories() async {
final db = await _database.first;
List<Map<String, dynamic>> maps = await db.query(
Expand All @@ -266,6 +286,7 @@ class AppDatabase {
return result;
}

@override
Future<int> setCategory(Category category) async {
if (category.id == Category.ID_DEFAULT) {
return Category.ID_DEFAULT;
Expand All @@ -279,6 +300,7 @@ class AppDatabase {
}
}

@override
Future<void> removeCategory(Category category) async {
final db = await _database.first;
await db.delete(
Expand Down
14 changes: 7 additions & 7 deletions lib/data/CategoryRepositoryImpl.dart
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@

import 'package:todo_app/data/AppDatabase.dart';
import 'package:todo_app/data/datasource/CategoryDataSource.dart';
import 'package:todo_app/domain/entity/Category.dart';
import 'package:todo_app/domain/repository/CategoryRepository.dart';

class CategoryRepositoryImpl implements CategoryRepository {
final AppDatabase _db;
final CategoryDataSource _dataSource;

const CategoryRepositoryImpl(this._db);
const CategoryRepositoryImpl(this._dataSource);

@override
Future<Category> getCategory(int id) async {
return _db.getCategory(id);
return _dataSource.getCategory(id);
}

@override
Future<int> setCategory(Category category) async {
return _db.setCategory(category);
return _dataSource.setCategory(category);
}

@override
Future<List<Category>> getAllCategories() async {
return _db.getAllCategories();
return _dataSource.getAllCategories();
}

@override
Future<void> removeCategory(Category category) async {
return _db.removeCategory(category);
return _dataSource.removeCategory(category);
}
}
14 changes: 7 additions & 7 deletions lib/data/LockRepositoryImpl.dart
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@

import 'package:todo_app/data/AppDatabase.dart';
import 'package:todo_app/data/datasource/LockDataSource.dart';
import 'package:todo_app/domain/entity/DateInWeek.dart';
import 'package:todo_app/domain/repository/LockRepository.dart';

class LockRepositoryImpl implements LockRepository {
final AppDatabase _db;
final LockDataSource _dataSource;

const LockRepositoryImpl(this._db);
const LockRepositoryImpl(this._dataSource);

@override
Future<bool> getIsCheckPointsLocked(DateTime date, bool defaultValue) async {
return await _db.getIsCheckPointsLocked(date, defaultValue);
return await _dataSource.getIsCheckPointsLocked(date, defaultValue);
}

@override
Future<bool> getIsDayRecordLocked(DateTime date, bool defaultValue) async {
return await _db.getIsDayRecordLocked(date, defaultValue);
return await _dataSource.getIsDayRecordLocked(date, defaultValue);
}

@override
void setIsCheckPointsLocked(DateInWeek dateInWeek, bool value) {
_db.setIsCheckPointsLocked(dateInWeek, value);
_dataSource.setIsCheckPointsLocked(dateInWeek, value);
}

@override
void setIsDayRecordLocked(DateTime date, bool value) {
_db.setIsDayRecordLocked(date, value);
_dataSource.setIsDayRecordLocked(date, value);
}

}
14 changes: 7 additions & 7 deletions lib/data/MemoRepositoryImpl.dart
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@

import 'package:todo_app/data/AppDatabase.dart';
import 'package:todo_app/data/datasource/MemoDataSource.dart';
import 'package:todo_app/domain/entity/CheckPoint.dart';
import 'package:todo_app/domain/entity/DayMemo.dart';
import 'package:todo_app/domain/repository/MemoRepository.dart';

class MemoRepositoryImpl implements MemoRepository {
final AppDatabase _db;
final MemoDataSource _dataSource;

const MemoRepositoryImpl(this._db);
const MemoRepositoryImpl(this._dataSource);

@override
Future<List<CheckPoint>> getCheckPoints(DateTime date) async {
return await _db.getCheckPoints(date);
return await _dataSource.getCheckPoints(date);
}

@override
Future<DayMemo> getDayMemo(DateTime date) async {
return await _db.getDayMemo(date);
return await _dataSource.getDayMemo(date);
}

@override
void setDayMemo(DayMemo dayMemo) {
_db.setDayMemo(dayMemo);
_dataSource.setDayMemo(dayMemo);
}

@override
void setCheckPoint(CheckPoint checkPoint) {
_db.setCheckPoint(checkPoint);
_dataSource.setCheckPoint(checkPoint);
}

}
14 changes: 7 additions & 7 deletions lib/data/ToDoRepositoryImpl.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@

import 'package:todo_app/data/AppDatabase.dart';
import 'package:todo_app/data/datasource/ToDoDataSource.dart';
import 'package:todo_app/domain/entity/ToDo.dart';
import 'package:todo_app/domain/repository/ToDoRepository.dart';

class TodoRepositoryImpl implements ToDoRepository {
final AppDatabase _db;
class ToDoRepositoryImpl implements ToDoRepository {
final ToDoDataSource _dataSource;

const TodoRepositoryImpl(this._db);
const ToDoRepositoryImpl(this._dataSource);

@override
Future<List<ToDo>> getToDos(DateTime date) async {
return await _db.getToDos(date);
return await _dataSource.getToDos(date);
}

@override
void setToDo(ToDo toDo) {
_db.setToDo(toDo);
_dataSource.setToDo(toDo);
}

@override
void removeToDo(ToDo toDo) {
_db.removeToDo(toDo);
_dataSource.removeToDo(toDo);
}

}
9 changes: 9 additions & 0 deletions lib/data/datasource/CategoryDataSource.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

import 'package:todo_app/domain/entity/Category.dart';

abstract class CategoryDataSource {
Future<Category> getCategory(int id);
Future<List<Category>> getAllCategories();
Future<int> setCategory(Category category);
Future<void> removeCategory(Category category);
}
9 changes: 9 additions & 0 deletions lib/data/datasource/LockDataSource.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

import 'package:todo_app/domain/entity/DateInWeek.dart';

abstract class LockDataSource {
Future<bool> getIsCheckPointsLocked(DateTime date, bool defaultValue);
void setIsCheckPointsLocked(DateInWeek dateInWeek, bool value);
Future<bool> getIsDayRecordLocked(DateTime date, bool defaultValue);
void setIsDayRecordLocked(DateTime date, bool value);
}
10 changes: 10 additions & 0 deletions lib/data/datasource/MemoDataSource.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

import 'package:todo_app/domain/entity/CheckPoint.dart';
import 'package:todo_app/domain/entity/DayMemo.dart';

abstract class MemoDataSource {
Future<List<CheckPoint>> getCheckPoints(DateTime date);
void setCheckPoint(CheckPoint checkPoint);
Future<DayMemo> getDayMemo(DateTime date);
void setDayMemo(DayMemo dayMemo);
}
Loading

0 comments on commit 84e1f7f

Please sign in to comment.