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-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';
|
|
|
|
import 'package:flutter/foundation.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';
|
|
|
|
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-05-04 07:13:24 -04:00
|
|
|
import 'package:workmanager/workmanager.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-04 08:44:40 -04:00
|
|
|
if (Platform.isAndroid) {
|
|
|
|
await Workmanager().initialize(
|
|
|
|
callbackDispatcher, // The top level function, aka callbackDispatcher
|
|
|
|
isInDebugMode: true // If enabled it will post a notification whenever the task is running. Handy for debugging tasks
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:05:06 -04:00
|
|
|
Logger.root.onRecord.listen((event) => log(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-04 07:13:24 -04:00
|
|
|
@pragma('vm:entry-point') // Mandatory if the App is obfuscated or using Flutter 3.1+
|
|
|
|
void callbackDispatcher() {
|
|
|
|
Workmanager().executeTask((String task, Map<String, dynamic>? inputData) async {
|
|
|
|
debugPrint("Native called background task: $task"); //simpleTask will be emitted here.
|
2023-05-04 13:04:33 -04:00
|
|
|
if (task == MessagingClient.taskName) {
|
|
|
|
final unreads = MessagingClient.backgroundCheckUnreads(inputData);
|
2023-05-04 07:13:24 -04:00
|
|
|
}
|
|
|
|
return Future.value(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
if (_checkedForUpdate || kDebugMode) return;
|
|
|
|
_checkedForUpdate = true;
|
|
|
|
GithubApi.getLatestTagName().then((value) async {
|
|
|
|
final currentVer = (await PackageInfo.fromPlatform()).version;
|
|
|
|
|
|
|
|
try {
|
|
|
|
final currentSem = SemVer.fromString(currentVer);
|
|
|
|
final remoteSem = SemVer.fromString(value);
|
|
|
|
if (remoteSem > currentSem && navigator.overlay?.context != null) {
|
|
|
|
showDialog(
|
|
|
|
context: navigator.overlay!.context,
|
|
|
|
builder: (context) {
|
|
|
|
return const UpdateNotifier();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
if (currentVer != value && navigator.overlay?.context != null) {
|
|
|
|
showDialog(
|
|
|
|
context: navigator.overlay!.context,
|
|
|
|
builder: (context) {
|
|
|
|
return const UpdateNotifier();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
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-06 16:12:18 -04:00
|
|
|
child: MaterialApp(
|
|
|
|
debugShowCheckedModeBanner: false,
|
|
|
|
title: 'Contacts++',
|
|
|
|
theme: ThemeData(
|
|
|
|
useMaterial3: true,
|
|
|
|
textTheme: _typography.white,
|
|
|
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.purple, brightness: Brightness.dark)
|
|
|
|
),
|
|
|
|
home: Builder(
|
|
|
|
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
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|