9 Commits

14 changed files with 407 additions and 38 deletions
+6 -4
View File
@@ -2,8 +2,10 @@ import 'model/task.dart';
import 'service/tools.dart' show generateId; import 'service/tools.dart' show generateId;
List<Task> tasks = [ List<Task> tasks = [
Task(id: generateId(), title: 'Hund föhnen', position: 0), Task(id: generateId(), title: 'Hund föhnen'),
Task(id: '${generateId()}1', title: 'Fuchs streicheln', position: 1), Task(id: '${generateId()}1', title: 'Fuchs streicheln'),
Task(id: '${generateId()}2', title: 'Katze füttern', position: 2), Task(id: '${generateId()}2', title: 'Katze füttern'),
Task(id: '${generateId()}3', title: 'Bär kraulen', position: 3), Task(id: '${generateId()}3', title: 'Bär kraulen'),
]; ];
Iterable<String> taskPositions = tasks.map<String>((e) => e.id);
+14 -2
View File
@@ -1,10 +1,22 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'model/repositories/local_repository.dart';
import 'pages/task_edit_page.dart'; import 'pages/task_edit_page.dart';
import 'pages/task_overview_page.dart'; import 'pages/task_overview_page.dart';
import 'service/controller_scope.dart';
import 'service/task_controller.dart';
void main() { void main() async {
runApp(const MainApp()); final repository = LocalRepository();
WidgetsFlutterBinding.ensureInitialized();
await repository.initialize();
runApp(
ControllerScope(
controller: TaskController(repository),
child: const MainApp(),
),
);
} }
class MainApp extends StatelessWidget { class MainApp extends StatelessWidget {
@@ -38,7 +38,7 @@ class CreateTaskRequest {
location = task.location, location = task.location,
url = task.url; url = task.url;
Task toTask({required String id, required int position}) { Task toTask({required String id}) {
return Task( return Task(
id: id, id: id,
title: title, title: title,
@@ -51,7 +51,6 @@ class CreateTaskRequest {
alarms: alarms, alarms: alarms,
location: location, location: location,
url: url, url: url,
position: position,
); );
} }
} }
@@ -0,0 +1,9 @@
import 'package:flutter/material.dart';
import '../../service/controller_scope.dart';
extension ControllerContext on BuildContext {
T controller<T extends ChangeNotifier>() {
return ControllerScope.of<T>(this);
}
}
+8
View File
@@ -4,4 +4,12 @@ class LatLng {
LatLng(this.lat, this.lng); LatLng(this.lat, this.lng);
LatLng.empty() : lat = 0, lng = 0; LatLng.empty() : lat = 0, lng = 0;
factory LatLng.fromJson(Map<String, dynamic> json) {
return LatLng(json['lat'] as double, json['lng'] as double);
}
Map<String, double> toJson() {
return {'lat': lat, 'lng': lng};
}
} }
+11
View File
@@ -11,4 +11,15 @@ class Location {
String get address => _address ?? ''; String get address => _address ?? '';
LatLng get coordinates => _coordinates ?? LatLng.empty(); LatLng get coordinates => _coordinates ?? LatLng.empty();
factory Location.fromJson(Map<String, dynamic> json) {
return Location(
address: json['address'] as String,
coordinates: LatLng.fromJson(json['coordinates']),
);
}
Map<String, dynamic> toJson() {
return {'address': address, 'coordinates': coordinates};
}
} }
@@ -0,0 +1,15 @@
import '../../task.dart';
abstract class TaskRepository {
Future<List<Task>> loadTasks();
Future<Iterable<String>> loadTaskOrder();
Future<void> saveTaskOrder(List<String> taskOrder);
Future<void> saveTask(Task task);
Future<void> saveTasks(List<Task> tasks);
Future<void> deleteTask(Task task);
}
@@ -0,0 +1,74 @@
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import '../task.dart';
import 'interfaces/task_repository.dart';
class LocalRepository implements TaskRepository {
static const String _tasksKey = 'tasks';
static const String _taskOrderKey = 'taskOrder';
SharedPreferencesWithCache? _prefs;
Future<void> initialize() async {
if (_prefs == null) {
await SharedPreferencesWithCache.create(
cacheOptions: const SharedPreferencesWithCacheOptions(
allowList: <String>{_tasksKey, _taskOrderKey},
),
).then((value) => _prefs = value);
}
}
// Create
@override
Future<void> saveTask(Task task) async {
final tasks = await loadTasks();
if (tasks.contains(task)) {
tasks.remove(task);
} else {
final taskOrder = (await loadTaskOrder()).toList();
taskOrder.add(task.id);
await saveTaskOrder(taskOrder);
}
tasks.add(task);
return saveTasks(tasks);
}
@override
Future<void> saveTasks(List<Task> tasks) async {
final jsonList = tasks.map<String>((e) => jsonEncode(e.toJson())).toList();
return _prefs!.setStringList(_tasksKey, jsonList);
}
@override
Future<void> saveTaskOrder(Iterable<String> taskOrder) {
final jsonList = taskOrder.map<String>((e) => jsonEncode(e)).toList();
return _prefs!.setStringList(_taskOrderKey, jsonList);
}
// Read
@override
Future<Iterable<String>> loadTaskOrder() async {
final Iterable<String> jsonList =
_prefs!.getStringList(_taskOrderKey) ?? [];
return jsonList.map<String>((e) => jsonDecode(e));
}
@override
Future<List<Task>> loadTasks() async {
final Iterable<String> jsonList = _prefs!.getStringList(_tasksKey) ?? [];
return jsonList.map<Task>((e) => Task.fromJson(jsonDecode(e))).toList();
}
// Delete
@override
Future<void> deleteTask(Task task) async {
final tasks = await loadTasks();
tasks.remove(task);
saveTasks(tasks);
}
}
+54 -4
View File
@@ -12,7 +12,6 @@ class Task {
final List<DateTime> alarms; final List<DateTime> alarms;
final Location? location; final Location? location;
final String url; final String url;
final int position;
Task({ Task({
required this.id, required this.id,
@@ -26,7 +25,6 @@ class Task {
this.alarms = const [], this.alarms = const [],
this.location, this.location,
this.url = '', this.url = '',
required this.position,
}); });
Task copyWith({ Task copyWith({
@@ -41,7 +39,6 @@ class Task {
List<DateTime>? alarms, List<DateTime>? alarms,
Location? location, Location? location,
String? url, String? url,
int? position,
}) { }) {
return Task( return Task(
id: id ?? this.id, id: id ?? this.id,
@@ -55,7 +52,60 @@ class Task {
alarms: alarms ?? this.alarms, alarms: alarms ?? this.alarms,
location: location ?? this.location, location: location ?? this.location,
url: url ?? this.url, url: url ?? this.url,
position: position ?? this.position,
); );
} }
factory Task.fromJson(Map<String, dynamic> json) {
return Task(
id: json['id'] as String,
title: json['title'] as String,
description: json['description'] as String? ?? '',
start: json['start'] != null
? DateTime.parse(json['start'] as String)
: null,
due: json['due'] != null ? DateTime.parse(json['due'] as String) : null,
isCompleted: json['isCompleted'] as bool? ?? false,
category: json['category'] as String? ?? '',
subtasks:
(json['subtasks'] as List<dynamic>?)
?.map((e) => Task.fromJson(e as Map<String, dynamic>))
.toList() ??
[],
alarms:
(json['alarms'] as List<dynamic>?)
?.map((e) => DateTime.parse(e as String))
.toList() ??
[],
location: json['location'] != null
? Location.fromJson(json['location'] as Map<String, dynamic>)
: null,
url: json['url'] as String? ?? '',
);
}
Map<String, dynamic> toJson() {
return {
'id': id,
'title': title,
'description': description,
'start': start?.toIso8601String(),
'due': due?.toIso8601String(),
'isCompleted': isCompleted,
'category': category,
'subtasks': subtasks.map((e) => e.toJson()).toList(),
'alarms': alarms.map((e) => e.toIso8601String()).toList(),
'location': location?.toJson(),
'url': url,
};
}
@override
bool operator ==(Object other) {
if (other is! Task) return false;
return hashCode == other.hashCode;
}
@override
int get hashCode => id.hashCode;
} }
+13 -25
View File
@@ -1,8 +1,9 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../example_data.dart';
import '../model/callback_models/create_task_request.dart'; import '../model/callback_models/create_task_request.dart';
import '../model/extensions/controller_context.dart';
import '../model/task.dart'; import '../model/task.dart';
import '../service/task_controller.dart';
import '../service/tools.dart'; import '../service/tools.dart';
import 'task_edit_page.dart'; import 'task_edit_page.dart';
@@ -15,7 +16,8 @@ class TaskOverviewPage extends StatefulWidget {
} }
class _TaskOverviewPageState extends State<TaskOverviewPage> { class _TaskOverviewPageState extends State<TaskOverviewPage> {
//TODO: Replace example data call List<Task> get tasks => context.controller<TaskController>().orderedTasks;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
@@ -23,8 +25,7 @@ class _TaskOverviewPageState extends State<TaskOverviewPage> {
body: ReorderableListView.builder( body: ReorderableListView.builder(
itemBuilder: itemBuilder, itemBuilder: itemBuilder,
itemCount: tasks.length, itemCount: tasks.length,
onReorderItem: (oldIndex, newIndex) => onReorderItem: context.controller<TaskController>().reorderTask,
reorderList(tasks, oldIndex, newIndex),
), ),
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
onPressed: onCreateTaskTapped, onPressed: onCreateTaskTapped,
@@ -42,10 +43,10 @@ class _TaskOverviewPageState extends State<TaskOverviewPage> {
subtitle: Text(task.description), subtitle: Text(task.description),
onTap: () async { onTap: () async {
final result = await onTaskTapped(task); final result = await onTaskTapped(task);
if (result != null) { if (result != null && context.mounted) {
tasks.remove(task); context.controller<TaskController>().saveTask(
tasks.add(result.toTask(id: task.id, position: task.position)); result.toTask(id: task.id),
setState(() {}); );
} }
}, },
); );
@@ -58,28 +59,15 @@ class _TaskOverviewPageState extends State<TaskOverviewPage> {
return result as CreateTaskRequest?; return result as CreateTaskRequest?;
} }
List<Task> reorderList(List<Task> tasks, oldIndex, newIndex) {
final item = tasks.removeAt(oldIndex);
tasks.insert(newIndex, item);
final List<Task> reordered = [];
for (int i = 0; i < tasks.length; i++) {
reordered.add(tasks.elementAt(i).copyWith(position: i));
}
return reordered;
}
void onCreateTaskTapped() async { void onCreateTaskTapped() async {
//TODO: example data call
final result = final result =
await Navigator.of(context).pushNamed(TaskEditPage.routeName) await Navigator.of(context).pushNamed(TaskEditPage.routeName)
as CreateTaskRequest?; as CreateTaskRequest?;
if (result != null) { if (result != null && context.mounted) {
tasks.add(result.toTask(id: generateId(), position: tasks.length)); context.controller<TaskController>().saveTask(
setState(() {}); result.toTask(id: generateId()),
);
} }
} }
} }
+18
View File
@@ -0,0 +1,18 @@
import 'package:flutter/widgets.dart';
class ControllerScope<T extends ChangeNotifier> extends InheritedNotifier<T> {
const ControllerScope({
super.key,
required T controller,
required super.child,
}) : super(notifier: controller);
static T of<T extends ChangeNotifier>(BuildContext context) {
final scope = context
.dependOnInheritedWidgetOfExactType<ControllerScope<T>>();
assert(scope != null, 'No ControllerScope<$T> found in context');
return scope!.notifier!;
}
}
+49
View File
@@ -0,0 +1,49 @@
import 'package:flutter/material.dart' show ChangeNotifier;
import '../model/repositories/interfaces/task_repository.dart';
import '../model/task.dart';
class TaskController extends ChangeNotifier {
TaskController(TaskRepository repository) : _repository = repository {
Future.wait([
_loadTaskOrder(),
_loadTasks(),
]).whenComplete(() => notifyListeners());
}
final TaskRepository _repository;
final List<Task> _tasks = [];
final List<String> _taskOrder = [];
List<Task> get orderedTasks {
final lookup = {for (final task in _tasks) task.id: task};
return _taskOrder.map((id) => lookup[id]!).toList();
}
Future<void> reorderTask(int oldIndex, int newIndex) async {
final taskId = _taskOrder.removeAt(oldIndex);
_taskOrder.insert(newIndex, taskId);
_repository.saveTaskOrder(_taskOrder);
}
Future<void> saveTask(Task task) async {
await _repository.saveTask(task);
await _loadTasks();
await _loadTaskOrder();
notifyListeners();
}
Future<void> _loadTasks() async {
_tasks.clear();
return _repository.loadTasks().then((value) => _tasks.addAll(value));
}
Future<void> _loadTaskOrder() async {
_taskOrder.clear();
return _repository.loadTaskOrder().then(
(value) => _taskOrder.addAll(value),
);
}
}
+134 -1
View File
@@ -49,6 +49,22 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.3.3" version: "1.3.3"
ffi:
dependency: transitive
description:
name: ffi
sha256: "6d7fd89431262d8f3125e81b50d3847a091d846eafcd4fdb88dd06f36d705a45"
url: "https://pub.dev"
source: hosted
version: "2.2.0"
file:
dependency: transitive
description:
name: file
sha256: a3b4f84adafef897088c160faf7dfffb7696046cb13ae90b508c2cbc95d3b8d4
url: "https://pub.dev"
source: hosted
version: "7.0.1"
flutter: flutter:
dependency: "direct main" dependency: "direct main"
description: flutter description: flutter
@@ -67,6 +83,11 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
leak_tracker: leak_tracker:
dependency: transitive dependency: transitive
description: description:
@@ -131,6 +152,102 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.9.1" version: "1.9.1"
path_provider_linux:
dependency: transitive
description:
name: path_provider_linux
sha256: f7a1fe3a634fe7734c8d3f2766ad746ae2a2884abe22e241a8b301bf5cac3279
url: "https://pub.dev"
source: hosted
version: "2.2.1"
path_provider_platform_interface:
dependency: transitive
description:
name: path_provider_platform_interface
sha256: "88f5779f72ba699763fa3a3b06aa4bf6de76c8e5de842cf6f29e2e06476c2334"
url: "https://pub.dev"
source: hosted
version: "2.1.2"
path_provider_windows:
dependency: transitive
description:
name: path_provider_windows
sha256: bd6f00dbd873bfb70d0761682da2b3a2c2fccc2b9e84c495821639601d81afe7
url: "https://pub.dev"
source: hosted
version: "2.3.0"
platform:
dependency: transitive
description:
name: platform
sha256: "5d6b1b0036a5f331ebc77c850ebc8506cbc1e9416c27e59b439f917a902a4984"
url: "https://pub.dev"
source: hosted
version: "3.1.6"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02"
url: "https://pub.dev"
source: hosted
version: "2.1.8"
shared_preferences:
dependency: "direct main"
description:
name: shared_preferences
sha256: c3025c5534b01739267eb7d76959bbc25a6d10f6988e1c2a3036940133dd10bf
url: "https://pub.dev"
source: hosted
version: "2.5.5"
shared_preferences_android:
dependency: transitive
description:
name: shared_preferences_android
sha256: "93ae5884a9df5d3bb696825bceb3a17590754548b5d740eba51500afc8d088f5"
url: "https://pub.dev"
source: hosted
version: "2.4.26"
shared_preferences_foundation:
dependency: transitive
description:
name: shared_preferences_foundation
sha256: "4e7eaffc2b17ba398759f1151415869a34771ba11ebbccd1b0145472a619a64f"
url: "https://pub.dev"
source: hosted
version: "2.5.6"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
sha256: "580abfd40f415611503cae30adf626e6656dfb2f0cee8f465ece7b6defb40f2f"
url: "https://pub.dev"
source: hosted
version: "2.4.1"
shared_preferences_platform_interface:
dependency: transitive
description:
name: shared_preferences_platform_interface
sha256: "649dc798a33931919ea356c4305c2d1f81619ea6e92244070b520187b5140ef9"
url: "https://pub.dev"
source: hosted
version: "2.4.2"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
sha256: c49bd060261c9a3f0ff445892695d6212ff603ef3115edbb448509d407600019
url: "https://pub.dev"
source: hosted
version: "2.4.3"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
sha256: "94ef0f72b2d71bc3e700e025db3710911bd51a71cefb65cc609dd0d9a982e3c1"
url: "https://pub.dev"
source: hosted
version: "2.4.1"
sky_engine: sky_engine:
dependency: transitive dependency: transitive
description: flutter description: flutter
@@ -200,6 +317,22 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "15.2.0" version: "15.2.0"
web:
dependency: transitive
description:
name: web
sha256: "868d88a33d8a87b18ffc05f9f030ba328ffefba92d6c127917a2ba740f9cfe4a"
url: "https://pub.dev"
source: hosted
version: "1.1.1"
xdg_directories:
dependency: transitive
description:
name: xdg_directories
sha256: "7a3f37b05d989967cdddcbb571f1ea834867ae2faa29725fd085180e0883aa15"
url: "https://pub.dev"
source: hosted
version: "1.1.0"
sdks: sdks:
dart: ">=3.12.1 <4.0.0" dart: ">=3.12.1 <4.0.0"
flutter: ">=3.18.0-18.0.pre.54" flutter: ">=3.44.0"
+1
View File
@@ -9,6 +9,7 @@ environment:
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
shared_preferences: ^2.5.5
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: