OpenContacts/lib/main.dart

233 lines
8.2 KiB
Dart
Raw Normal View History

import 'dart:developer';
import 'dart:isolate';
import 'dart:ui';
import 'package:background_downloader/background_downloader.dart';
import 'package:dynamic_color/dynamic_color.dart';
2023-06-23 16:23:46 -04:00
import 'package:flutter/foundation.dart';
2023-04-29 13:18:46 -04:00
import 'package:flutter/material.dart';
2023-09-30 09:22:37 -04:00
import 'package:flutter/services.dart';
import 'package:flutter_phoenix/flutter_phoenix.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:intl/intl.dart';
import 'package:logging/logging.dart';
2023-05-06 16:12:18 -04:00
import 'package:package_info_plus/package_info_plus.dart';
import 'package:provider/provider.dart';
import 'package:recon/apis/github_api.dart';
import 'package:recon/client_holder.dart';
import 'package:recon/clients/api_client.dart';
import 'package:recon/clients/inventory_client.dart';
import 'package:recon/clients/messaging_client.dart';
import 'package:recon/clients/session_client.dart';
import 'package:recon/clients/settings_client.dart';
import 'package:recon/models/sem_ver.dart';
import 'package:recon/widgets/homepage.dart';
import 'package:recon/widgets/login_screen.dart';
import 'package:recon/widgets/update_notifier.dart';
2023-04-29 13:18:46 -04:00
import 'models/authentication_data.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setSystemUIOverlayStyle(
const SystemUiOverlayStyle(
systemStatusBarContrastEnforced: true,
systemNavigationBarColor: Colors.transparent,
systemNavigationBarDividerColor: Colors.transparent,
),
);
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge, overlays: [SystemUiOverlay.top]);
await Hive.initFlutter();
2023-06-23 16:23:46 -04:00
final dateFormat = DateFormat.Hms();
2023-06-03 11:17:54 -04:00
Logger.root.onRecord.listen(
(event) => log("${dateFormat.format(event.time)}: ${event.message}", name: event.loggerName, time: event.time));
2023-06-23 16:23:46 -04:00
final settingsClient = SettingsClient();
await settingsClient.loadSettings();
2023-06-03 11:17:54 -04:00
final newSettings =
settingsClient.currentSettings.copyWith(machineId: settingsClient.currentSettings.machineId.valueOrDefault);
await settingsClient.changeSettings(newSettings); // Save generated machineId to disk
2023-06-23 16:23:46 -04:00
AuthenticationData cachedAuth = AuthenticationData.unauthenticated();
try {
cachedAuth = await ApiClient.tryCachedLogin();
} catch (_) {
// Ignore
}
2023-06-23 16:23:46 -04:00
runApp(ReCon(settingsClient: settingsClient, cachedAuthentication: cachedAuth));
2023-04-29 13:18:46 -04:00
}
class ReCon extends StatefulWidget {
const ReCon({required this.settingsClient, required this.cachedAuthentication, super.key});
final SettingsClient settingsClient;
final AuthenticationData cachedAuthentication;
2023-04-30 07:39:09 -04:00
@override
State<ReCon> createState() => _ReConState();
2023-04-30 07:39:09 -04:00
}
class _ReConState extends State<ReCon> {
final Typography _typography = Typography.material2021(platform: defaultTargetPlatform);
final ReceivePort _port = ReceivePort();
late AuthenticationData _authData = widget.cachedAuthentication;
2023-05-06 16:12:18 -04:00
bool _checkedForUpdate = false;
void showUpdateDialogOnFirstBuild(BuildContext context) {
final navigator = Navigator.of(context);
2023-06-03 11:17:54 -04:00
final settings = ClientHolder.of(context).settingsClient;
if (_checkedForUpdate) return;
2023-05-06 16:12:18 -04:00
_checkedForUpdate = true;
GithubApi.getLatestTagName().then((remoteVer) async {
2023-05-06 16:12:18 -04:00
final currentVer = (await PackageInfo.fromPlatform()).version;
SemVer currentSem;
SemVer remoteSem;
SemVer lastDismissedSem;
2023-05-06 16:12:18 -04:00
try {
currentSem = SemVer.fromString(currentVer);
} catch (_) {
currentSem = SemVer.zero();
}
try {
lastDismissedSem = SemVer.fromString(settings.currentSettings.lastDismissedVersion.valueOrDefault);
} catch (_) {
lastDismissedSem = SemVer.zero();
}
try {
remoteSem = SemVer.fromString(remoteVer);
} catch (_) {
return;
}
if (remoteSem <= lastDismissedSem && lastDismissedSem.isNotZero) {
return;
}
if (remoteSem > currentSem && navigator.overlay?.context != null && context.mounted) {
showDialog(
context: navigator.overlay!.context,
builder: (context) {
return UpdateNotifier(
remoteVersion: remoteSem,
localVersion: currentSem,
);
},
);
2023-05-06 16:12:18 -04:00
}
});
}
2023-04-29 13:18:46 -04:00
@override
void initState() {
super.initState();
IsolateNameServer.registerPortWithName(_port.sendPort, 'downloader_send_port');
_port.listen((dynamic data) {
// Not useful yet? idk...
// String id = data[0];
// DownloadTaskStatus status = data[1];
// int progress = data[2];
});
FileDownloader().updates.listen(downloadCallback);
}
@override
void dispose() {
IsolateNameServer.removePortNameMapping('downloader_send_port');
super.dispose();
}
@pragma('vm:entry-point')
static void downloadCallback(TaskUpdate event) {}
2023-04-29 13:18:46 -04:00
@override
Widget build(BuildContext context) {
return Phoenix(
2023-06-03 11:17:54 -04:00
child: Builder(builder: (context) {
return ClientHolder(
settingsClient: widget.settingsClient,
authenticationData: _authData,
onLogout: () {
setState(() {
_authData = AuthenticationData.unauthenticated();
});
Phoenix.rebirth(context);
},
child: DynamicColorBuilder(
builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) => MaterialApp(
2023-06-04 10:27:18 -04:00
debugShowCheckedModeBanner: false,
title: 'ReCon',
2023-06-04 10:27:18 -04:00
theme: ThemeData(
useMaterial3: true,
textTheme: _typography.black,
colorScheme:
lightDynamic ?? ColorScheme.fromSeed(seedColor: Colors.purple, brightness: Brightness.light),
),
darkTheme: ThemeData(
useMaterial3: true,
textTheme: _typography.white,
colorScheme: darkDynamic ?? ColorScheme.fromSeed(seedColor: Colors.purple, brightness: Brightness.dark),
),
themeMode: ThemeMode.values[widget.settingsClient.currentSettings.themeMode.valueOrDefault],
home: Builder(
// Builder is necessary here since we need a context which has access to the ClientHolder
builder: (context) {
2023-06-03 11:17:54 -04:00
showUpdateDialogOnFirstBuild(context);
final clientHolder = ClientHolder.of(context);
return _authData.isAuthenticated
2023-06-04 10:27:18 -04:00
? MultiProvider(
providers: [
ChangeNotifierProvider(
2023-06-04 10:27:18 -04:00
create: (context) => MessagingClient(
apiClient: clientHolder.apiClient,
settingsClient: clientHolder.settingsClient,
2023-06-04 10:27:18 -04:00
notificationClient: clientHolder.notificationClient,
),
),
ChangeNotifierProvider(
2023-06-04 10:27:18 -04:00
create: (context) => SessionClient(
apiClient: clientHolder.apiClient,
settingsClient: clientHolder.settingsClient,
2023-06-04 10:27:18 -04:00
),
),
ChangeNotifierProvider(
2023-06-17 10:58:32 -04:00
create: (context) => InventoryClient(
apiClient: clientHolder.apiClient,
2023-06-04 10:27:18 -04:00
),
2023-06-17 10:58:32 -04:00
)
],
child: AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle(
statusBarColor: Theme.of(context).colorScheme.surfaceVariant,
),
child: const Home(),
),
2023-06-03 11:17:54 -04:00
)
: LoginScreen(
onLoginSuccessful: (AuthenticationData authData) async {
if (authData.isAuthenticated) {
setState(() {
_authData = authData;
});
}
},
);
2023-06-04 10:27:18 -04:00
},
),
),
2023-06-03 11:17:54 -04:00
),
);
}),
2023-04-29 13:18:46 -04:00
);
}
}