2023-05-03 11:51:18 -04:00
|
|
|
import 'dart:developer';
|
2023-05-04 08:44:40 -04:00
|
|
|
import 'dart:io' show Platform;
|
2023-05-03 11:51:18 -04:00
|
|
|
|
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';
|
2023-05-04 13:04:33 -04:00
|
|
|
import 'package:contacts_plus_plus/clients/messaging_client.dart';
|
2023-05-03 15:55:34 -04:00
|
|
|
import 'package:contacts_plus_plus/clients/settings_client.dart';
|
2023-05-07 09:01:01 -04:00
|
|
|
import 'package:contacts_plus_plus/models/sem_ver.dart';
|
2023-05-06 13:24:28 -04:00
|
|
|
import 'package:contacts_plus_plus/widgets/friends/friends_list.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';
|
2023-05-15 09:45:41 -04:00
|
|
|
import 'package:dynamic_color/dynamic_color.dart';
|
2023-04-29 13:18:46 -04:00
|
|
|
import 'package:flutter/material.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-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-05-16 07:01:42 -04:00
|
|
|
await Hive.initFlutter();
|
|
|
|
final dateFormat = DateFormat.Hms();
|
|
|
|
Logger.root.onRecord.listen((event) => log("${dateFormat.format(event.time)}: ${event.message}", name: event.loggerName, time: event.time));
|
2023-05-03 15:55:34 -04:00
|
|
|
final settingsClient = SettingsClient();
|
|
|
|
await settingsClient.loadSettings();
|
|
|
|
runApp(Phoenix(child: ContactsPlusPlus(settingsClient: settingsClient,)));
|
2023-04-29 13:18:46 -04:00
|
|
|
}
|
|
|
|
|
2023-05-01 13:13:40 -04:00
|
|
|
class ContactsPlusPlus extends StatefulWidget {
|
2023-05-03 15:55:34 -04:00
|
|
|
const ContactsPlusPlus({required this.settingsClient, super.key});
|
|
|
|
|
|
|
|
final SettingsClient settingsClient;
|
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);
|
2023-04-30 07:39:09 -04:00
|
|
|
AuthenticationData _authData = AuthenticationData.unauthenticated();
|
2023-05-06 16:12:18 -04:00
|
|
|
bool _checkedForUpdate = false;
|
|
|
|
|
|
|
|
void showUpdateDialogOnFirstBuild(BuildContext context) {
|
|
|
|
final navigator = Navigator.of(context);
|
2023-05-07 09:01:01 -04:00
|
|
|
final settings = ClientHolder
|
|
|
|
.of(context)
|
|
|
|
.settingsClient;
|
|
|
|
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
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-05-02 04:04:54 -04:00
|
|
|
return ClientHolder(
|
2023-05-03 15:55:34 -04:00
|
|
|
settingsClient: widget.settingsClient,
|
2023-04-30 07:39:09 -04:00
|
|
|
authenticationData: _authData,
|
2023-05-15 09:45:41 -04:00
|
|
|
child: DynamicColorBuilder(
|
|
|
|
builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) => MaterialApp(
|
|
|
|
debugShowCheckedModeBanner: false,
|
|
|
|
title: 'Contacts++',
|
|
|
|
theme: ThemeData(
|
2023-05-06 16:12:18 -04:00
|
|
|
useMaterial3: true,
|
|
|
|
textTheme: _typography.white,
|
2023-05-15 09:45:41 -04:00
|
|
|
colorScheme: darkDynamic ?? ColorScheme.fromSeed(seedColor: Colors.purple, brightness: Brightness.dark),
|
|
|
|
),
|
|
|
|
home: Builder( // Builder is necessary here since we need a context which has access to the ClientHolder
|
|
|
|
builder: (context) {
|
|
|
|
showUpdateDialogOnFirstBuild(context);
|
|
|
|
final clientHolder = ClientHolder.of(context);
|
|
|
|
return _authData.isAuthenticated ?
|
|
|
|
ChangeNotifierProvider( // This doesn't need to be a proxy provider since the arguments should never change during it's lifetime.
|
|
|
|
create: (context) =>
|
|
|
|
MessagingClient(
|
|
|
|
apiClient: clientHolder.apiClient,
|
|
|
|
notificationClient: clientHolder.notificationClient,
|
|
|
|
),
|
|
|
|
child: const FriendsList(),
|
|
|
|
) :
|
|
|
|
LoginScreen(
|
|
|
|
onLoginSuccessful: (AuthenticationData authData) async {
|
|
|
|
if (authData.isAuthenticated) {
|
|
|
|
setState(() {
|
|
|
|
_authData = authData;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
),
|
2023-04-29 13:18:46 -04:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|