From ef8bb74e7ea345549591126fbacc6e1d82209e09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Fri, 28 Jun 2024 00:50:31 +0100 Subject: [PATCH] feat: examples/alice_objectbox --- examples/alice_objectbox/lib/main.dart | 216 +++++++++++++----- examples/alice_objectbox/pubspec.yaml | 6 +- .../alice_objectbox/lib/alice_objectbox.dart | 8 +- 3 files changed, 172 insertions(+), 58 deletions(-) diff --git a/examples/alice_objectbox/lib/main.dart b/examples/alice_objectbox/lib/main.dart index b7350b81..c6bc78ce 100644 --- a/examples/alice_objectbox/lib/main.dart +++ b/examples/alice_objectbox/lib/main.dart @@ -1,80 +1,190 @@ +import 'package:alice_http/alice_http_adapter.dart'; +import 'package:alice_http/alice_http_extensions.dart'; +import 'package:alice_objectbox/alice_objectbox.dart'; import 'package:alice_objectbox/alice_store.dart'; import 'package:flutter/material.dart'; - -/// Provides access to the ObjectBox Store throughout the app. -late AliceStore aliceStore; +import 'package:http/http.dart' as http; Future main() async { /// This is required so ObjectBox can get the application directory /// to store the database in. WidgetsFlutterBinding.ensureInitialized(); - aliceStore = await AliceStore.create(); + runApp(MyApp( + store: await AliceStore.create(), + )); +} + +class MyApp extends StatefulWidget { + const MyApp({ + super.key, + required this.store, + }); + + final AliceStore store; - runApp(const MyApp()); + @override + State createState() => _MyAppState(); } -class MyApp extends StatelessWidget { - const MyApp({super.key}); +class _MyAppState extends State { + late final AliceHttpAdapter _aliceHttpAdapter = AliceHttpAdapter(); + + late final AliceObjectBox _alice = AliceObjectBox( + store: widget.store, + showNotification: true, + showInspectorOnShake: true, + maxCallsCount: 1000, + )..addAdapter(_aliceHttpAdapter); - // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( - title: 'Flutter Demo', - theme: ThemeData( - colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), - useMaterial3: true, + navigatorKey: _alice.getNavigatorKey(), + debugShowCheckedModeBanner: false, + home: Scaffold( + appBar: AppBar( + title: const Text('Alice + ObjectBox + HTTP - Example'), + ), + body: Container( + padding: const EdgeInsets.all(16), + child: ListView( + children: [ + const SizedBox(height: 8), + const Text( + style: TextStyle(fontSize: 14), + 'Welcome to example of Alice Http Inspector. ' + 'Click buttons below to generate sample data.', + ), + ElevatedButton( + onPressed: _runHttpHttpRequests, + child: const Text( + 'Run http/http HTTP Requests', + ), + ), + const SizedBox(height: 8), + const Text( + style: TextStyle(fontSize: 14), + 'After clicking on buttons above, you should receive notification.' + ' Click on it to show inspector. ' + 'You can also shake your device or click button below.', + ), + ElevatedButton( + onPressed: _runHttpInspector, + child: const Text( + 'Run HTTP Inspector', + ), + ) + ], + ), + ), ), - home: const MyHomePage(title: 'Flutter Demo Home Page'), ); } -} -class MyHomePage extends StatefulWidget { - const MyHomePage({super.key, required this.title}); + void _runHttpHttpRequests() async { + final Map body = { + 'title': 'foo', + 'body': 'bar', + 'userId': '1' + }; - final String title; + http + .post(Uri.https('jsonplaceholder.typicode.com', '/posts'), body: body) + .interceptWithAlice(_aliceHttpAdapter, body: body); - @override - State createState() => _MyHomePageState(); -} + http + .get(Uri.https('jsonplaceholder.typicode.com', '/posts')) + .interceptWithAlice(_aliceHttpAdapter); + + http + .put(Uri.https('jsonplaceholder.typicode.com', '/posts/1'), body: body) + .interceptWithAlice(_aliceHttpAdapter, body: body); + + http + .patch( + Uri.https('jsonplaceholder.typicode.com', '/posts/1'), + body: body, + ) + .interceptWithAlice(_aliceHttpAdapter, body: body); + + http + .delete(Uri.https('jsonplaceholder.typicode.com', '/posts/1')) + .interceptWithAlice(_aliceHttpAdapter, body: body); + + http + .get(Uri.https('jsonplaceholder.typicode.com', '/test/test')) + .interceptWithAlice(_aliceHttpAdapter); -class _MyHomePageState extends State { - int _counter = 0; + http + .post(Uri.https('jsonplaceholder.typicode.com', '/posts'), body: body) + .then((response) => _aliceHttpAdapter.onResponse(response, body: body)); - void _incrementCounter() { - setState(() { - _counter++; - }); + http + .get(Uri.https('jsonplaceholder.typicode.com', '/posts')) + .then((response) => _aliceHttpAdapter.onResponse(response)); + + http + .put(Uri.https('jsonplaceholder.typicode.com', '/posts/1'), body: body) + .then((response) => _aliceHttpAdapter.onResponse(response, body: body)); + + http + .patch( + Uri.https('jsonplaceholder.typicode.com', '/posts/1'), + body: body, + ) + .then((response) => _aliceHttpAdapter.onResponse(response, body: body)); + + http + .delete(Uri.https('jsonplaceholder.typicode.com', '/posts/1')) + .then((response) => _aliceHttpAdapter.onResponse(response)); + + http + .get(Uri.https('jsonplaceholder.typicode.com', '/test/test')) + .then((response) => _aliceHttpAdapter.onResponse(response)); + + http + .post( + Uri.https( + 'jsonplaceholder.typicode.com', + '/posts', + {'key1': 'value1'}, + ), + body: body, + ) + .interceptWithAlice(_aliceHttpAdapter, body: body); + + http + .post( + Uri.https( + 'jsonplaceholder.typicode.com', + '/posts', + { + 'key1': 'value1', + 'key2': 'value2', + 'key3': 'value3', + }, + ), + body: body, + ) + .interceptWithAlice(_aliceHttpAdapter, body: body); + + http + .get( + Uri.https( + 'jsonplaceholder.typicode.com', + '/test/test', + { + 'key1': 'value1', + 'key2': 'value2', + 'key3': 'value3', + }, + ), + ) + .then((response) => _aliceHttpAdapter.onResponse(response)); } - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - backgroundColor: Theme.of(context).colorScheme.inversePrimary, - title: Text(widget.title), - ), - body: Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Text( - 'You have pushed the button this many times:', - ), - Text( - '$_counter', - style: Theme.of(context).textTheme.headlineMedium, - ), - ], - ), - ), - floatingActionButton: FloatingActionButton( - onPressed: _incrementCounter, - tooltip: 'Increment', - child: const Icon(Icons.add), - ), - ); + void _runHttpInspector() { + _alice.showInspector(); } } diff --git a/examples/alice_objectbox/pubspec.yaml b/examples/alice_objectbox/pubspec.yaml index 90b9f2a1..a0e9af87 100644 --- a/examples/alice_objectbox/pubspec.yaml +++ b/examples/alice_objectbox/pubspec.yaml @@ -1,5 +1,5 @@ name: alice_objectbox_example -description: "Alice + ObjectBox Example" +description: "Alice + ObjectBox + HTTP package - Example" publish_to: 'none' # Remove this line if you wish to publish to pub.dev version: 1.0.0+1 @@ -10,8 +10,10 @@ dependencies: flutter: sdk: flutter alice: + alice_http: alice_objectbox: cupertino_icons: ^1.0.8 + http: ^1.2.1 dev_dependencies: flutter_test: @@ -21,6 +23,8 @@ dev_dependencies: dependency_overrides: alice: path: ../../packages/alice + alice_http: + path: ../../packages/alice_http alice_objectbox: path: ../../packages/alice_objectbox diff --git a/packages/alice_objectbox/lib/alice_objectbox.dart b/packages/alice_objectbox/lib/alice_objectbox.dart index 568088ed..c1c1df98 100644 --- a/packages/alice_objectbox/lib/alice_objectbox.dart +++ b/packages/alice_objectbox/lib/alice_objectbox.dart @@ -5,10 +5,10 @@ import 'package:alice_objectbox/core/alice_core_objectbox.dart'; class AliceObjectBox extends Alice { AliceObjectBox({ required AliceStore store, - required super.showNotification, - required super.showInspectorOnShake, - required super.notificationIcon, - required super.maxCallsCount, + super.showNotification, + super.showInspectorOnShake, + super.notificationIcon, + super.maxCallsCount, super.directionality, super.showShareButton, }) : _store = store;