Skip to content

Commit

Permalink
Get system theme for Flatpak build
Browse files Browse the repository at this point in the history
  • Loading branch information
Merrit committed May 19, 2022
1 parent 83b0815 commit c45b99f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
6 changes: 2 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void main() async {

// Initialize the settings service.
final settingsService = SettingsService(storageService);
final _settingsCubit = await SettingsCubit.init(settingsService);

// Run the app and pass in the state controllers.
runApp(
Expand All @@ -40,10 +41,7 @@ void main() async {
),
lazy: false,
),
BlocProvider(
create: (context) => SettingsCubit(settingsService),
lazy: false,
),
BlocProvider.value(value: _settingsCubit),
],
child: const App(),
),
Expand Down
14 changes: 10 additions & 4 deletions lib/src/settings/cubit/settings_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,23 @@ late SettingsCubit settingsCubit;
class SettingsCubit extends Cubit<SettingsState> {
final SettingsService _settingsService;

SettingsCubit(
this._settingsService,
) : super(
SettingsCubit._(this._settingsService, {required ThemeMode themeMode})
: super(
SettingsState(
exitOnCopy: _settingsService.exitOnCopy(),
themeMode: _settingsService.themeMode(),
themeMode: themeMode,
),
) {
settingsCubit = this;
}

static Future<SettingsCubit> init(SettingsService settingsService) async {
return SettingsCubit._(
settingsService,
themeMode: await settingsService.themeMode(),
);
}

/// Update and persist whether the app should exit after copy.
Future<void> updateExitOnCopy(bool value) async {
emit(state.copyWith(exitOnCopy: value));
Expand Down
19 changes: 16 additions & 3 deletions lib/src/settings/settings_service.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_flatpak/flutter_flatpak.dart';

import '../emoji/emoji.dart';
import '../storage/storage_service.dart';
Expand Down Expand Up @@ -66,16 +67,28 @@ class SettingsService {
}

/// Loads the user's preferred ThemeMode from storage.
ThemeMode themeMode() {
Future<ThemeMode> themeMode() async {
final theme = _storageService.getValue('ThemeMode');
switch (theme) {
case 'ThemeMode.dark':
return ThemeMode.dark;
case 'ThemeMode.light':
return ThemeMode.light;
default:
// If the user has not made a choice we follow system theme.
return ThemeMode.system;
final Flatpak? flatpak = await Flatpak.init();

ThemeMode themeMode;
if (flatpak != null) {
/// If the app is running in Flatpak we check the system theme
/// through the `flutter_flatpak` package.
themeMode = flatpak.themeMode;
} else {
/// If the user has not specified a specific theme for
/// the app we follow the system theme.
themeMode = ThemeMode.system;
}

return themeMode;
}
}

Expand Down
11 changes: 10 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_flatpak:
dependency: "direct main"
description:
path: "."
ref: HEAD
resolved-ref: "67f21802d33ac1f6137acf3ac64c7452526dea33"
url: "https://github.com/Merrit/flutter_flatpak.git"
source: git
version: "0.0.1"
flutter_launcher_icons:
dependency: "direct dev"
description:
Expand Down Expand Up @@ -620,5 +629,5 @@ packages:
source: hosted
version: "3.1.0"
sdks:
dart: ">=2.17.0-0 <3.0.0"
dart: ">=2.17.0 <3.0.0"
flutter: ">=2.5.0"
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ dependencies:
flutter:
sdk: flutter
flutter_bloc: ^7.3.3
flutter_flatpak:
git:
url: https://github.com/Merrit/flutter_flatpak.git
flutter_localizations:
sdk: flutter
hive: ^2.0.4
Expand Down

0 comments on commit c45b99f

Please sign in to comment.