Skip to content

Commit

Permalink
feat: examples/alice_objectbox
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse committed Jun 27, 2024
1 parent b02818a commit ef8bb74
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 58 deletions.
216 changes: 163 additions & 53 deletions examples/alice_objectbox/lib/main.dart
Original file line number Diff line number Diff line change
@@ -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<void> 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<MyApp> createState() => _MyAppState();
}

class MyApp extends StatelessWidget {
const MyApp({super.key});
class _MyAppState extends State<MyApp> {
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<String, String> 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<MyHomePage> 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<MyHomePage> {
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: <Widget>[
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();
}
}
6 changes: 5 additions & 1 deletion examples/alice_objectbox/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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:
Expand All @@ -21,6 +23,8 @@ dev_dependencies:
dependency_overrides:
alice:
path: ../../packages/alice
alice_http:
path: ../../packages/alice_http
alice_objectbox:
path: ../../packages/alice_objectbox

Expand Down
8 changes: 4 additions & 4 deletions packages/alice_objectbox/lib/alice_objectbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ef8bb74

Please sign in to comment.