forked from cypherstack/Campfire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
210 lines (193 loc) · 7 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:keyboard_dismisser/keyboard_dismisser.dart';
import 'package:path_provider/path_provider.dart' as path;
import 'package:paymint/models/models.dart';
import 'package:paymint/pages/loading_view.dart';
import 'package:paymint/pages/onboarding_view/onboarding_view.dart';
import 'package:paymint/pages/wallet_selection_view.dart';
import 'package:paymint/services/address_book_service.dart';
import 'package:paymint/services/coins/manager.dart';
import 'package:paymint/services/locale_service.dart';
import 'package:paymint/services/node_service.dart';
import 'package:paymint/services/notes_service.dart';
import 'package:paymint/services/notifications_api.dart';
import 'package:paymint/services/wallets_service.dart';
import 'package:paymint/utilities/cfcolors.dart';
import 'package:paymint/utilities/db_version_migration.dart';
import 'package:paymint/utilities/logger.dart';
import 'package:paymint/utilities/misc_global_constants.dart';
import 'package:paymint/utilities/sizing_utilities.dart';
import 'package:provider/provider.dart';
import 'route_generator.dart';
// main() is the entry point to the app. It initializes Hive (local database),
// runs the MyApp widget and checks for new users, caching the value in the
// miscellaneous box for later use
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final appDirectory = await path.getApplicationDocumentsDirectory();
await Hive.initFlutter(appDirectory.path);
// Registering Transaction Model Adapters
Hive.registerAdapter(TransactionDataAdapter());
Hive.registerAdapter(TransactionChunkAdapter());
Hive.registerAdapter(TransactionAdapter());
Hive.registerAdapter(InputAdapter());
Hive.registerAdapter(OutputAdapter());
// Registering Utxo Model Adapters
Hive.registerAdapter(UtxoDataAdapter());
Hive.registerAdapter(UtxoObjectAdapter());
Hive.registerAdapter(StatusAdapter());
// Registering Lelantus Model Adapters
Hive.registerAdapter(LelantusCoinAdapter());
final wallets = await Hive.openBox('wallets');
await wallets.put('currentWalletName', "");
int dbVersion = await wallets.get("db_version");
if (dbVersion == null || dbVersion < CampfireConstants.currentDbVersion) {
if (dbVersion == null) dbVersion = 0;
await DbVersionMigrator().migrate(dbVersion);
}
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
overlays: [SystemUiOverlay.bottom]);
await NotificationApi.init();
runApp(MyApp());
}
/// MyApp initialises relevant services with a MultiProvider
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final localeService = LocaleService();
localeService.loadLocale();
return KeyboardDismisser(
child: MultiProvider(
providers: [
ChangeNotifierProvider(
create: (_) => Manager(),
),
ChangeNotifierProvider(
create: (_) => WalletsService(),
),
ChangeNotifierProvider(
create: (_) => AddressBookService(),
),
ChangeNotifierProvider(
create: (_) => NodeService(),
),
ChangeNotifierProvider(
create: (_) => NotesService(),
),
ChangeNotifierProvider(
create: (_) => localeService,
),
],
child: MaterialAppWithTheme(),
),
);
}
}
// Sidenote: MaterialAppWithTheme and InitView are only separated for clarity. No other reason.
class MaterialAppWithTheme extends StatefulWidget {
const MaterialAppWithTheme({
Key key,
}) : super(key: key);
@override
_MaterialAppWithThemeState createState() => _MaterialAppWithThemeState();
}
class _MaterialAppWithThemeState extends State<MaterialAppWithTheme> {
@override
void initState() {
super.initState();
}
/// Returns true if the user has never set up any wallets before
Future<bool> _checkForWallets() async {
final walletsBox = await Hive.openBox('wallets');
final wallets = await walletsBox.get("names");
Logger.print("walletBox: ${walletsBox.toMap()}");
if (!(wallets != null && wallets.length > 0)) {
return true;
} else {
return false;
}
}
OutlineInputBorder _buildOutlineInputBorder(Color color) {
return OutlineInputBorder(
borderSide: BorderSide(
width: 1,
color: color,
),
borderRadius: BorderRadius.circular(SizingUtilities.circularBorderRadius),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Campfire',
onGenerateRoute: RouteGenerator.generateRoute,
theme: ThemeData(
brightness: Brightness.light,
fontFamily: GoogleFonts.workSans().fontFamily,
textTheme: GoogleFonts.workSansTextTheme(),
primaryColor: CFColors.spark,
primarySwatch: CFColors.createMaterialColor(CFColors.spark),
checkboxTheme: CheckboxThemeData(
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(SizingUtilities.checkboxBorderRadius),
),
checkColor: MaterialStateColor.resolveWith(
(state) {
if (state.contains(MaterialState.selected)) {
return CFColors.white;
}
return CFColors.fog;
},
),
fillColor: MaterialStateColor.resolveWith(
(states) {
if (states.contains(MaterialState.selected)) {
return CFColors.spark;
}
return CFColors.dew;
},
),
),
appBarTheme: AppBarTheme(
centerTitle: true,
color: CFColors.starryNight,
elevation: 0,
),
inputDecorationTheme: InputDecorationTheme(
fillColor: CFColors.fog,
filled: true,
contentPadding: EdgeInsets.symmetric(vertical: 12, horizontal: 16),
hintStyle: GoogleFonts.workSans(
color: CFColors.twilight,
fontWeight: FontWeight.w400,
fontSize: 16,
),
enabledBorder: _buildOutlineInputBorder(CFColors.dew),
focusedBorder: _buildOutlineInputBorder(CFColors.focusedBorder),
errorBorder: _buildOutlineInputBorder(CFColors.errorBorder),
disabledBorder: _buildOutlineInputBorder(CFColors.dew),
focusedErrorBorder: _buildOutlineInputBorder(CFColors.errorBorder),
),
),
home: FutureBuilder(
future: _checkForWallets(),
builder: (BuildContext context,
AsyncSnapshot<bool> shouldRouteToOnboarding) {
if (shouldRouteToOnboarding.connectionState == ConnectionState.done) {
if (shouldRouteToOnboarding.data) {
return OnboardingView();
} else {
return WalletSelectionView();
}
} else {
return LoadingView();
}
},
),
);
}
}