Skip to content

Commit

Permalink
moved theming options to separate section
Browse files Browse the repository at this point in the history
  • Loading branch information
hjiangsu committed Jun 25, 2023
1 parent 26149fa commit 3e71a0e
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 82 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
## Unreleased
### Added
- Added adaptive icons for Android - contribution from @coslu

### Changed
- Moved theming options into a separate section in settings

## 0.2.1+7 - 2023-06-25
### Fixed
- Fixed issue where creating a comment on a post would not work
- Added back icon to image preview for edge cases where the swipe down gesture does not work
Expand Down
11 changes: 11 additions & 0 deletions lib/routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:go_router/go_router.dart';
import 'package:thunder/settings/pages/about_settings_page.dart';

import 'package:thunder/settings/pages/general_settings_page.dart';
import 'package:thunder/settings/pages/theme_settings_page.dart';
import 'package:thunder/settings/settings.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';
import 'package:thunder/thunder/thunder.dart';
Expand Down Expand Up @@ -33,6 +34,16 @@ final GoRouter router = GoRouter(
);
},
),
GoRoute(
name: 'themes',
path: 'themes',
builder: (context, state) {
return BlocProvider.value(
value: state.extra! as ThunderBloc,
child: const ThemeSettingsPage(),
);
},
),
GoRoute(
name: 'about',
path: 'about',
Expand Down
80 changes: 0 additions & 80 deletions lib/settings/pages/general_settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:flutter_bloc/flutter_bloc.dart';

import 'package:shared_preferences/shared_preferences.dart';

import 'package:thunder/core/theme/bloc/theme_bloc.dart';
import 'package:thunder/settings/widgets/toggle_option.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';

Expand All @@ -27,8 +26,6 @@ class _GeneralSettingsPageState extends State<GeneralSettingsPage> {
bool showInAppUpdateNotification = true;

String defaultInstance = 'lemmy.world';
String themeType = 'dark';
bool useBlackTheme = false;

// Error Reporting
bool enableSentryErrorTracking = false;
Expand Down Expand Up @@ -70,16 +67,6 @@ class _GeneralSettingsPageState extends State<GeneralSettingsPage> {
await prefs.setString('setting_instance_default_instance', value);
setState(() => defaultInstance = value);
break;
case 'setting_theme_type':
await prefs.setString('setting_theme_type', value);
setState(() => themeType = value);
if (context.mounted) context.read<ThemeBloc>().add(ThemeChangeEvent());
break;
case 'setting_theme_use_black_theme':
await prefs.setBool('setting_theme_use_black_theme', value);
setState(() => useBlackTheme = value);
if (context.mounted) context.read<ThemeBloc>().add(ThemeChangeEvent());
break;
case 'setting_notifications_show_inapp_update':
await prefs.setBool('setting_notifications_show_inapp_update', value);
setState(() => showInAppUpdateNotification = value);
Expand Down Expand Up @@ -117,10 +104,6 @@ class _GeneralSettingsPageState extends State<GeneralSettingsPage> {
showFullHeightImages = prefs.getBool('setting_general_show_full_height_images') ?? false;
hideNsfwPreviews = prefs.getBool('setting_general_hide_nsfw_previews') ?? true;

// Theme Settings
themeType = prefs.getString('setting_theme_type') ?? 'dark';
useBlackTheme = prefs.getBool('setting_theme_use_black_theme') ?? false;

// Notification Settings
showInAppUpdateNotification = prefs.getBool('setting_notifications_show_inapp_update') ?? true;

Expand Down Expand Up @@ -206,69 +189,6 @@ class _GeneralSettingsPageState extends State<GeneralSettingsPage> {
],
),
),
Container(
padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Text(
'Theme',
style: theme.textTheme.titleLarge,
),
),
ToggleOption(
description: 'Use dark theme',
value: themeType == 'dark',
iconEnabled: Icons.dark_mode_rounded,
iconDisabled: Icons.dark_mode_outlined,
onToggle: (bool value) => setPreferences('setting_theme_type', value == true ? 'dark' : 'light'),
),
ToggleOption(
description: 'Pure black theme',
value: useBlackTheme,
iconEnabled: Icons.dark_mode_outlined,
iconDisabled: Icons.dark_mode_outlined,
onToggle: (bool value) => setPreferences('setting_theme_use_black_theme', value),
),
// TextFormField(
// // initialValue: defaultInstance ?? '',

// controller: instanceController,
// decoration: const InputDecoration(
// prefix: Text('https://'),
// isDense: true,
// hintText: 'lemmy.ml',
// ),
// ),
// const SizedBox(height: 16.0),
// ElevatedButton(
// onPressed: () {
// setPreferences('setting_instance_default_instance', 'https://${instanceController.text}');

// LemmyClient lemmyClient = LemmyClient.instance;
// lemmyClient.changeBaseUrl('https://${instanceController.text}');
// SnackBar snackBar = SnackBar(
// content: Text('Default instance changed to ${instanceController.text}'),
// behavior: SnackBarBehavior.floating,
// );
// ScaffoldMessenger.of(context).clearSnackBars();
// ScaffoldMessenger.of(context).showSnackBar(snackBar);
// },
// child: const Text('Change default instance'),
// ),
// ToggleOption(
// description: 'Change default instance',
// value: defaultInstance,
// iconEnabled: Icons.computer_rounded,
// iconDisabled: Icons.photo_size_select_actual_rounded,
// onToggle: (bool value) => setPreferences('setting_general_show_link_previews', value),
// ),
],
),
),
Container(
padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Column(
Expand Down
3 changes: 1 addition & 2 deletions lib/settings/pages/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ class SettingsPage extends StatelessWidget {

final List<SettingTopic> topics = [
SettingTopic(title: 'General', icon: Icons.settings, path: '/settings/general'),
SettingTopic(title: 'Theming', icon: Icons.text_fields, path: '/settings/themes'),
SettingTopic(title: 'About', icon: Icons.info_rounded, path: '/settings/about'),

// SettingTopic(title: 'Appearance', icon: Icons.text_fields, path: '/settings/appearance'),
// SettingTopic(title: 'Developer', icon: Icons.developer_mode, path: '/settings/developer'),
];

Expand Down
109 changes: 109 additions & 0 deletions lib/settings/pages/theme_settings_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import 'package:shared_preferences/shared_preferences.dart';

import 'package:thunder/core/theme/bloc/theme_bloc.dart';
import 'package:thunder/settings/widgets/toggle_option.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';

class ThemeSettingsPage extends StatefulWidget {
const ThemeSettingsPage({super.key});

@override
State<ThemeSettingsPage> createState() => _ThemeSettingsPageState();
}

class _ThemeSettingsPageState extends State<ThemeSettingsPage> {
String themeType = 'dark';
bool useBlackTheme = false;

// Loading
bool isLoading = true;

void setPreferences(attribute, value) async {
final prefs = await SharedPreferences.getInstance();

switch (attribute) {
case 'setting_theme_type':
await prefs.setString('setting_theme_type', value);
setState(() => themeType = value);
if (context.mounted) context.read<ThemeBloc>().add(ThemeChangeEvent());
break;
case 'setting_theme_use_black_theme':
await prefs.setBool('setting_theme_use_black_theme', value);
setState(() => useBlackTheme = value);
if (context.mounted) context.read<ThemeBloc>().add(ThemeChangeEvent());
break;
}

if (context.mounted) {
context.read<ThunderBloc>().add(UserPreferencesChangeEvent());
}
}

void _initPreferences() async {
final prefs = await SharedPreferences.getInstance();

setState(() {
// Theme Settings
themeType = prefs.getString('setting_theme_type') ?? 'dark';
useBlackTheme = prefs.getBool('setting_theme_use_black_theme') ?? false;

isLoading = false;
});
}

@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback((_) => _initPreferences());
super.initState();
}

@override
Widget build(BuildContext context) {
final ThemeData theme = Theme.of(context);

return Scaffold(
appBar: AppBar(title: const Text('Theming'), centerTitle: false),
body: isLoading
? const Center(child: CircularProgressIndicator())
: SingleChildScrollView(
child: Column(
children: [
Container(
padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Text(
'Theme',
style: theme.textTheme.titleLarge,
),
),
ToggleOption(
description: 'Use dark theme',
value: themeType == 'dark',
iconEnabled: Icons.dark_mode_rounded,
iconDisabled: Icons.dark_mode_outlined,
onToggle: (bool value) => setPreferences('setting_theme_type', value == true ? 'dark' : 'light'),
),
ToggleOption(
description: 'Pure black theme',
value: useBlackTheme,
iconEnabled: Icons.dark_mode_outlined,
iconDisabled: Icons.dark_mode_outlined,
onToggle: (bool value) => setPreferences('setting_theme_use_black_theme', value),
),
],
),
),
],
),
),
);
}
}

0 comments on commit 3e71a0e

Please sign in to comment.