Skip to content

Commit

Permalink
Merge pull request #199 from keshav-space/198-dark-mode-simplify
Browse files Browse the repository at this point in the history
UI: Simplify dark mode settings
  • Loading branch information
keshav-space authored May 19, 2024
2 parents cfd0ead + 483ca59 commit 60cbddf
Show file tree
Hide file tree
Showing 4 changed files with 336 additions and 164 deletions.
35 changes: 21 additions & 14 deletions lib/data/preference_and_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class PreferencesStorage {
static const _keyKeyboardIncognito = 'keyboardIcognito';
static const _keyIsInactivityTimeoutOn = 'isInactivityTimeoutOn';
static const _keyInactivityTimeout = 'inactivityTimeout';
//static const _keyFocusTimeout = 'focusTimeout';
static const _keyPreInactivityLogoutCounter = 'preInactivityLogoutCounter';
static const _keyNoOfLogginAttemptAllowed = 'noOfLogginAttemptAllowed';
static const _keyBruteforceLockOutTime = 'bruteforceLockOutTime';
Expand All @@ -45,10 +44,12 @@ class PreferencesStorage {
'biometricAttemptAllTimeCount';
static const _keyIsCompactPreview = 'isCompactPreview';
static const _keyIsDimTheme = 'isDimTheme';
static const _keyDarkModeEnum = 'isDarkModeEnum';
static const _keyDarkThemeEnum = 'isDarkThemeEnum';
static const _keyIsAutoRotate = 'isAutoRotate';
static const _keyIsBackupNeeded = 'isBackupNeeded';
static const _keyIsLocalDarkSwitchEnabled = 'isLocalDarkSwitchEnabled';
static const _keyIsSystemDarkLightSwitchEnabled =
'isSystemDarkLightSwitchEnabled';

static Future init() async =>
_preferences = await SharedPreferences.getInstance();
Expand Down Expand Up @@ -76,6 +77,13 @@ class PreferencesStorage {

static bool get isThemeDark {
bool? isDark = _preferences?.getBool(_keyIsThemeDark);
bool isSystemDark =
WidgetsBinding.instance.platformDispatcher.platformBrightness ==
Brightness.dark;

if (isSystemDarkLightSwitchEnabled) {
return isSystemDark;
}
if (isDark != null) return isDark;
return WidgetsBinding.instance.platformDispatcher.platformBrightness ==
Brightness.dark;
Expand Down Expand Up @@ -176,14 +184,6 @@ class PreferencesStorage {
static int get preInactivityLogoutCounter =>
_preferences?.getInt(_keyPreInactivityLogoutCounter) ?? 15;

// static Future<void> setFocusTimeout({required int minutes}) async {
// await _preferences?.setInt(_keyFocusTimeout, minutes * 60);
// }

// static Future<void> setPreInactivityLogoutCounter(
// {required int seconds}) async {
// await _preferences?.setInt(_keyPreInactivityLogoutCounter, seconds);
// }
static bool get isBiometricAuthEnabled =>
_preferences?.getBool(_keyIsBiometricAuthEnabled) ?? false;
static Future<void> setIsBiometricAuthEnabled(bool flag) async =>
Expand All @@ -205,10 +205,17 @@ class PreferencesStorage {
static Future<void> setIsDimTheme(bool flag) async =>
await _preferences?.setBool(_keyIsDimTheme, flag);

//Default is the device settings. i.e enumIndex = 2
static int get darkModeEnum => _preferences?.getInt(_keyDarkModeEnum) ?? 2;
static Future<void> setDarkModeEnum({required int index}) async =>
await _preferences?.setInt(_keyDarkModeEnum, index);
static bool get isLocalDarkSwitchEnabled =>
_preferences?.getBool(_keyIsLocalDarkSwitchEnabled) ?? false;

static Future<void> setLocalDarkSwitchEnabled(bool flag) async =>
await _preferences?.setBool(_keyIsLocalDarkSwitchEnabled, flag);

static bool get isSystemDarkLightSwitchEnabled =>
_preferences?.getBool(_keyIsSystemDarkLightSwitchEnabled) ?? true;

static Future<void> setSystemDarkLightSwitchEnabled(bool flag) async =>
await _preferences?.setBool(_keyIsSystemDarkLightSwitchEnabled, flag);

//Default is Dim. i.e enumIndex = 0
static int get darkThemeEnum => _preferences?.getInt(_keyDarkThemeEnum) ?? 0;
Expand Down
157 changes: 157 additions & 0 deletions lib/utils/ios_style_list_tiles.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright (C) Keshav Priyadarshi and others - All Rights Reserved.
*
* SPDX-License-Identifier: GPL-3.0-or-later
* You may use, distribute and modify this code under the
* terms of the GPL-3.0+ license.
*
* You should have received a copy of the GNU General Public License v3.0 with
* this file. If not, please visit https://www.gnu.org/licenses/gpl-3.0.html
*
* See https://safenotes.dev for support or download.
*/

// Flutter imports:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class CupertinoSwitchListTile extends StatelessWidget {
final bool value;
final ValueChanged<bool> onChanged;
final Widget title;
final Widget? subtitle;
final Widget? secondary;
final bool isThreeLine;
final bool dense;
final EdgeInsetsGeometry contentPadding;
final bool selected;

const CupertinoSwitchListTile({
Key? key,
required this.value,
required this.onChanged,
required this.title,
this.subtitle,
this.secondary,
this.isThreeLine = false,
this.dense = false,
this.contentPadding = const EdgeInsets.symmetric(horizontal: 15.0),
this.selected = false,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return MergeSemantics(
child: ListTile(
onTap: () {
onChanged(!value);
},
leading: secondary,
title: title,
subtitle: subtitle,
trailing: CupertinoSwitch(
value: value,
onChanged: onChanged,
),
isThreeLine: isThreeLine,
dense: dense,
contentPadding: contentPadding,
selected: selected,
visualDensity: VisualDensity.compact,
),
);
}
}

class CupertinoCheckListTile extends StatelessWidget {
final Widget title;
final bool value;
final ValueChanged<bool>? onChanged;
final EdgeInsetsGeometry contentPadding;

const CupertinoCheckListTile({
Key? key,
required this.title,
required this.value,
this.onChanged,
this.contentPadding = const EdgeInsets.symmetric(horizontal: 15.0),
}) : super(key: key);

@override
Widget build(BuildContext context) {
bool isEnabled = onChanged != null;
return CupertinoButton(
padding: contentPadding,
onPressed: () {
if (onChanged != null) onChanged!(!value);
},
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
title,
CupertinoCheckboxIcon(
value: value,
isEnabled: isEnabled,
),
],
),
);
}
}

class CupertinoCheckboxIcon extends StatelessWidget {
final bool value;
final bool isEnabled;

const CupertinoCheckboxIcon({
Key? key,
required this.value,
required this.isEnabled,
}) : super(key: key);

@override
Widget build(BuildContext context) {
final fillColor =
isEnabled ? CupertinoColors.activeBlue : CupertinoColors.inactiveGray;
const double iconCircleSize = 30;
const double iconCheckSize = 18;

return value
? _buildCheckedIcon(
fillColor,
iconCircleSize,
iconCheckSize,
)
: _buildUncheckedIcon(iconCircleSize);
}

Widget _buildCheckedIcon(
Color fillColor,
double iconCircleSize,
double iconCheckSize,
) {
return Stack(
alignment: Alignment.center,
children: [
Icon(
CupertinoIcons.circle_filled,
color: fillColor,
size: iconCircleSize,
),
Icon(
CupertinoIcons.check_mark,
color: CupertinoColors.white,
size: iconCheckSize,
),
],
);
}

Widget _buildUncheckedIcon(double iconCircleSize) {
return Icon(
CupertinoIcons.circle,
color: CupertinoColors.inactiveGray,
size: iconCircleSize,
);
}
}
Loading

0 comments on commit 60cbddf

Please sign in to comment.