git-touch-android-ios-app/lib/models/theme.dart

299 lines
8.4 KiB
Dart
Raw Normal View History

2019-09-23 11:08:51 +02:00
import 'dart:async';
2022-09-11 19:54:07 +02:00
2022-09-17 15:57:43 +02:00
import 'package:antd_mobile/antd_mobile.dart';
import 'package:fimber/fimber.dart';
import 'package:flutter/cupertino.dart';
2022-09-11 19:54:07 +02:00
import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/S.dart';
2019-12-25 03:46:31 +01:00
import 'package:git_touch/utils/utils.dart';
2019-11-03 16:33:24 +01:00
import 'package:git_touch/widgets/action_button.dart';
import 'package:shared_preferences/shared_preferences.dart';
2022-09-11 19:54:07 +02:00
import 'package:universal_io/io.dart';
2020-01-13 14:07:28 +01:00
class AppBrightnessType {
2020-01-14 11:13:07 +01:00
static const followSystem = 0;
2020-01-13 14:07:28 +01:00
static const light = 1;
static const dark = 2;
static const values = [
2020-01-14 11:13:07 +01:00
AppBrightnessType.followSystem,
2020-01-13 14:07:28 +01:00
AppBrightnessType.light,
AppBrightnessType.dark
];
}
class AppMarkdownType {
static const flutter = 0;
static const webview = 1;
static const values = [AppMarkdownType.flutter, AppMarkdownType.webview];
}
2019-09-29 09:35:33 +02:00
class PickerItem<T> {
2022-09-21 18:28:21 +02:00
PickerItem(this.value, {required this.text});
2019-09-29 09:35:33 +02:00
final T value;
2021-05-16 09:16:35 +02:00
final String? text;
2019-09-29 09:35:33 +02:00
}
class PickerGroupItem<T> {
PickerGroupItem({
2021-05-16 09:16:35 +02:00
required this.value,
required this.items,
2019-09-29 09:35:33 +02:00
this.onChange,
this.onClose,
});
2022-09-21 18:28:21 +02:00
final T value;
final List<PickerItem<T>> items;
final Function(T value)? onChange;
final Function(T value)? onClose;
2019-09-29 09:35:33 +02:00
}
2019-10-02 08:58:11 +02:00
class SelectorItem<T> {
2022-09-21 18:28:21 +02:00
SelectorItem({required this.value, required this.text});
2019-10-02 08:58:11 +02:00
T value;
String text;
}
2019-09-29 10:01:03 +02:00
// No animation. For replacing route
// TODO: Go back
class StaticRoute extends PageRouteBuilder {
StaticRoute({this.builder})
: super(
2022-09-24 09:36:22 +02:00
pageBuilder: (context, animation, secondaryAnimation) {
2021-05-16 09:16:35 +02:00
return builder!(context);
2019-09-29 10:01:03 +02:00
},
2022-09-24 09:36:22 +02:00
transitionsBuilder: (context, animation, secondaryAnimation, child) {
2019-09-29 10:01:03 +02:00
return child;
},
);
2022-09-21 18:28:21 +02:00
final WidgetBuilder? builder;
2019-09-29 10:01:03 +02:00
}
class ThemeModel with ChangeNotifier {
2021-05-16 09:16:35 +02:00
String? markdownCss;
2020-01-16 05:45:04 +01:00
Brightness systemBrightness = Brightness.light;
void setSystemBrightness(Brightness v) {
if (v != systemBrightness) {
Future.microtask(() {
systemBrightness = v;
notifyListeners();
});
2020-01-14 11:13:07 +01:00
}
}
2021-05-16 09:16:35 +02:00
int? _brightnessValue = AppBrightnessType.followSystem;
int? get brighnessValue => _brightnessValue;
2020-01-16 05:45:04 +01:00
2020-01-14 11:13:07 +01:00
// could be null
Brightness get brightness {
2020-01-13 14:07:28 +01:00
switch (_brightnessValue) {
case AppBrightnessType.light:
return Brightness.light;
case AppBrightnessType.dark:
return Brightness.dark;
default:
2020-01-16 05:45:04 +01:00
return systemBrightness;
2020-01-13 14:07:28 +01:00
}
}
Future<void> setBrightness(int v) async {
_brightnessValue = v;
final prefs = await SharedPreferences.getInstance();
2020-01-16 05:52:47 +01:00
await prefs.setInt(StorageKeys.iBrightness, v);
2020-01-13 14:07:28 +01:00
Fimber.d('write brightness: $v');
2019-11-05 14:22:41 +01:00
notifyListeners();
}
// markdown render engine
2021-05-16 09:16:35 +02:00
int? _markdown;
int? get markdown => _markdown;
Future<void> setMarkdown(int v) async {
_markdown = v;
final prefs = await SharedPreferences.getInstance();
await prefs.setInt(StorageKeys.iMarkdown, v);
Fimber.d('write markdown engine: $v');
notifyListeners();
}
2020-11-14 09:39:00 +01:00
bool get shouldUseMarkdownFlutterView {
// webview on macOS not working
if (Platform.isMacOS) return true;
// android webview has some issues, prefer flutter
// https://github.com/git-touch/git-touch/issues/132
if (Platform.isAndroid && markdown == null) return true;
// otherwise, prefer webview
return markdown == AppMarkdownType.flutter;
}
2021-02-14 05:05:41 +01:00
// supported languages
2021-05-16 09:16:35 +02:00
String? _locale;
String? get locale => _locale;
2021-02-14 06:01:09 +01:00
2021-05-30 18:08:38 +02:00
Future<void> setLocale(String? v) async {
2021-02-14 05:05:41 +01:00
_locale = v;
final prefs = await SharedPreferences.getInstance();
2021-05-30 18:08:38 +02:00
if (v == null) {
await prefs.remove(StorageKeys.locale);
} else {
await prefs.setString(StorageKeys.locale, v);
}
2021-02-14 05:05:41 +01:00
notifyListeners();
}
2019-11-05 08:09:54 +01:00
Future<void> init() async {
markdownCss = await rootBundle.loadString('images/github-markdown.css');
2020-01-13 14:07:28 +01:00
final prefs = await SharedPreferences.getInstance();
2020-01-16 05:52:47 +01:00
final b = prefs.getInt(StorageKeys.iBrightness);
2020-01-13 14:07:28 +01:00
Fimber.d('read brightness: $b');
if (AppBrightnessType.values.contains(b)) {
_brightnessValue = b;
}
final m = prefs.getInt(StorageKeys.iMarkdown);
if (AppMarkdownType.values.contains(m)) {
_markdown = m;
}
2021-02-14 05:05:41 +01:00
final l = prefs.getString(StorageKeys.locale);
2021-02-14 14:23:15 +01:00
if (AppLocalizations.supportedLocales.any((v) => l == v.toString())) {
2021-02-14 05:05:41 +01:00
_locale = l;
}
notifyListeners();
}
2021-05-16 09:16:35 +02:00
Future<bool?> showConfirm(BuildContext context, Widget content) {
2020-02-08 05:53:56 +01:00
return showCupertinoDialog(
context: context,
builder: (context) {
return CupertinoAlertDialog(
title: content,
actions: <Widget>[
CupertinoDialogAction(
isDefaultAction: true,
onPressed: () {
Navigator.pop(context, false);
},
2022-09-06 18:28:12 +02:00
child: const Text('cancel'),
2020-02-08 05:53:56 +01:00
),
CupertinoDialogAction(
child: const Text('OK'),
onPressed: () {
Navigator.pop(context, true);
},
),
],
);
2020-02-08 05:53:56 +01:00
},
);
}
2021-05-16 09:16:35 +02:00
static Timer? _debounce;
String? _selectedItem;
2019-09-29 09:35:33 +02:00
2021-05-16 09:16:35 +02:00
showPicker(BuildContext context, PickerGroupItem<String?> groupItem) async {
2022-09-18 14:37:33 +02:00
await AntPopup.show(
2020-02-08 05:53:56 +01:00
context: context,
2022-09-18 14:37:33 +02:00
closeOnMaskClick: true,
2020-02-08 05:53:56 +01:00
builder: (context) {
return Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
alignment: Alignment.bottomCenter,
decoration: BoxDecoration(
2022-09-24 20:46:37 +02:00
color: AntTheme.of(context).colorBackground,
border: Border(
bottom: BorderSide(
2022-09-24 20:46:37 +02:00
color: AntTheme.of(context).colorBox,
width: 0.0,
),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
CupertinoButton(
onPressed: () {
Navigator.pop(context);
_selectedItem = groupItem.value;
},
padding: const EdgeInsets.symmetric(
horizontal: 16.0,
vertical: 5.0,
),
2022-09-06 18:28:12 +02:00
child: const Text('Cancel'),
),
CupertinoButton(
onPressed: () {
Navigator.pop(context);
2021-05-16 09:16:35 +02:00
groupItem.onClose!(_selectedItem);
},
padding: const EdgeInsets.symmetric(
horizontal: 16.0,
vertical: 5.0,
),
2022-09-06 18:28:12 +02:00
child: const Text('Confirm'),
)
],
),
),
2022-09-06 18:28:12 +02:00
SizedBox(
height: 216,
child: CupertinoPicker(
2022-09-24 20:46:37 +02:00
backgroundColor: AntTheme.of(context).colorBackground,
itemExtent: 40,
scrollController: FixedExtentScrollController(
initialItem: groupItem.items
.toList()
.indexWhere((v) => v.value == groupItem.value)),
onSelectedItemChanged: (index) {
_selectedItem = groupItem.items[index].value;
if (groupItem.onChange != null) {
if (_debounce?.isActive ?? false) {
2021-05-16 09:16:35 +02:00
_debounce!.cancel();
}
_debounce = Timer(const Duration(milliseconds: 500), () {
2021-05-16 09:16:35 +02:00
groupItem.onChange!(_selectedItem);
});
}
},
2022-09-06 18:28:12 +02:00
children: <Widget>[
for (var v in groupItem.items)
2022-09-24 20:46:37 +02:00
Text(v.text!,
style:
TextStyle(color: AntTheme.of(context).colorText)),
2022-09-06 18:28:12 +02:00
],
),
)
],
2019-09-23 11:08:51 +02:00
);
2020-02-08 05:53:56 +01:00
},
);
2019-09-23 11:08:51 +02:00
}
2019-11-03 16:33:24 +01:00
showActions(BuildContext context, List<ActionItem> actionItems) async {
2022-09-17 15:57:43 +02:00
await AntActionSheet.show(
2019-11-03 16:33:24 +01:00
context: context,
2022-09-17 15:57:43 +02:00
extra: const Text('Actions'),
actions: [
for (final item in actionItems)
AntActionSheetAction(
text: Text(
item.text!,
2022-09-24 09:36:22 +02:00
style: TextStyle(
color: item.danger ? AntTheme.of(context).colorDanger : null),
2022-09-17 15:57:43 +02:00
),
onClick: () {
item.onTap?.call(context);
2019-11-03 16:33:24 +01:00
},
2022-09-17 15:57:43 +02:00
key: null,
2019-11-03 16:33:24 +01:00
),
2022-09-17 15:57:43 +02:00
],
2019-11-03 16:33:24 +01:00
);
}
}