-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
274 additions
and
48 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,39 +1,207 @@ | ||
<!-- | ||
This README describes the package. If you publish this package to pub.dev, | ||
this README's contents appear on the landing page for your package. | ||
<!-- markdownlint-disable MD033 MD041 --> | ||
<p align="center" style="margin-bottom: 0px;"> | ||
<img src="https://avatars2.githubusercontent.com/u/61839689?s=200&v=4" width="85px"> | ||
</p> | ||
|
||
For information about how to write a good package README, see the guide for | ||
[writing package pages](https://dart.dev/guides/libraries/writing-package-pages). | ||
<h1 align="center" style="margin-top: 0px; font-size: 4em;">Flutter Data</h1> | ||
|
||
For general information about developing packages, see the Dart guide for | ||
[creating packages](https://dart.dev/guides/libraries/create-library-packages) | ||
and the Flutter guide for | ||
[developing packages and plugins](https://flutter.dev/developing-packages). | ||
--> | ||
[![tests](https://img.shields.io/github/actions/workflow/status/flutterdata/flutter_data/test.yml?branch=master)](https://github.com/flutterdata/flutter_data/actions) [![codecov](https://codecov.io/gh/flutterdata/flutter_data/branch/master/graph/badge.svg)](https://codecov.io/gh/flutterdata/flutter_data) [![pub.dev](https://img.shields.io/pub/v/flutter_data?label=pub.dev&labelColor=333940&logo=dart)](https://pub.dev/packages/flutter_data) [![license](https://img.shields.io/github/license/flutterdata/flutter_data?color=%23007A88&labelColor=333940&logo=mit)](https://github.com/flutterdata/flutter_data/blob/master/LICENSE) | ||
|
||
TODO: Put a short description of the package here that helps potential users | ||
know whether this package might be useful for them. | ||
|
||
# panda_sync | ||
|
||
`panda_sync` is a Dart library designed to facilitate the development of offline-first applications | ||
using Flutter. It provides seamless data synchronization between local storage and a remote server, | ||
ensuring your app remains functional even without internet connectivity. The | ||
|
||
## Features | ||
|
||
TODO: List what your package can do. Maybe include images, gifs, or videos. | ||
- **Offline-First Functionality**: Automatically handles data synchronization when the device is online. | ||
- **Local Storage with Isar**: Utilizes Isar, a high-performance NoSQL database, for local data storage. | ||
- **Network Request Management**: Uses Dio for making network requests and managing connectivity states. | ||
- **Type Registration**: Enforces type registration for data serialization and deserialization. | ||
- **Automatic Retry**: Automatically retries failed network requests when the device regains connectivity. | ||
- **Customizable**: Allows customization of request handling and data processing. | ||
|
||
## Getting started | ||
## Installation | ||
|
||
TODO: List prerequisites and provide or point to information on how to | ||
start using the package. | ||
Add the following dependency to your `pubspec.yaml` file: | ||
|
||
```yaml | ||
|
||
dependencies: | ||
panda_sync: ^1.0.0 # Add the latest version | ||
``` | ||
## Usage | ||
### Step 1: Initialize Local Storage | ||
Before using the library, initialize Isar core: | ||
```dart | ||
|
||
import 'package:panda_sync/panda_sync.dart'; | ||
|
||
void main() async { | ||
await OfflineFirstLocalStorageInit.initialize(); | ||
runApp(MyApp()); | ||
} | ||
``` | ||
|
||
## Step 2: Extend your data models with `Identifieble` and implement static `fromJson` and `toJson` methods. | ||
|
||
|
||
```dart | ||
import 'package:panda_sync/panda_sync.dart'; | ||
import 'package:json_annotation/json_annotation.dart'; | ||
part 'task_model.g.dart'; | ||
//This example uses json_serializable but this is not mandatory | ||
@JsonSerializable() | ||
class Task extends Identifiable{ | ||
@override | ||
int id; | ||
... | ||
factory Task.fromJson(Map<String, dynamic> json) => _$TaskFromJson(json); | ||
static Map<String, dynamic> taskToJson(Task task) => _$TaskToJson(task); | ||
} | ||
``` | ||
|
||
### Step 3: Register the types your app will use: | ||
|
||
```dart | ||
import 'package:panda_sync/panda_sync.dart'; | ||
import 'model/task_model.dart'; // Import your model | ||
void main() async { | ||
await OfflineFirstLocalStorageInit.initialize(); | ||
TypeRegistry.register<Task>('TaskBox', Task.taskToJson, Task.fromJson); | ||
runApp(MyApp()); | ||
} | ||
``` | ||
|
||
TODO: Include short and useful examples for package users. Add longer examples | ||
to `/example` folder. | ||
### Step 4: Create an Instance of `OfflineFirstClient` | ||
|
||
Create an instance of OfflineFirstClient: | ||
|
||
```dart | ||
final OfflineFirstClient offlineFirstClient = | ||
OfflineFirstClient(); | ||
``` | ||
|
||
### Step 5: Use the `OfflineFirstClient` as you would use any other Http client | ||
|
||
Here's how to use the library in a service class: | ||
|
||
```dart | ||
const like = 'sample'; | ||
import 'package:dio/dio.dart'; | ||
import 'package:panda_sync/panda_sync.dart'; | ||
import 'model/task_model.dart'; | ||
class TodoService { | ||
static final TodoService _instance = TodoService._internal(); | ||
static final OfflineFirstClient offlineFirstClient = | ||
OfflineFirstClient(); | ||
factory TodoService() { | ||
return _instance; | ||
} | ||
TodoService._internal(); | ||
Future<List<Task>> getAllTasks() async { | ||
try { | ||
Response<List<Task>> response = await offlineFirstClient.getList<Task>('http://10.0.2.2:8080/api/tasks'); | ||
return response.data!; | ||
} catch (e) { | ||
throw Exception('Failed to load tasks: $e'); | ||
} | ||
} | ||
Future<Task> getTaskById(int id) async { | ||
try { | ||
Response<Task> response = await offlineFirstClient.get<Task>('http://10.0.2.2:8080/api/tasks/$id'); | ||
return response.data!; | ||
} catch (e) { | ||
throw Exception('Failed to get task: $e'); | ||
} | ||
} | ||
Future<Task> createTask(Task task) async { | ||
try { | ||
var postResponse = await offlineFirstClient.post<Task>('http://10.0.2.2:8080/api/tasks', task); | ||
return postResponse.data!; | ||
} catch (e) { | ||
throw Exception('Failed to create task: $e'); | ||
} | ||
} | ||
Future<Task> updateTask(Task task) async { | ||
try { | ||
var putResponse = await offlineFirstClient.put<Task>('http://10.0.2.2:8080/api/tasks/${task.id}', task); | ||
return putResponse.data!; | ||
} catch (e) { | ||
throw Exception('Failed to update task: $e'); | ||
} | ||
} | ||
Future<void> deleteTask(int id) async { | ||
try { | ||
Task taskToDelete = await getTaskById(id); | ||
await offlineFirstClient.delete<Task>('http://10.0.2.2:8080/api/tasks/$id',taskToDelete); | ||
} catch (e) { | ||
throw Exception('Failed to delete task: $e'); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Additional information | ||
# API Documentation | ||
|
||
- **OfflineFirstClient** | ||
|
||
- `OfflineFirstClient()`: Creates an instance of **OfflineFirstClient**. | ||
- `Future<Response<T>> get<T extends Identifiable>(String url, {Map<String, dynamic>? queryParameters}):` Makes a GET request. | ||
- `Future<Response<T>> post<T extends Identifiable>(String url, T data, {Map<String, dynamic>? queryParameters}):` Makes a POST request. | ||
- `Future<Response<T>> put<T extends Identifiable>(String url, T data, {Map<String, dynamic>? queryParameters}):` Makes a PUT request. | ||
- `Future<Response<T>> delete<T extends Identifiable>(String url, T data, {Map<String, dynamic>? queryParameters}):` Makes a DELETE request. | ||
- `Future<Response<List<T>>> getList<T extends Identifiable>(String url, {Map<String, dynamic>? queryParameters}):` Makes a GET request for a list of items. | ||
- `Future<Response<List<T>>> postList<T extends Identifiable>(String url, List<T> dataList, {Map<String, dynamic>? queryParameters}):` Makes a POST request for a list of items. | ||
- `Future<Response<List<T>>> putList<T extends Identifiable>(String url, List<T> dataList, {Map<String, dynamic>? queryParameters}):` Makes a PUT request for a list of items. | ||
- `Future<Response<List<T>>> deleteList<T extends Identifiable>(String url, List<T> dataList, {Map<String, dynamic>? queryParameters}):` Makes a DELETE request for a list of items. | ||
|
||
|
||
# Contributing | ||
|
||
We welcome contributions! Please follow these steps to contribute: | ||
|
||
1. Fork the repository. | ||
2. Create a new branch (`git checkout -b my-feature-branch`). | ||
3. Make your changes. | ||
4. Commit your changes (`git commit -am 'Add new feature'`). | ||
5. Push to the branch (git push origin my-feature-branch). | ||
6. Create a Pull Request. | ||
|
||
# License | ||
|
||
This project is licensed under the MIT License - see the LICENSE file for details. | ||
|
||
# Acknowledgements | ||
|
||
- [Isar Database](https://github.com/isar/isar) | ||
- [Dio HTTP Client](https://github.com/cfug/dio) | ||
- [Connectivity Plus](https://github.com/fluttercommunity/plus_plugins/tree/main/packages/connectivity_plus/connectivity_plus) | ||
|
||
# Contact | ||
|
||
TODO: Tell users more about the package: where to find more information, how to | ||
contribute to the package, how to file issues, what response they can expect | ||
from the package authors, and more. | ||
For any questions or suggestions, feel free to open an issue or contact any maintainer. |
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,16 +0,0 @@ | ||
# panda_sync_todo | ||
|
||
A new Flutter project. | ||
|
||
## Getting Started | ||
|
||
This project is a starting point for a Flutter application. | ||
|
||
A few resources to get you started if this is your first Flutter project: | ||
|
||
- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab) | ||
- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook) | ||
|
||
For help getting started with Flutter development, view the | ||
[online documentation](https://docs.flutter.dev/), which offers tutorials, | ||
samples, guidance on mobile development, and a full API reference. | ||
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 |
---|---|---|
|
@@ -21,4 +21,4 @@ dev_dependencies: | |
sdk: flutter | ||
isar_generator: ^3.1.0+1 | ||
mockito: ^5.4.4 | ||
test: any | ||
test: any |
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