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

451 lines
12 KiB
Dart
Raw Normal View History

import 'package:universal_io/io.dart';
2019-09-23 11:08:51 +02:00
import 'dart:async';
import 'package:fimber/fimber.dart';
2020-11-01 07:24:19 +01:00
import 'package:fluro/fluro.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.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';
2020-01-27 03:47:34 +01:00
import 'package:primer/primer.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/services.dart';
2021-02-14 14:23:15 +01:00
import 'package:flutter_gen/gen_l10n/S.dart';
class DialogOption<T> {
final T value;
final Widget widget;
DialogOption({this.value, this.widget});
}
2019-09-19 15:10:50 +02:00
class AppThemeType {
static const material = 0;
static const cupertino = 1;
2019-09-19 15:10:50 +02:00
static const values = [AppThemeType.material, AppThemeType.cupertino];
}
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> {
final T value;
final String text;
PickerItem(this.value, {@required this.text});
}
class PickerGroupItem<T> {
final T value;
final List<PickerItem<T>> items;
final Function(T value) onChange;
final Function(T value) onClose;
PickerGroupItem({
@required this.value,
@required this.items,
this.onChange,
this.onClose,
});
}
2019-10-02 08:58:11 +02:00
class SelectorItem<T> {
T value;
String text;
SelectorItem({@required this.value, @required this.text});
}
2019-09-29 10:01:03 +02:00
// No animation. For replacing route
// TODO: Go back
class StaticRoute extends PageRouteBuilder {
final WidgetBuilder builder;
StaticRoute({this.builder})
: super(
pageBuilder: (BuildContext context, Animation<double> animation,
Animation<double> secondaryAnimation) {
return builder(context);
},
transitionsBuilder: (BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child) {
return child;
},
);
}
2019-11-05 14:22:41 +01:00
class Palette {
2020-01-14 11:13:07 +01:00
final Color primary;
final Color text;
final Color secondaryText;
final Color tertiaryText;
final Color background;
final Color grayBackground;
final Color border;
const Palette({
2019-11-05 14:22:41 +01:00
this.primary,
this.text,
this.secondaryText,
this.tertiaryText,
this.background,
2019-12-20 15:41:38 +01:00
this.grayBackground,
2019-11-05 14:22:41 +01:00
this.border,
});
}
class ThemeModel with ChangeNotifier {
String markdownCss;
int _theme;
int get theme => _theme;
bool get ready => _theme != null;
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
}
}
2020-01-16 05:45:04 +01:00
int _brightnessValue = AppBrightnessType.followSystem;
int get brighnessValue => _brightnessValue;
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
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
String _locale;
String get locale => _locale;
2021-02-14 06:01:09 +01:00
2021-02-14 05:05:41 +01:00
Future<void> setLocale(String v) async {
_locale = v;
final prefs = await SharedPreferences.getInstance();
await prefs.setString(StorageKeys.locale, v);
notifyListeners();
}
2020-11-01 07:24:19 +01:00
final router = FluroRouter();
2019-12-12 13:29:56 +01:00
2020-01-14 11:13:07 +01:00
final paletteLight = Palette(
2020-01-27 03:47:34 +01:00
primary: PrimerColors.blue500,
2020-01-14 11:13:07 +01:00
text: Colors.black,
secondaryText: Colors.grey.shade800,
tertiaryText: Colors.grey.shade600,
background: Colors.white,
grayBackground: Colors.grey.shade100,
2020-01-27 03:47:34 +01:00
border: Colors.grey.shade300,
2020-01-14 11:13:07 +01:00
);
final paletteDark = Palette(
2020-01-27 03:47:34 +01:00
primary: PrimerColors.blue500,
2020-01-14 11:13:07 +01:00
text: Colors.grey.shade300,
secondaryText: Colors.grey.shade400,
tertiaryText: Colors.grey.shade500,
background: Colors.black,
grayBackground: Colors.grey.shade900,
border: Colors.grey.shade700,
);
2020-01-27 08:11:51 +01:00
Palette get palette {
2020-01-16 05:45:04 +01:00
switch (brightness) {
2019-11-05 14:22:41 +01:00
case Brightness.dark:
2020-01-14 11:13:07 +01:00
return paletteDark;
2020-01-27 08:11:51 +01:00
case Brightness.light:
2019-11-05 14:22:41 +01:00
default:
2020-01-27 08:11:51 +01:00
return paletteLight;
2019-11-05 14:22:41 +01:00
}
}
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 v = prefs.getInt(StorageKeys.iTheme);
Fimber.d('read theme: $v');
2019-09-19 15:10:50 +02:00
if (AppThemeType.values.contains(v)) {
_theme = v;
2020-11-14 09:39:00 +01:00
} else if (Platform.isIOS || Platform.isMacOS) {
2019-09-19 15:10:50 +02:00
_theme = AppThemeType.cupertino;
} else {
2019-09-19 15:10:50 +02:00
_theme = AppThemeType.material;
}
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();
}
Future<void> setTheme(int v) async {
_theme = v;
2020-01-13 14:07:28 +01:00
final prefs = await SharedPreferences.getInstance();
2020-01-16 05:52:47 +01:00
await prefs.setInt(StorageKeys.iTheme, v);
Fimber.d('write theme: $v');
notifyListeners();
}
2019-12-25 03:46:31 +01:00
push(BuildContext context, String url, {bool replace = false}) {
2020-02-01 03:32:29 +01:00
// Fimber.d(url);
2019-12-25 03:46:31 +01:00
if (url.startsWith('/')) {
return router.navigateTo(
context,
url,
2020-04-06 07:18:23 +02:00
transition: theme == AppThemeType.cupertino
2020-11-01 07:24:19 +01:00
? TransitionType.cupertino
: TransitionType.material,
2019-12-25 03:46:31 +01:00
replace: replace,
);
} else {
launchUrl(url);
}
2019-12-12 13:29:56 +01:00
}
2020-02-08 08:06:36 +01:00
Future<void> showWarning(BuildContext context, String message) async {
showCupertinoDialog(
context: context,
builder: (context) {
return CupertinoAlertDialog(
title: Text(message),
actions: <Widget>[
CupertinoDialogAction(
child: const Text('OK'),
onPressed: () {
Navigator.pop(context, true);
},
),
],
);
},
);
}
2019-10-03 06:55:17 +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(
child: const Text('cancel'),
isDefaultAction: true,
onPressed: () {
Navigator.pop(context, false);
},
),
CupertinoDialogAction(
child: const Text('OK'),
onPressed: () {
Navigator.pop(context, true);
},
),
],
);
2020-02-08 05:53:56 +01:00
},
);
// default:
// return showDialog(
// context: context,
// builder: (BuildContext context) {
// return AlertDialog(
// content: content,
// actions: <Widget>[
// FlatButton(
// child: const Text('CANCEL'),
// onPressed: () {
// Navigator.pop(context, false);
// },
// ),
// FlatButton(
// child: const Text('OK'),
// onPressed: () {
// Navigator.pop(context, true);
// },
// )
// ],
// );
// },
// );
}
2019-10-02 08:58:11 +02:00
static Timer _debounce;
2019-09-29 09:35:33 +02:00
String _selectedItem;
2019-09-23 11:08:51 +02:00
showPicker(BuildContext context, PickerGroupItem<String> groupItem) async {
2020-02-08 05:53:56 +01:00
await showCupertinoModalPopup(
context: context,
builder: (context) {
return Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
alignment: Alignment.bottomCenter,
decoration: BoxDecoration(
color: palette.background,
border: Border(
bottom: BorderSide(
color: palette.grayBackground,
width: 0.0,
),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
CupertinoButton(
child: Text('Cancel'),
onPressed: () {
Navigator.pop(context);
_selectedItem = groupItem.value;
},
padding: const EdgeInsets.symmetric(
horizontal: 16.0,
vertical: 5.0,
),
),
CupertinoButton(
child: Text('Confirm'),
onPressed: () {
Navigator.pop(context);
groupItem.onClose(_selectedItem);
},
padding: const EdgeInsets.symmetric(
horizontal: 16.0,
vertical: 5.0,
),
)
],
),
),
Container(
height: 216,
child: CupertinoPicker(
backgroundColor: palette.background,
children: <Widget>[
for (var v in groupItem.items)
Text(v.text, style: TextStyle(color: palette.text)),
],
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) {
_debounce.cancel();
}
_debounce = Timer(const Duration(milliseconds: 500), () {
groupItem.onChange(_selectedItem);
});
}
},
),
)
],
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 {
2019-12-12 07:02:48 +01:00
if (actionItems == null) return;
2019-11-03 16:33:24 +01:00
final value = await showCupertinoModalPopup<int>(
context: context,
builder: (BuildContext context) {
return CupertinoActionSheet(
title: Text('Actions'),
actions: actionItems.asMap().entries.map((entry) {
return CupertinoActionSheetAction(
child: Text(entry.value.text),
2020-02-01 11:30:32 +01:00
isDestructiveAction: entry.value.isDestructiveAction,
2019-11-03 16:33:24 +01:00
onPressed: () {
Navigator.pop(context, entry.key);
},
);
}).toList(),
cancelButton: CupertinoActionSheetAction(
child: const Text('Cancel'),
isDefaultAction: true,
onPressed: () {
Navigator.pop(context);
},
),
);
},
);
if (value != null) {
2020-01-01 13:59:20 +01:00
actionItems[value].onTap(context);
2019-11-03 16:33:24 +01:00
}
}
}