-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace core client singleton by provider
This is a preparation for introducing a custom router and splitting the core client into separate feature-specific classes. The `CoreClient` instance can now be access from `context` via a provider with `context.read<CoreClient>`. There is also a shortcut available via an extension of `BuildContext`: `context.coreClient`. Also: * Move `App` into a separate file * Configure Flutter logging in main * Init Flutter Rust Bridge in main * Move out theme data into a separate file
- Loading branch information
Showing
26 changed files
with
295 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# SPDX-FileCopyrightText: 2024 Phoenix R&D GmbH <[email protected]> | ||
# | ||
# SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
description: This file stores settings for Dart & Flutter DevTools. | ||
documentation: https://docs.flutter.dev/tools/devtools/extensions#configure-extension-enablement-states | ||
extensions: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// SPDX-FileCopyrightText: 2024 Phoenix R&D GmbH <[email protected]> | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
import 'dart:io'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:logging/logging.dart'; | ||
import 'package:permission_handler/permission_handler.dart'; | ||
import 'package:prototype/core_client.dart'; | ||
import 'package:prototype/homescreen.dart'; | ||
import 'package:prototype/platform.dart'; | ||
import 'package:provider/provider.dart'; | ||
|
||
import 'theme/theme.dart'; | ||
|
||
final GlobalKey<NavigatorState> appNavigator = GlobalKey<NavigatorState>(); | ||
|
||
final _log = Logger('App'); | ||
|
||
class App extends StatefulWidget { | ||
const App({super.key}); | ||
|
||
@override | ||
State<App> createState() => _AppState(); | ||
} | ||
|
||
class _AppState extends State<App> with WidgetsBindingObserver { | ||
final CoreClient _coreClient = CoreClient(); | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
WidgetsBinding.instance.addObserver(this); | ||
_requestMobileNotifications(); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
WidgetsBinding.instance.removeObserver(this); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
void didChangeAppLifecycleState(AppLifecycleState state) { | ||
super.didChangeAppLifecycleState(state); | ||
_onStateChanged(state); | ||
} | ||
|
||
Future<void> _onStateChanged(AppLifecycleState state) async { | ||
if (state == AppLifecycleState.paused) { | ||
_log.fine('App is in the background'); | ||
|
||
// iOS only | ||
if (Platform.isIOS) { | ||
// only set the badge count if the user is logged in | ||
if (_coreClient.maybeUser case final user?) { | ||
final count = await user.globalUnreadMessagesCount(); | ||
await setBadgeCount(count); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
// TODO: This provider should be moved below the `MaterialApp`. This can be | ||
// done when the app router is introduced. We can't just wrap the | ||
// `HomeScreen` because it is replaced in other places by another screens. | ||
return Provider.value( | ||
value: _coreClient, | ||
child: MaterialApp( | ||
title: 'Prototype', | ||
debugShowCheckedModeBanner: false, | ||
theme: themeData(context), | ||
navigatorKey: appNavigator, | ||
home: const HomeScreen(), | ||
), | ||
); | ||
} | ||
} | ||
|
||
void _requestMobileNotifications() async { | ||
// Mobile initialization | ||
if (Platform.isAndroid || Platform.isIOS) { | ||
// Initialize the method channel | ||
initMethodChannel(); | ||
|
||
// Ask for notification permission | ||
var status = await Permission.notification.status; | ||
switch (status) { | ||
case PermissionStatus.denied: | ||
_log.info("Notification permission denied, will ask the user"); | ||
var requestStatus = await Permission.notification.request(); | ||
_log.fine("The status is $requestStatus"); | ||
break; | ||
default: | ||
_log.info("Notification permission status: $status"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.