OpenContacts/lib/main.dart

82 lines
2.8 KiB
Dart
Raw Normal View History

import 'dart:developer';
import 'dart:io' show Platform;
import 'package:contacts_plus_plus/clients/messaging_client.dart';
import 'package:contacts_plus_plus/clients/settings_client.dart';
2023-05-03 12:16:40 -04:00
import 'package:contacts_plus_plus/widgets/friends_list.dart';
2023-05-01 13:13:40 -04:00
import 'package:contacts_plus_plus/widgets/login_screen.dart';
2023-04-29 13:18:46 -04:00
import 'package:flutter/material.dart';
import 'package:flutter_phoenix/flutter_phoenix.dart';
import 'package:logging/logging.dart';
import 'package:workmanager/workmanager.dart';
import 'clients/api_client.dart';
2023-04-29 13:18:46 -04:00
import 'models/authentication_data.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
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
);
}
Logger.root.onRecord.listen((event) => log(event.message, name: event.loggerName));
final settingsClient = SettingsClient();
await settingsClient.loadSettings();
runApp(Phoenix(child: ContactsPlusPlus(settingsClient: settingsClient,)));
2023-04-29 13:18:46 -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.
if (task == MessagingClient.taskName) {
final unreads = MessagingClient.backgroundCheckUnreads(inputData);
}
return Future.value(true);
});
}
2023-05-01 13:13:40 -04:00
class ContactsPlusPlus extends StatefulWidget {
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-04-29 13:18:46 -04:00
@override
Widget build(BuildContext context) {
2023-05-02 04:04:54 -04:00
return ClientHolder(
settingsClient: widget.settingsClient,
2023-04-30 07:39:09 -04:00
authenticationData: _authData,
2023-05-02 04:04:54 -04:00
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Contacts++',
theme: ThemeData(
2023-04-30 17:14:29 -04:00
useMaterial3: true,
2023-04-30 07:39:09 -04:00
textTheme: _typography.white,
colorScheme: ColorScheme.fromSeed(seedColor: Colors.purple, brightness: Brightness.dark)
2023-05-02 04:04:54 -04:00
),
home: _authData.isAuthenticated ?
const FriendsList() :
2023-05-02 04:04:54 -04:00
LoginScreen(
onLoginSuccessful: (AuthenticationData authData) async {
if (authData.isAuthenticated) {
setState(() {
_authData = authData;
});
}
},
2023-04-30 07:39:09 -04:00
),
2023-04-29 13:18:46 -04:00
),
);
}
}