OpenContacts/lib/main.dart

190 lines
7.1 KiB
Dart
Raw Normal View History

import 'dart:developer';
2023-05-06 16:12:18 -04:00
import 'package:contacts_plus_plus/apis/github_api.dart';
2023-05-05 06:45:00 -04:00
import 'package:contacts_plus_plus/client_holder.dart';
import 'package:contacts_plus_plus/clients/api_client.dart';
2023-06-17 10:58:32 -04:00
import 'package:contacts_plus_plus/clients/inventory_client.dart';
import 'package:contacts_plus_plus/clients/messaging_client.dart';
2023-06-04 10:27:18 -04:00
import 'package:contacts_plus_plus/clients/session_client.dart';
import 'package:contacts_plus_plus/clients/settings_client.dart';
import 'package:contacts_plus_plus/models/sem_ver.dart';
2023-06-17 10:58:32 -04:00
import 'package:contacts_plus_plus/widgets/homepage.dart';
2023-05-01 13:13:40 -04:00
import 'package:contacts_plus_plus/widgets/login_screen.dart';
2023-05-06 16:12:18 -04:00
import 'package:contacts_plus_plus/widgets/update_notifier.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-06-23 16:23:46 -04:00
import 'package:flutter_downloader/flutter_downloader.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';
2023-04-29 13:18:46 -04:00
import 'models/authentication_data.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
2023-06-23 16:23:46 -04:00
await FlutterDownloader.initialize(
debug: kDebugMode,
);
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 (_) {}
2023-06-23 16:23:46 -04:00
runApp(ContactsPlusPlus(settingsClient: settingsClient, cachedAuthentication: cachedAuth));
2023-04-29 13:18:46 -04:00
}
2023-05-01 13:13:40 -04:00
class ContactsPlusPlus extends StatefulWidget {
const ContactsPlusPlus({required this.settingsClient, required this.cachedAuthentication, super.key});
final SettingsClient settingsClient;
final AuthenticationData cachedAuthentication;
2023-04-30 07:39:09 -04:00
@override
2023-05-01 13:13:40 -04:00
State<ContactsPlusPlus> createState() => _ContactsPlusPlusState();
2023-04-30 07:39:09 -04:00
}
2023-05-01 13:13:40 -04:00
class _ContactsPlusPlusState extends State<ContactsPlusPlus> {
2023-04-29 16:21:00 -04:00
final Typography _typography = Typography.material2021(platform: TargetPlatform.android);
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
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: 'Contacts++',
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,
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: 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
);
}
}