feat: add query and infinite query disk caching support

This commit is contained in:
Kingkor Roy Tirtho
2022-12-15 15:43:40 +06:00
parent 51cad70954
commit 074175ed39
25 changed files with 689 additions and 19 deletions
@@ -4,3 +4,9 @@ connectivity_plus_linux=/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connec
connectivity_plus_macos=/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_macos-1.2.4/
connectivity_plus_web=/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_web-1.2.5/
connectivity_plus_windows=/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_windows-1.2.2/
path_provider=/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/path_provider-2.0.11/
path_provider_android=/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/path_provider_android-2.0.22/
path_provider_ios=/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/path_provider_ios-2.0.11/
path_provider_linux=/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/path_provider_linux-2.1.7/
path_provider_macos=/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/path_provider_macos-2.0.6/
path_provider_windows=/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/path_provider_windows-2.1.3/
@@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"connectivity_plus","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus-2.3.9/","native_build":true,"dependencies":[]}],"android":[{"name":"connectivity_plus","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus-2.3.9/","native_build":true,"dependencies":[]}],"macos":[{"name":"connectivity_plus_macos","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_macos-1.2.4/","native_build":true,"dependencies":[]}],"linux":[{"name":"connectivity_plus_linux","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_linux-1.3.1/","native_build":false,"dependencies":[]}],"windows":[{"name":"connectivity_plus_windows","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_windows-1.2.2/","native_build":true,"dependencies":[]}],"web":[{"name":"connectivity_plus_web","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_web-1.2.5/","dependencies":[]}]},"dependencyGraph":[{"name":"connectivity_plus","dependencies":["connectivity_plus_linux","connectivity_plus_macos","connectivity_plus_web","connectivity_plus_windows"]},{"name":"connectivity_plus_linux","dependencies":[]},{"name":"connectivity_plus_macos","dependencies":[]},{"name":"connectivity_plus_web","dependencies":[]},{"name":"connectivity_plus_windows","dependencies":[]}],"date_created":"2022-12-14 12:54:36.553871","version":"3.3.8"}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"connectivity_plus","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus-2.3.9/","native_build":true,"dependencies":[]},{"name":"path_provider_ios","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/path_provider_ios-2.0.11/","native_build":true,"dependencies":[]}],"android":[{"name":"connectivity_plus","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus-2.3.9/","native_build":true,"dependencies":[]},{"name":"path_provider_android","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/path_provider_android-2.0.22/","native_build":true,"dependencies":[]}],"macos":[{"name":"connectivity_plus_macos","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_macos-1.2.4/","native_build":true,"dependencies":[]},{"name":"path_provider_macos","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/path_provider_macos-2.0.6/","native_build":true,"dependencies":[]}],"linux":[{"name":"connectivity_plus_linux","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_linux-1.3.1/","native_build":false,"dependencies":[]},{"name":"path_provider_linux","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/path_provider_linux-2.1.7/","native_build":false,"dependencies":[]}],"windows":[{"name":"connectivity_plus_windows","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_windows-1.2.2/","native_build":true,"dependencies":[]},{"name":"path_provider_windows","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/path_provider_windows-2.1.3/","native_build":false,"dependencies":[]}],"web":[{"name":"connectivity_plus_web","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_web-1.2.5/","dependencies":[]}]},"dependencyGraph":[{"name":"connectivity_plus","dependencies":["connectivity_plus_linux","connectivity_plus_macos","connectivity_plus_web","connectivity_plus_windows"]},{"name":"connectivity_plus_linux","dependencies":[]},{"name":"connectivity_plus_macos","dependencies":[]},{"name":"connectivity_plus_web","dependencies":[]},{"name":"connectivity_plus_windows","dependencies":[]},{"name":"path_provider","dependencies":["path_provider_android","path_provider_ios","path_provider_linux","path_provider_macos","path_provider_windows"]},{"name":"path_provider_android","dependencies":[]},{"name":"path_provider_ios","dependencies":[]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_macos","dependencies":[]},{"name":"path_provider_windows","dependencies":[]}],"date_created":"2022-12-15 14:54:08.206645","version":"3.3.8"}
@@ -20,5 +20,10 @@ public final class GeneratedPluginRegistrant {
} catch(Exception e) {
Log.e(TAG, "Error registering plugin connectivity_plus, dev.fluttercommunity.plus.connectivity.ConnectivityPlugin", e);
}
try {
flutterEngine.getPlugins().add(new io.flutter.plugins.pathprovider.PathProviderPlugin());
} catch(Exception e) {
Log.e(TAG, "Error registering plugin path_provider_android, io.flutter.plugins.pathprovider.PathProviderPlugin", e);
}
}
}
@@ -12,10 +12,17 @@
@import connectivity_plus;
#endif
#if __has_include(<path_provider_ios/FLTPathProviderPlugin.h>)
#import <path_provider_ios/FLTPathProviderPlugin.h>
#else
@import path_provider_ios;
#endif
@implementation GeneratedPluginRegistrant
+ (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry {
[ConnectivityPlusPlugin registerWithRegistrar:[registry registrarForPlugin:@"ConnectivityPlusPlugin"]];
[FLTPathProviderPlugin registerWithRegistrar:[registry registrarForPlugin:@"FLTPathProviderPlugin"]];
}
@end
@@ -0,0 +1,99 @@
import 'dart:convert';
import 'package:fl_query/fl_query.dart';
import 'package:fl_query_example/components/query_disk_cache.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
final infiniteQueryDiskCacheExampleQuery =
InfiniteQueryJob<List<User>, void, int>(
queryKey: 'infiniteQueryDiskCacheExampleQuery',
initialParam: 0,
getNextPageParam: (lastPage, lastParam) {
if (lastPage.length < 5) return null;
return lastParam + 5;
},
getPreviousPageParam: (firstPage, firstParam) {
if (firstParam == 0) return null;
return firstParam - 5;
},
serialize: (data) {
return jsonEncode(data.map((user) => user.toJson()).toList());
},
deserialize: (raw) {
return List.from(jsonDecode(raw))
.map((user) => User.fromJson(user))
.toList();
},
serializePageParam: (param) => param.toString(),
deserializePageParam: (rawParam) => int.parse(rawParam),
task: (_, pageParam, __) async {
final res = await http.get(
Uri.parse(
"https://jsonplaceholder.typicode.com/users?_start=$pageParam&_end=${pageParam + 5}"),
);
final body = List.from(jsonDecode(res.body))
.map((user) => User.fromJson(user))
.toList()
..shuffle();
if (pageParam == 0) await Future.delayed(const Duration(seconds: 5));
return body;
},
);
class InfiniteQueryDiskCacheExample extends StatefulWidget {
const InfiniteQueryDiskCacheExample({Key? key}) : super(key: key);
@override
State<InfiniteQueryDiskCacheExample> createState() =>
_InfiniteQueryDiskCacheExampleState();
}
class _InfiniteQueryDiskCacheExampleState
extends State<InfiniteQueryDiskCacheExample> {
@override
Widget build(BuildContext context) {
return ListTile(
title: const Text("Infinite Query Disk Cache Example"),
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
return Scaffold(
appBar: AppBar(
title: const Text("Infinite Query Disk Cache Example")),
body: InfiniteQueryBuilder<List<User>, void, int>(
job: infiniteQueryDiskCacheExampleQuery,
externalData: null,
builder: (context, query) {
final data =
query.pages.expand((page) => page!.toList()).toList();
return Scaffold(
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.download_rounded),
onPressed: () {
if (query.hasNextPage == true) {
query.fetchNextPage();
}
},
),
body: ListView.builder(
itemCount: data.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(data[index].name!),
subtitle: Text(data[index].email!),
);
},
),
);
},
),
);
},
),
);
},
);
}
}
@@ -0,0 +1,79 @@
import 'dart:convert';
import 'package:fl_query/fl_query.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
final queryDiskCacheExampleQuery = QueryJob<List<User>, void>(
queryKey: 'queryDiskCacheExampleQuery',
serialize: (data) {
return jsonEncode(data.map((user) => user.toJson()).toList());
},
deserialize: (raw) {
return List.from(jsonDecode(raw))
.map((user) => User.fromJson(user))
.toList();
},
task: (_, __) async {
final res =
await http.get(Uri.parse("https://jsonplaceholder.typicode.com/users"));
final body = List.from(jsonDecode(res.body))
.map((user) => User.fromJson(user))
.toList()
..shuffle();
await Future.delayed(const Duration(seconds: 5));
return body;
},
);
class User {
int? id;
String? name;
String? username;
String? email;
User({this.id, this.name, this.username, this.email});
User.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
username = json['username'];
email = json['email'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = {};
data['id'] = id;
data['name'] = name;
data['username'] = username;
data['email'] = email;
return data;
}
}
class QueryDiskCacheExample extends StatelessWidget {
const QueryDiskCacheExample({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return QueryBuilder<List<User>, void>(
job: queryDiskCacheExampleQuery,
externalData: null,
builder: (context, query) {
if (!query.hasData) {
return const Center(child: CircularProgressIndicator());
}
return ListView.builder(
shrinkWrap: true,
itemCount: query.data?.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(query.data![index].name!),
subtitle: Text(query.data![index].email!),
);
},
);
},
);
}
}
+7 -1
View File
@@ -2,15 +2,19 @@ import 'package:fl_query_example/components/basic_infinite_query.dart';
import 'package:fl_query_example/components/basic_mutation.dart';
import 'package:fl_query/fl_query.dart';
import 'package:fl_query_example/components/basic_query.dart';
import 'package:fl_query_example/components/infinite_query_disk_cache.dart';
import 'package:fl_query_example/components/lazy_query.dart';
import 'package:fl_query_example/components/mutation_variable_key.dart';
import 'package:fl_query_example/components/query_disk_cache.dart';
import 'package:fl_query_example/components/query_external_data.dart';
import 'package:fl_query_example/components/query_previous_data.dart';
import 'package:fl_query_example/components/query_variable_key.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() {
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await initializeFlQuery(cacheKey: "example");
debugRepaintRainbowEnabled = true;
runApp(const MyApp());
}
@@ -70,6 +74,8 @@ class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
);
},
),
const QueryDiskCacheExample(),
const InfiniteQueryDiskCacheExample(),
const Divider(),
const BasicMutationExample(),
const MutationVariableKeyExample(),
@@ -6,7 +6,9 @@ import FlutterMacOS
import Foundation
import connectivity_plus_macos
import path_provider_macos
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
ConnectivityPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
}
+100 -2
View File
@@ -120,6 +120,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1"
file:
dependency: transitive
description:
name: file
url: "https://pub.dartlang.org"
source: hosted
version: "6.1.4"
fl_query:
dependency: "direct main"
description:
@@ -149,6 +156,20 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
hive:
dependency: transitive
description:
name: hive
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.3"
hive_flutter:
dependency: transitive
description:
name: hive_flutter
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
http:
dependency: "direct main"
description:
@@ -219,6 +240,55 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.2"
path_provider:
dependency: transitive
description:
name: path_provider
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.11"
path_provider_android:
dependency: transitive
description:
name: path_provider_android
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.22"
path_provider_ios:
dependency: transitive
description:
name: path_provider_ios
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.11"
path_provider_linux:
dependency: transitive
description:
name: path_provider_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.7"
path_provider_macos:
dependency: transitive
description:
name: path_provider_macos
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.6"
path_provider_platform_interface:
dependency: transitive
description:
name: path_provider_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.5"
path_provider_windows:
dependency: transitive
description:
name: path_provider_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
petitparser:
dependency: transitive
description:
@@ -226,6 +296,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "5.0.0"
platform:
dependency: transitive
description:
name: platform
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.0"
plugin_platform_interface:
dependency: transitive
description:
@@ -233,13 +310,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.2"
process:
dependency: transitive
description:
name: process
url: "https://pub.dartlang.org"
source: hosted
version: "4.2.4"
queue:
dependency: transitive
description:
name: queue
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.0+1"
version: "3.1.0+2"
sky_engine:
dependency: transitive
description: flutter
@@ -315,6 +399,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.2"
win32:
dependency: transitive
description:
name: win32
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.2"
xdg_directories:
dependency: transitive
description:
name: xdg_directories
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.0+2"
xml:
dependency: transitive
description:
@@ -324,4 +422,4 @@ packages:
version: "6.1.0"
sdks:
dart: ">=2.17.1 <3.0.0"
flutter: ">=1.22.0"
flutter: ">=3.0.0"