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
23 changes: 23 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
PODS:
- Flutter (1.0.0)
- shared_preferences_foundation (0.0.1):
- Flutter
- FlutterMacOS

DEPENDENCIES:
- Flutter (from `Flutter`)
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)

EXTERNAL SOURCES:
Flutter:
:path: Flutter
shared_preferences_foundation:
:path: ".symlinks/plugins/shared_preferences_foundation/darwin"

SPEC CHECKSUMS:
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
shared_preferences_foundation: 9e1978ff2562383bd5676f64ec4e9aa8fa06a6f7

PODFILE CHECKSUM: 4305caec6b40dde0ae97be1573c53de1882a07e5

COCOAPODS: 1.16.2
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.

21 changes: 21 additions & 0 deletions lib/features/todo/data/datasources/todo_local_datasource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,25 @@ class TodoLocalDataSource {
final String _todoKey = 'todos';

TodoLocalDataSource(this.sharedPreferences);

/// Toggles the favourite status of a todo by its [id].
/// Returns the new favourite status.
Future<bool> toggleFavourite(String id) async {
final favourites = sharedPreferences.getStringList('favourite_todos') ?? [];
if (favourites.contains(id)) {
favourites.remove(id);
await sharedPreferences.setStringList('favourite_todos', favourites);
return false;
} else {
favourites.add(id);
await sharedPreferences.setStringList('favourite_todos', favourites);
return true;
}
}

/// Checks if a todo with [id] is favourited.
bool isFavourite(String id) {
final favourites = sharedPreferences.getStringList('favourite_todos') ?? [];
return favourites.contains(id);
}
}
51 changes: 46 additions & 5 deletions lib/features/todo/data/datasources/todo_remote_datasource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import '../models/todo_dto.dart';
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://683469d6464b49963602b8e8.mockapi.io';

TodoRemoteDataSource(this._client);

Future<List<TodoDTO>> getTodos() async {
try {
final response = await _client.get(Uri.parse('$baseUrl/todos'));
final response = await _client.get(Uri.parse('$baseUrl/todo'));

if (response.statusCode == 200) {
final List<dynamic> jsonList = json.decode(response.body);
Expand All @@ -32,14 +32,18 @@ class TodoRemoteDataSource {
}
}

Future<TodoDTO> addTodo(String title) async {
Future<TodoDTO> addTodo(String title, String description) async {
try {
final newTodo = {'title': title, 'createdAtSeconds': DateTime.now().millisecondsSinceEpoch ~/ 1000};
final newTodo = {
'title': title,
'description': description,
'createdAtSeconds': DateTime.now().millisecondsSinceEpoch ~/ 1000,
};
Comment on lines +37 to +41

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: You could have request body DTO with a to json method to avoid creating/parsing JSON directly here


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

final response = await _client.post(
Uri.parse('$baseUrl/todos'),
Uri.parse('$baseUrl/todo'),
headers: {'Content-Type': 'application/json'},
body: json.encode(newTodo),
);
Expand All @@ -61,6 +65,43 @@ class TodoRemoteDataSource {
}
}

Future<TodoDTO> toggleTodoCompletion(String todoId, bool isDone) async {
try {
final response = await _client.put(
Uri.parse('$baseUrl/todo/$todoId'),
headers: {'Content-Type': 'application/json'},
body: json.encode({'isDone': isDone}),
);

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 create todo: ${response.statusCode}');
}
} catch (e) {
log('Error toggling todo completion: $e');
throw Exception('Failed to toggle todo completion: $e');
}
}

Future<void> deleteTodo(String todoId) async {
try {
final response = await _client.delete(
Uri.parse('$baseUrl/todo/$todoId'),
headers: {'Content-Type': 'application/json'},
);
if (response.statusCode != 200 && response.statusCode != 204) {
throw Exception('Failed to delete todo: ${response.statusCode}');
}
} catch (e) {
log('Error deleting todo: $e');
throw Exception('Failed to delete todo: $e');
}
}

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

factory TodoDTO.fromJson(Map<String, dynamic> json) => _$TodoDTOFromJson(json);
factory TodoDTO.fromJson(Map<String, dynamic> json) =>
_$TodoDTOFromJson(json);
}
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