Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/placeholder.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 6 additions & 3 deletions lib/core/di/injection.config.dart

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

20 changes: 17 additions & 3 deletions lib/features/todo/data/datasources/todo_local_datasource.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import 'package:injectable/injectable.dart';
import 'package:shared_preferences/shared_preferences.dart';

// This class is a stub for workshop participants to implement
@injectable
class TodoLocalDataSource {
final SharedPreferences sharedPreferences;
// ignore: unused_field
final String _todoKey = 'todos';
final String _todoFavouriteKey = 'favourite_todos';

TodoLocalDataSource(this.sharedPreferences);

Future<List<String>> getFavouriteTodos() async {
try {
return sharedPreferences.getStringList(_todoFavouriteKey) ?? [];
} catch (e) {
return [];
}
}

Future<void> setFavouriteTodos(List<String> todoIds) async {
try {
await sharedPreferences.setStringList(_todoFavouriteKey, todoIds);
} catch (e) {
return;
}
}
}
57 changes: 53 additions & 4 deletions lib/features/todo/data/datasources/todo_remote_datasource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import '../models/todo_dto.dart';
@injectable
class TodoRemoteDataSource {
final http.Client _client;
// TODO: Replace this with the URL we provide you
final String baseUrl = 'https://6825aa0f0f0188d7e72ddd9a.mockapi.io/api/v1';
final String baseUrl = 'https://68346e5b464b49963602ccb5.mockapi.io';

TodoRemoteDataSource(this._client);

Expand All @@ -32,9 +31,14 @@ class TodoRemoteDataSource {
}
}

Future<TodoDTO> addTodo(String title) async {
Future<TodoDTO> addTodo(String title, String? description, String? imageUrl) async {
try {
final newTodo = {'title': title, 'createdAtSeconds': DateTime.now().millisecondsSinceEpoch ~/ 1000};
final newTodo = {
'title': title,
'description': description,
'imageUrl': imageUrl,
'createdAtSeconds': DateTime.now().millisecondsSinceEpoch ~/ 1000,
};

log('Sending todo: ${json.encode(newTodo)}');

Expand All @@ -61,6 +65,51 @@ class TodoRemoteDataSource {
}
}

Future<void> deleteTodo(String id) async {
try {
final response = await _client.delete(Uri.parse('$baseUrl/todo/$id'));

log('Response status: ${response.statusCode}');
log('Response body: ${response.body}');

if (response.statusCode != 200) {
throw Exception('Failed to delete todo: ${response.statusCode}');
}
} catch (e) {
log('Error deleting todo: $e');
throw Exception('Failed to delete todo: $e');
}
}

Future<TodoDTO> updateTodoStatus(String id, bool isDone) async {
try {
final newTodo = {'isDone': isDone};

log('Updating todo status: ${json.encode(newTodo)}');

final response = await _client.patch(
Uri.parse('$baseUrl/todo/$id'),
headers: {'Content-Type': 'application/json'},
body: json.encode(newTodo),
);

log('Response status: ${response.statusCode}');
log('Response body: ${response.body}');

if (response.statusCode == 201 || response.statusCode == 200) {
final Map<String, dynamic> responseData = json.decode(response.body);

// If the API response is not in the expected format, transform it
return TodoDTO.fromJson(responseData);
} else {
throw Exception('Failed to update todo: ${response.statusCode}');
}
} catch (e) {
log('Error updating todo: $e');
throw Exception('Failed to update todo: $e');
}
}

// For local client-side ID generation
String generateId() {
return const Uuid().v4();
Expand Down
3 changes: 3 additions & 0 deletions lib/features/todo/data/models/todo_dto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class TodoDTO with _$TodoDTO {
const factory TodoDTO({
required String id,
required String title,
String? description,
String? imageUrl,
bool? isDone,
int? createdAtSeconds,
}) = _TodoDTO;

Expand Down
92 changes: 88 additions & 4 deletions lib/features/todo/data/models/todo_dto.freezed.dart

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

6 changes: 6 additions & 0 deletions lib/features/todo/data/models/todo_dto.g.dart

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

Loading