2023-05-03 11:51:18 -04:00
|
|
|
import 'dart:developer';
|
2023-11-14 15:28:14 -04:00
|
|
|
import 'dart:isolate';
|
|
|
|
import 'dart:ui';
|
2023-05-03 11:51:18 -04:00
|
|
|
|
2023-11-14 15:28:14 -04:00
|
|
|
import 'package:background_downloader/background_downloader.dart';
|
2023-05-15 09:45:41 -04:00
|
|
|
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';
|
2023-05-03 11:51:18 -04:00
|
|
|
import 'package:flutter_phoenix/flutter_phoenix.dart';
|
2023-05-16 07:01:42 -04:00
|
|
|
import 'package:hive_flutter/hive_flutter.dart';
|
|
|
|
import 'package:intl/intl.dart';
|
2023-05-03 11:51:18 -04:00
|
|
|
import 'package:logging/logging.dart';
|
2023-05-06 16:12:18 -04:00
|
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
2023-05-06 10:45:26 -04:00
|
|
|
import 'package:provider/provider.dart';
|
2023-10-10 04:57:14 -04:00
|
|
|
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';
|
|
|
|
|
2023-05-03 15:55:34 -04:00
|
|
|
void main() async {
|
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
2023-05-17 07:54:30 -04:00
|
|
|
|
2023-10-10 04:57:14 -04:00
|
|
|
SystemChrome.setSystemUIOverlayStyle(
|
|
|
|
const SystemUiOverlayStyle(
|
|
|
|
systemStatusBarContrastEnforced: true,
|
|
|
|
systemNavigationBarColor: Colors.transparent,
|
|
|
|
systemNavigationBarDividerColor: Colors.transparent,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge, overlays: [SystemUiOverlay.top]);
|
|
|
|
|
2023-05-16 07:01:42 -04:00
|
|
|
await Hive.initFlutter();
|
2023-06-23 16:23:46 -04:00
|
|
|
|
2023-05-16 07:01:42 -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
|
|
|
|
2023-05-03 15:55:34 -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);
|
2023-05-25 12:54:21 -04:00
|
|
|
await settingsClient.changeSettings(newSettings); // Save generated machineId to disk
|
2023-06-23 16:23:46 -04:00
|
|
|
|
2023-05-25 12:54:21 -04:00
|
|
|
AuthenticationData cachedAuth = AuthenticationData.unauthenticated();
|
|
|
|
try {
|
|
|
|
cachedAuth = await ApiClient.tryCachedLogin();
|
2023-11-14 15:28:14 -04:00
|
|
|
} catch (_) {
|
|
|
|
// Ignore
|
|
|
|
}
|
2023-06-23 16:23:46 -04:00
|
|
|
|
2023-10-03 13:00:59 -04:00
|
|
|
runApp(ReCon(settingsClient: settingsClient, cachedAuthentication: cachedAuth));
|
2023-04-29 13:18:46 -04:00
|
|
|
}
|
|
|
|
|
2023-10-03 13:00:59 -04:00
|
|
|
class ReCon extends StatefulWidget {
|
|
|
|
const ReCon({required this.settingsClient, required this.cachedAuthentication, super.key});
|
2023-05-03 15:55:34 -04:00
|
|
|
|
|
|
|
final SettingsClient settingsClient;
|
2023-05-25 12:54:21 -04:00
|
|
|
final AuthenticationData cachedAuthentication;
|
2023-04-30 07:39:09 -04:00
|
|
|
|
|
|
|
@override
|
2023-10-03 13:00:59 -04:00
|
|
|
State<ReCon> createState() => _ReConState();
|
2023-04-30 07:39:09 -04:00
|
|
|
}
|
|
|
|
|
2023-10-03 13:00:59 -04:00
|
|
|
class _ReConState extends State<ReCon> {
|
2023-11-14 15:28:14 -04:00
|
|
|
final Typography _typography = Typography.material2021(platform: defaultTargetPlatform);
|
|
|
|
final ReceivePort _port = ReceivePort();
|
2023-05-25 12:54:21 -04:00
|
|
|
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;
|
2023-05-07 09:01:01 -04:00
|
|
|
if (_checkedForUpdate) return;
|
2023-05-06 16:12:18 -04:00
|
|
|
_checkedForUpdate = true;
|
2023-05-07 09:01:01 -04:00
|
|
|
GithubApi.getLatestTagName().then((remoteVer) async {
|
2023-05-06 16:12:18 -04:00
|
|
|
final currentVer = (await PackageInfo.fromPlatform()).version;
|
2023-05-07 09:01:01 -04:00
|
|
|
SemVer currentSem;
|
|
|
|
SemVer remoteSem;
|
|
|
|
SemVer lastDismissedSem;
|
2023-05-06 16:12:18 -04:00
|
|
|
|
|
|
|
try {
|
2023-05-07 09:01:01 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-05-15 04:39:22 -04:00
|
|
|
if (remoteSem > currentSem && navigator.overlay?.context != null && context.mounted) {
|
2023-05-07 09:01:01 -04:00
|
|
|
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
|
|
|
|
2023-11-14 15:28:14 -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) {
|
2023-05-25 12:54:21 -04:00
|
|
|
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(
|
2024-01-27 11:15:27 -04:00
|
|
|
debugShowCheckedModeBanner: true,
|
2023-10-03 13:00:59 -04:00
|
|
|
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: [
|
2023-07-11 12:44:20 -04:00
|
|
|
ChangeNotifierProvider(
|
2023-06-04 10:27:18 -04:00
|
|
|
create: (context) => MessagingClient(
|
|
|
|
apiClient: clientHolder.apiClient,
|
2023-10-21 06:39:09 -04:00
|
|
|
settingsClient: clientHolder.settingsClient,
|
2023-06-04 10:27:18 -04:00
|
|
|
notificationClient: clientHolder.notificationClient,
|
|
|
|
),
|
|
|
|
),
|
2023-07-11 12:44:20 -04:00
|
|
|
ChangeNotifierProvider(
|
2023-06-04 10:27:18 -04:00
|
|
|
create: (context) => SessionClient(
|
|
|
|
apiClient: clientHolder.apiClient,
|
2023-07-11 12:44:20 -04:00
|
|
|
settingsClient: clientHolder.settingsClient,
|
2023-06-04 10:27:18 -04:00
|
|
|
),
|
|
|
|
),
|
2023-07-11 12:44:20 -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
|
|
|
)
|
|
|
|
],
|
2023-10-10 04:57:14 -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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|