-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Refactor model and add sqlite database
Signed-off-by: Tiago Góes <[email protected]>
- Loading branch information
1 parent
95849a0
commit 970a500
Showing
15 changed files
with
464 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig" | ||
#include "Generated.xcconfig" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig" | ||
#include "Generated.xcconfig" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Uncomment this line to define a global platform for your project | ||
# platform :ios, '12.0' | ||
|
||
# CocoaPods analytics sends network stats synchronously affecting flutter build latency. | ||
ENV['COCOAPODS_DISABLE_STATS'] = 'true' | ||
|
||
project 'Runner', { | ||
'Debug' => :debug, | ||
'Profile' => :release, | ||
'Release' => :release, | ||
} | ||
|
||
def flutter_root | ||
generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__) | ||
unless File.exist?(generated_xcode_build_settings_path) | ||
raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first" | ||
end | ||
|
||
File.foreach(generated_xcode_build_settings_path) do |line| | ||
matches = line.match(/FLUTTER_ROOT\=(.*)/) | ||
return matches[1].strip if matches | ||
end | ||
raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get" | ||
end | ||
|
||
require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root) | ||
|
||
flutter_ios_podfile_setup | ||
|
||
target 'Runner' do | ||
use_frameworks! | ||
use_modular_headers! | ||
|
||
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__)) | ||
target 'RunnerTests' do | ||
inherit! :search_paths | ||
end | ||
end | ||
|
||
post_install do |installer| | ||
installer.pods_project.targets.each do |target| | ||
flutter_additional_ios_build_settings(target) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import 'package:uuid/uuid.dart'; | ||
|
||
class Task { | ||
String id; | ||
String title; | ||
String description; | ||
DateTime createdAt; | ||
DateTime? completedAt; | ||
bool isCompleted; | ||
int? rewardInSatoshis; | ||
|
||
Task({ | ||
String? id, | ||
required this.title, | ||
required this.description, | ||
required this.createdAt, | ||
this.completedAt, | ||
this.isCompleted = false, | ||
this.rewardInSatoshis = 0, | ||
}) : id = id ?? const Uuid().v4(); | ||
|
||
Map<String, dynamic> toMap() { | ||
return { | ||
'id': id, | ||
'title': title, | ||
'description': description, | ||
'createdAt': createdAt.toIso8601String(), | ||
'completedAt': completedAt?.toIso8601String(), | ||
'isCompleted': isCompleted, | ||
'rewardInSatoshis': rewardInSatoshis, | ||
}; | ||
} | ||
|
||
factory Task.fromMap(Map<String, dynamic> map) { | ||
return Task( | ||
id: map['id'], | ||
title: map['title'], | ||
description: map['description'], | ||
createdAt: DateTime.parse(map['createdAt']), | ||
completedAt: (map['completedAt'] == null || map['completedAt'] == 'null') | ||
? null | ||
: DateTime.parse(map['completedAt']), | ||
isCompleted: map['isCompleted'] == 0 ? false : true, | ||
rewardInSatoshis: map['rewardInSatoshis'], | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import 'package:flutter/cupertino.dart'; | ||
import 'package:sqflite/sqflite.dart'; | ||
import 'package:path/path.dart'; | ||
import '../models/task.dart'; | ||
import 'task_repository.dart'; | ||
|
||
class SQLiteTaskRepository extends ChangeNotifier implements TaskRepository { | ||
Database? _database; | ||
|
||
Future<Database> get database async { | ||
if (_database != null) return _database!; | ||
_database = await _initDatabase(); | ||
return _database!; | ||
} | ||
|
||
Future<Database> _initDatabase() async { | ||
// await deleteDatabase(join(await getDatabasesPath(), 'tasks.db')); | ||
return openDatabase( | ||
join(await getDatabasesPath(), 'tasks.db'), | ||
onCreate: (db, version) { | ||
return db.execute( | ||
'CREATE TABLE tasks(id TEXT PRIMARY KEY, title TEXT, description TEXT, createdAt TEXT, completedAt TEXT, isCompleted INTEGER, rewardInSatoshis INTEGER)', | ||
); | ||
}, | ||
version: 1, | ||
); | ||
} | ||
|
||
@override | ||
Future<List<Task>> getAllTasks() async { | ||
final db = await database; | ||
final List<Map<String, dynamic>> maps = | ||
await db.query('tasks', orderBy: 'createdAt DESC'); | ||
|
||
return List.generate(maps.length, (i) { | ||
return Task.fromMap(maps[i]); | ||
}); | ||
} | ||
|
||
@override | ||
Future<Task?> getTaskById(String id) async { | ||
final db = await database; | ||
final List<Map<String, dynamic>> maps = await db.query( | ||
'tasks', | ||
where: 'id = ?', | ||
whereArgs: [id], | ||
); | ||
|
||
if (maps.isNotEmpty) { | ||
return Task.fromMap(maps.first); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@override | ||
Future<void> addTask(Task task) async { | ||
final db = await database; | ||
await db.insert( | ||
'tasks', | ||
task.toMap(), | ||
conflictAlgorithm: ConflictAlgorithm.replace, | ||
); | ||
notifyListeners(); | ||
} | ||
|
||
@override | ||
Future<void> updateTask(Task task) async { | ||
final db = await database; | ||
await db.update( | ||
'tasks', | ||
task.toMap(), | ||
where: 'id = ?', | ||
whereArgs: [task.id], | ||
); | ||
} | ||
|
||
@override | ||
Future<void> deleteTask(String id) async { | ||
final db = await database; | ||
await db.delete( | ||
'tasks', | ||
where: 'id = ?', | ||
whereArgs: [id], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import '../models/task.dart'; | ||
|
||
abstract class TaskRepository { | ||
Future<List<Task>> getAllTasks(); | ||
Future<Task?> getTaskById(String id); | ||
Future<void> addTask(Task task); | ||
Future<void> updateTask(Task task); | ||
Future<void> deleteTask(String id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,40 @@ | ||
import 'package:flutter/cupertino.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:provider/provider.dart'; | ||
import 'package:todo/models/task_model.dart'; | ||
import 'package:todo/repositories/sqlite_task_repository.dart'; | ||
import 'package:todo/widgets/task_item_widget.dart'; | ||
|
||
class TaskList extends StatelessWidget { | ||
import '../models/task.dart'; | ||
|
||
class TaskList extends StatefulWidget { | ||
const TaskList({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Consumer<TaskModel>(builder: (context, value, child) { | ||
|
||
final widget = context.read<TaskModel>(); | ||
final List<TaskItem> taskItems = []; | ||
|
||
widget.taskList.forEach((task) { | ||
taskItems.add(TaskItem(task.toString())); | ||
}); | ||
State<TaskList> createState() => _TaskListState(); | ||
} | ||
|
||
return ListView( | ||
children: taskItems, | ||
); | ||
}); | ||
class _TaskListState extends State<TaskList> { | ||
@override | ||
Widget build(BuildContext context) { | ||
return Consumer<SQLiteTaskRepository>( | ||
builder: (context, taskRepository, child) => FutureBuilder<List<Task>>( | ||
future: taskRepository.getAllTasks(), | ||
builder: (context, snapshot) { | ||
if (snapshot.connectionState == ConnectionState.waiting) { | ||
return const Center(child: CircularProgressIndicator()); | ||
} else if (snapshot.hasError) { | ||
return Center(child: Text('Error: ${snapshot.error}')); | ||
} else if (!snapshot.hasData || snapshot.data!.isEmpty) { | ||
return const Center(child: Text('Add some work to do!')); | ||
} else { | ||
List<Task> tasks = snapshot.data!; | ||
return ListView.builder( | ||
itemCount: tasks.length, | ||
itemBuilder: (context, index) { | ||
Task task = tasks[index]; | ||
return TaskItem(task); | ||
}, | ||
); | ||
} | ||
})); | ||
} | ||
} |
Oops, something went wrong.