/
main.dart
164 lines (144 loc) · 5.83 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import 'dart:convert';
import 'dart:io';
import 'package:flutter/services.dart' show rootBundle;
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:realm/realm.dart';
import 'model.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final realmConfig = json.decode(await rootBundle.loadString('assets/atlas_app/realm_config.json'));
String appId = realmConfig['app_id'];
MyApp.allTasksRealm = await createRealm(appId, CollectionType.allTasks);
MyApp.importantTasksRealm = await createRealm(appId, CollectionType.importantTasks);
MyApp.normalTasksRealm = await createRealm(appId, CollectionType.normalTasks);
runApp(const MyApp());
}
enum CollectionType { allTasks, importantTasks, normalTasks }
Future<Realm> createRealm(String appId, CollectionType collectionType) async {
final appConfig = AppConfiguration(appId);
final app = App(appConfig);
final user = await app.logIn(Credentials.anonymous());
final flxConfig = Configuration.flexibleSync(user, [Task.schema], path: await absolutePath("db_${collectionType.name}.realm"));
var realm = Realm(flxConfig);
print("Created local realm db at: ${realm.config.path}");
final RealmResults<Task> query;
if (collectionType == CollectionType.allTasks) {
query = realm.all<Task>();
} else {
query = realm.query<Task>(r'isImportant == $0', [collectionType == CollectionType.importantTasks]);
}
realm.subscriptions.update((mutableSubscriptions) {
mutableSubscriptions.add(query);
});
await realm.subscriptions.waitForSynchronization();
await realm.syncSession.waitForDownload();
print("Syncronization completed for realm: ${realm.config.path}");
return realm;
}
Future<String> absolutePath(String fileName) async {
final appDocsDirectory = await getApplicationDocumentsDirectory();
final realmDirectory = '${appDocsDirectory.path}/mongodb-realm';
if (!Directory(realmDirectory).existsSync()) {
await Directory(realmDirectory).create(recursive: true);
}
return "$realmDirectory/$fileName";
}
class MyApp extends StatelessWidget {
static late Realm allTasksRealm;
static late Realm importantTasksRealm;
static late Realm normalTasksRealm;
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.green,
),
home: const MyHomePage(title: 'Flutter Realm Flexible Sync'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _allTasksCount = MyApp.allTasksRealm.all<Task>().length;
int _importantTasksCount = MyApp.importantTasksRealm.all<Task>().length;
int _normalTasksCount = MyApp.normalTasksRealm.all<Task>().length;
void _createImportantTasks() async {
var importantTasks = MyApp.importantTasksRealm.all<Task>();
var allTasksCount = MyApp.allTasksRealm.all<Task>();
MyApp.allTasksRealm.write(() {
MyApp.allTasksRealm.add(Task(ObjectId(), "Important task ${importantTasks.length + 1}", false, true));
});
await MyApp.allTasksRealm.syncSession.waitForUpload();
await MyApp.importantTasksRealm.subscriptions.waitForSynchronization();
setState(() {
_importantTasksCount = importantTasks.length;
_allTasksCount = allTasksCount.length;
});
}
void _createNormalTasks() async {
var normalTasks = MyApp.normalTasksRealm.all<Task>();
var allTasksCount = MyApp.allTasksRealm.all<Task>();
MyApp.allTasksRealm.write(() {
MyApp.allTasksRealm.add(Task(ObjectId(), "Normal task ${normalTasks.length + 1}", false, false));
});
await MyApp.allTasksRealm.syncSession.waitForUpload();
await MyApp.normalTasksRealm.subscriptions.waitForSynchronization();
setState(() {
_normalTasksCount = normalTasks.length;
_allTasksCount = allTasksCount.length;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('Important tasks count:', style: TextStyle(fontWeight: FontWeight.bold)),
Text('$_importantTasksCount', style: Theme.of(context).textTheme.headline4),
const Text('Normal tasks count:', style: TextStyle(fontWeight: FontWeight.bold)),
Text('$_normalTasksCount', style: Theme.of(context).textTheme.headline4),
const Text('All tasks count:', style: TextStyle(fontWeight: FontWeight.bold)),
Text('$_allTasksCount', style: Theme.of(context).textTheme.headline4),
],
),
),
floatingActionButton: Stack(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 0.0),
child: Align(
alignment: Alignment.bottomLeft,
child: FloatingActionButton(
onPressed: _createImportantTasks,
tooltip: 'High priority task',
child: const Icon(Icons.add),
)),
),
Padding(
padding: const EdgeInsets.only(right: 40.0),
child: Align(
alignment: Alignment.bottomRight,
child: FloatingActionButton(
onPressed: _createNormalTasks,
tooltip: 'Normal task',
child: const Icon(Icons.add),
)),
),
],
),
floatingActionButtonLocation: FloatingActionButtonLocation.startFloat);
}
}