Skip to content

Commit ca70e0d

Browse files
committed
🎉 Add Offline Saving & Delete
1 parent 3772b2a commit ca70e0d

File tree

5 files changed

+71
-10
lines changed

5 files changed

+71
-10
lines changed

CHANGELOG.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
## [0.0.3] - 2024-12-08
2+
3+
### Improvements
4+
- Added Offline Handling for `save` and `delete` methods
5+
- Change `CHANGELOG.md` to Descending ordering.
6+
### Comments
7+
- Thanks to all those who take time to like this package.
8+
- Am actively looking for contributors of this package, as a solo contributor I can't do much.
9+
- You can contribute either in the documentation, review, test, code and maintenance.
10+
11+
## [0.0.2] - 2024-11-23
12+
13+
### Improvements
14+
- Fixed Flutter Data Documentation Link
15+
- Fixed Bug in the Adapter's `sendRequest`
16+
117
## [0.0.1] - 2024-11-23
218

319
### Added
@@ -41,9 +57,3 @@
4157
- This is the initial release and should be considered beta
4258
- Feedback and contributions are welcome
4359
- Testing in production environments is recommended before critical usage
44-
45-
## [0.0.2] - 2024-11-23
46-
47-
### Improvements
48-
- Fixed Flutter Data Documentation Link
49-
- Fixed Bug in the Adapter's `sendRequest`

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Add this to your package's pubspec.yaml file:
2727

2828
```yaml
2929
dependencies:
30-
appwrite_offline: ^0.0.1
30+
appwrite_offline: ^0.0.3
3131
```
3232
3333
## Setup

lib/adapters/appwrite.dart

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'dart:async';
44
import 'dart:convert';
55

66
import 'package:appwrite_offline/config.dart';
7+
import 'package:appwrite_offline/extensions/framework.dart';
78
import 'package:appwrite_offline/models.dart';
89
import 'package:flutter_data/flutter_data.dart';
910
import 'package:appwrite/appwrite.dart';
@@ -297,9 +298,46 @@ mixin AppwriteAdapter<T extends DataModel<T>> on RemoteAdapter<T> {
297298
statusCode: 200,
298299
);
299300
return onSuccess(data, label);
300-
} on AppwriteException catch (e) {
301+
} on AppwriteException catch (error) {
301302
// AppwriteException thrown
302-
return onError(DataException(e), label);
303+
// Check Offline Error
304+
if (isOfflineError(error)) {
305+
// queue a new operation if:
306+
// - this is a network error and we're offline
307+
// - the request was not a find
308+
if (method != DataRequestMethod.GET) {
309+
OfflineOperation<T>(
310+
httpRequest: '${method.toShortString()} $uri',
311+
label: label,
312+
timestamp: DateTime.now().millisecondsSinceEpoch,
313+
body: body?.toString(),
314+
headers: headers,
315+
onSuccess: onSuccess as OnSuccessGeneric<T>,
316+
onError: onError as OnErrorGeneric<T>,
317+
adapter: this as RemoteAdapter<T>,
318+
).add();
319+
}
320+
321+
// wrap error in an OfflineException
322+
final offlineException = OfflineException(error: error);
323+
324+
// call error handler but do not return it
325+
// (this gives the user the chance to present
326+
// a UI element to retry fetching, for example)
327+
onError(offlineException, label);
328+
329+
// instead return a fallback model from local storage
330+
switch (label.kind) {
331+
case 'findAll':
332+
return findAll(remote: false) as Future<R?>;
333+
case 'findOne':
334+
case 'save':
335+
return label.model as R?;
336+
default:
337+
return null;
338+
}
339+
}
340+
return onError(DataException(error), label);
303341
} catch (e) {
304342
// Other Exception thrown
305343
return onError(DataException(e), label);

lib/extensions/framework.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'dart:async';
2+
3+
import 'package:flutter_data/flutter_data.dart';
4+
5+
extension ToStringX on DataRequestMethod {
6+
String toShortString() => toString().split('.').last;
7+
}
8+
9+
typedef OnSuccessGeneric<R> = FutureOr<R?> Function(
10+
DataResponse response, DataRequestLabel label);
11+
12+
typedef OnErrorGeneric<R> = FutureOr<R?> Function(
13+
DataException e, DataRequestLabel label);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: appwrite_offline
22
description: "A Flutter Data adapter for Appwrite that provides offline support, real-time updates, and seamless integration with Flutter Data's powerful features."
3-
version: 0.0.2
3+
version: 0.0.3
44
homepage: https://github.com/cybroidtech/appwrite_offline
55
repository: https://github.com/cybroidtech/appwrite_offline
66
issue_tracker: https://github.com/cybroidtech/appwrite_offline/issues

0 commit comments

Comments
 (0)