Skip to content

Commit

Permalink
Merge branch 'release/1.2.2' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
tokku5552 committed Feb 13, 2021
2 parents 56beb68 + 3a92e48 commit 2cffd89
Show file tree
Hide file tree
Showing 18 changed files with 276 additions and 200 deletions.
22 changes: 22 additions & 0 deletions .github/workflow/flutter_analyze.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Flutter_Analyzer

on:
pull_request:
types: [opened, synchronize]
push:
branches:
- master

jobs:
flutter_analyze:
runs-on: ubuntu-latest
timeout-minutes: 1
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 1
- uses: subosito/flutter-action@v1
with:
channel: 'beta'
- run: flutter pub get
- run: flutter analyze
2 changes: 2 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# https://pub.dev/packages/pedantic_mono
include: package:pedantic_mono/analysis_options.yaml
40 changes: 20 additions & 20 deletions lib/common/database_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,22 @@ import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';

class DatabaseProvider {
final databaseName = "TodoItem.db";
DatabaseProvider._();

final databaseName = 'TodoItem.db';
final databaseVersion = 2;
final migrations = [
'''
alter table todo_item add column isDone BOOL default false;
'''
];

DatabaseProvider._();

static final DatabaseProvider instance = DatabaseProvider._();

Database _database;

Future<Database> get database async {
if (_database != null) return _database;
_database = await initDatabase();
return _database;
return _database ??= await initDatabase();
}

void _createTable(Batch batch) {
Expand All @@ -42,19 +40,21 @@ class DatabaseProvider {
''');
}

initDatabase() async {
return await openDatabase(join(await getDatabasesPath(), databaseName),
version: databaseVersion,
onCreate: (db, version) async {
var batch = db.batch();
_createTable(batch);
await batch.commit();
},
onDowngrade: onDatabaseDowngradeDelete,
onUpgrade: (db, oldVersion, newVersion) async {
for (var i = oldVersion - 1; i <= newVersion - 1; i++) {
await db.execute(migrations[i]);
}
});
Future<Database> initDatabase() async {
final result =
await openDatabase(join(await getDatabasesPath(), databaseName),
version: databaseVersion,
onCreate: (db, version) async {
final batch = db.batch();
_createTable(batch);
await batch.commit();
},
onDowngrade: onDatabaseDowngradeDelete,
onUpgrade: (db, oldVersion, newVersion) async {
for (var i = oldVersion - 1; i <= newVersion - 1; i++) {
await db.execute(migrations[i]);
}
});
return result;
}
}
8 changes: 3 additions & 5 deletions lib/common/persistence_storage_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ class PersistenceStorageProvider {
SharedPreferences _prefs;

Future<SharedPreferences> get prefs async {
if (_prefs != null) return _prefs;
_prefs = await initSharedPreferences();
return _prefs;
return _prefs ??= await initSharedPreferences();
}

initSharedPreferences() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
Future<SharedPreferences> initSharedPreferences() async {
final prefs = await SharedPreferences.getInstance();
return prefs;
}
}
2 changes: 1 addition & 1 deletion lib/domain/storage_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ abstract class StorageRepository {
Future<bool> isExistKey(String key);
}

const String VIEW_COMPLETED_ITEMS_KEY = "view_completed_items";
const String viewCompletedItemsKey = 'view_completed_items';
22 changes: 11 additions & 11 deletions lib/domain/todo_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ class TodoItem {
this.updatedAt,
this.isDone});

factory TodoItem.fromMap(Map<String, dynamic> json) => TodoItem(
id: json['id'] as int,
title: json['title'] as String,
body: json['body'] as String,
createdAt: DateTime.parse(json['createdAt'] as String).toLocal(),
updatedAt: DateTime.parse(json['updatedAt'] as String).toLocal(),
isDone: json['isDone'] == '1',
);

final int id;
String title;
String body;
Expand All @@ -29,25 +38,16 @@ class TodoItem {
bool get getIsDone => isDone;

Map<String, dynamic> toMap() {
return {
return <String, dynamic>{
'id': id,
'title': title,
'body': body,
'createdAt': createdAt.toUtc().toIso8601String(),
'updatedAt': updatedAt.toUtc().toIso8601String(),
'isDone': (isDone) ? 1 : 0,
'isDone': isDone ? 1 : 0,
};
}

factory TodoItem.fromMap(Map<String, dynamic> json) => TodoItem(
id: json["id"],
title: json["title"],
body: json["body"],
createdAt: DateTime.parse(json["createdAt"]).toLocal(),
updatedAt: DateTime.parse(json["updatedAt"]).toLocal(),
isDone: (json["isDone"] == "1") ? true : false,
);

@override
String toString() {
return 'Todo{id: $id, title: $title, createdAt: $createdAt}';
Expand Down
16 changes: 11 additions & 5 deletions lib/domain/todo_item_repository.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import 'package:flutter/material.dart';
import 'package:todo_app_sample_flutter/domain/todo_item.dart';

abstract class TodoItemRepository {
Future<TodoItem> create(String title, String body, bool isDone, DateTime now);
Future<TodoItem> create({
@required String title,
@required String body,
@required bool isDone,
@required DateTime now,
});
Future<List<TodoItem>> findAll({bool viewCompletedItems});
Future<TodoItem> find(int id);
Future<void> update(TodoItem todoItem);
Future<void> updateIsDoneById(int id, bool isDone);
Future<void> delete(int id);
Future<TodoItem> find({@required int id});
Future<void> update({@required TodoItem todoItem});
Future<void> updateIsDoneById({@required int id, @required bool isDone});
Future<void> delete({@required int id});
}
56 changes: 35 additions & 21 deletions lib/infrastructure/todo_item_repository_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* https://opensource.org/licenses/mit-license.php
*
*/
import 'package:sqflite/sqflite.dart';
import 'package:flutter/material.dart';
import 'package:todo_app_sample_flutter/common/database_provider.dart';
import 'package:todo_app_sample_flutter/domain/todo_item.dart';
import 'package:todo_app_sample_flutter/domain/todo_item_repository.dart';
Expand All @@ -15,9 +15,13 @@ class TodoItemRepositoryImpl implements TodoItemRepository {
static DatabaseProvider instance = DatabaseProvider.instance;

@override
Future<TodoItem> create(
String title, String body, bool isDone, DateTime now) async {
final Map<String, dynamic> row = {
Future<TodoItem> create({
@required String title,
@required String body,
@required bool isDone,
@required DateTime now,
}) async {
final row = <String, dynamic>{
'title': title,
'body': body,
'createdAt': now.toString(),
Expand All @@ -28,8 +32,8 @@ class TodoItemRepositoryImpl implements TodoItemRepository {
final id = await db.insert(table, row);
return TodoItem(
id: id,
title: row["title"],
body: row["body"],
title: row['title'] as String,
body: row['body'] as String,
createdAt: now,
updatedAt: now,
isDone: isDone,
Expand All @@ -38,50 +42,60 @@ class TodoItemRepositoryImpl implements TodoItemRepository {

@override
Future<List<TodoItem>> findAll({bool viewCompletedItems = true}) async {
final Database db = await instance.database;
final db = await instance.database;

final rows = (viewCompletedItems == null || viewCompletedItems)
? await db.rawQuery('SELECT * FROM $table ORDER BY updatedAt DESC')
: await db.rawQuery(
'SELECT * FROM $table WHERE isDone = 0 ORDER BY updatedAt DESC');
if (rows.isEmpty) return null;
return rows.map((json) => TodoItem.fromMap(json)).toList();
if (rows.isEmpty) {
return null;
} else {
return rows.map((json) => TodoItem.fromMap(json)).toList();
}
}

@override
Future<TodoItem> find(int id) async {
Future<TodoItem> find({@required int id}) async {
final db = await instance.database;
final rows = await db.rawQuery('SELECT * FROM $table WHERE id = ?', [id]);
if (rows.isEmpty) return null;
return TodoItem.fromMap(rows.first);
final rows =
await db.rawQuery('SELECT * FROM $table WHERE id = ?', <int>[id]);
if (rows.isEmpty) {
return null;
} else {
return TodoItem.fromMap(rows.first);
}
}

@override
Future<void> updateIsDoneById(int id, bool isDone) async {
print("id=$id,isDone=$isDone");
Future<void> updateIsDoneById({
@required int id,
@required bool isDone,
}) async {
print('id=$id,isDone=$isDone');
final row = {
'id': id,
'isDone': (isDone) ? 1 : 0,
'isDone': isDone ? 1 : 0,
};
final db = await instance.database;
await db.update(table, row, where: 'id = ?', whereArgs: [id]);
await db.update(table, row, where: 'id = ?', whereArgs: <int>[id]);
}

@override
Future<void> delete(int id) async {
Future<void> delete({@required int id}) async {
final db = await instance.database;
await db.delete(table, where: "id = ?", whereArgs: [id]);
await db.delete(table, where: 'id = ?', whereArgs: <int>[id]);
}

@override
Future<void> update(TodoItem todoItem) async {
Future<void> update({@required TodoItem todoItem}) async {
final row = {
'id': todoItem.id,
'title': todoItem.title,
'body': todoItem.body,
'updatedAt': todoItem.updatedAt.toString(),
};
final db = await instance.database;
await db.update(table, row, where: 'id = ?', whereArgs: [todoItem.id]);
await db.update(table, row, where: 'id = ?', whereArgs: <int>[todoItem.id]);
}
}
29 changes: 17 additions & 12 deletions lib/presentation/todo_item_detail/todo_item_detail_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,38 @@ class TodoItemDetailModel extends ChangeNotifier {
TodoItemDetailModel({
@required TodoItemRepository todoItemRepository,
}) : _todoItemRepository = todoItemRepository;
TodoItemRepository _todoItemRepository;
final TodoItemRepository _todoItemRepository;

String todoTitle = "";
String todoBody = "";
String todoTitle = '';
String todoBody = '';
bool isDone = false;

void setTodoItem(TodoItem todoItem) {
todoTitle = todoItem.title;
todoBody = todoItem.body;
}

Future<void> add() async {
if (todoTitle == null || todoTitle.isEmpty) {
throw ("タイトルを入力してください。");
final Error error = ArgumentError('タイトルを入力してください。');
throw error;
}
await _todoItemRepository.create(
todoTitle,
(todoBody.isEmpty) ? "" : todoBody,
isDone,
DateTime.now(),
);
title: todoTitle,
body: (todoBody.isEmpty) ? '' : todoBody,
isDone: isDone,
now: DateTime.now());
notifyListeners();
}

Future<void> update(int id) async {
final todoItem = TodoItem(
id: id,
title: (todoTitle.isEmpty) ? todoTitle = "" : todoTitle,
body: (todoBody.isEmpty) ? "" : todoBody,
title: (todoTitle.isEmpty) ? todoTitle = '' : todoTitle,
body: (todoBody.isEmpty) ? '' : todoBody,
updatedAt: DateTime.now(),
);
await _todoItemRepository.update(todoItem);
await _todoItemRepository.update(todoItem: todoItem);
notifyListeners();
}
}
Loading

0 comments on commit 2cffd89

Please sign in to comment.