Skip to content

Commit

Permalink
feat: code settings
Browse files Browse the repository at this point in the history
  • Loading branch information
pd4d10 committed Sep 15, 2019
1 parent 9bc5c2b commit a47d84a
Show file tree
Hide file tree
Showing 18 changed files with 275 additions and 44 deletions.
5 changes: 4 additions & 1 deletion lib/main.dart
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:git_touch/models/code.dart';
import 'package:git_touch/models/settings.dart';
import 'package:git_touch/models/theme.dart';
import 'package:git_touch/screens/issues.dart';
Expand Down Expand Up @@ -34,6 +35,7 @@ class _HomeState extends State<Home> {
// FIXME:
Provider.of<ThemeModel>(context).init();
Provider.of<SettingsModel>(context).init();
Provider.of<CodeModel>(context).init();
});
}

Expand Down Expand Up @@ -134,7 +136,7 @@ class _HomeState extends State<Home> {
}

switch (Provider.of<ThemeModel>(context).theme) {
case ThemeMap.cupertino:
case AppThemeMap.cupertino:
return CupertinoApp(
home: CupertinoTheme(
data: CupertinoThemeData(
Expand Down Expand Up @@ -181,6 +183,7 @@ class App extends StatelessWidget {
ChangeNotifierProvider(builder: (context) => NotificationModel()),
ChangeNotifierProvider(builder: (context) => ThemeModel()),
ChangeNotifierProvider(builder: (context) => SettingsModel()),
ChangeNotifierProvider(builder: (context) => CodeModel()),
],
child: Home(),
);
Expand Down
71 changes: 71 additions & 0 deletions lib/models/code.dart
@@ -0,0 +1,71 @@
import 'package:flutter/foundation.dart';
import 'package:flutter_highlight/theme_map.dart';
import 'package:shared_preferences/shared_preferences.dart';

class CodeModel with ChangeNotifier {
static const _kTheme = 'code-theme';
static const _kFontSize = 'code-font-size';
static const _kFontFamily = 'code-font-family';

static var themes = themeMap.keys.toList();
static const fontSizes = [12, 13, 14, 15, 16, 17, 18, 19, 20];
static const fontFamilies = ['System'];

String _theme = 'github';
int _fontSize = 14;
String _fontFamily = 'System';

String get theme => _theme;
int get fontSize => _fontSize;
String get fontFamily => _fontFamily;

init() async {
var prefs = await SharedPreferences.getInstance();
var vh = prefs.getString(_kTheme);
var vs = prefs.getInt(_kFontSize);
var vf = prefs.getString(_kFontFamily);

print('read code: $vh, $vs, $vf');
if (themeMap.keys.contains(vh)) {
_theme = vh;
}
if (fontSizes.contains(vs)) {
_fontSize = vs;
}
if (fontFamilies.contains(vf)) {
_fontFamily = vf;
}

notifyListeners();
}

setTheme(String v) async {
var prefs = await SharedPreferences.getInstance();

await prefs.setString(_kTheme, v);
print('write code theme: $v');

_theme = v;
notifyListeners();
}

setFontSize(int v) async {
var prefs = await SharedPreferences.getInstance();

await prefs.setInt(_kFontSize, v);
print('write code font size: $v');

_fontSize = v;
notifyListeners();
}

setFontFamily(String v) async {
var prefs = await SharedPreferences.getInstance();

await prefs.setString(_kFontFamily, v);
print('write code font family: $v');

_fontFamily = v;
notifyListeners();
}
}
50 changes: 42 additions & 8 deletions lib/models/theme.dart
Expand Up @@ -10,10 +10,10 @@ class DialogOption<T> {
DialogOption({this.value, this.widget});
}

class ThemeMap {
class AppThemeMap {
static const material = 0;
static const cupertino = 1;
static const values = [ThemeMap.material, ThemeMap.cupertino];
static const values = [AppThemeMap.material, AppThemeMap.cupertino];
}

class ThemeModel with ChangeNotifier {
Expand All @@ -28,12 +28,12 @@ class ThemeModel with ChangeNotifier {

int v = prefs.getInt(storageKey);
print('read theme: $v');
if (ThemeMap.values.contains(v)) {
if (AppThemeMap.values.contains(v)) {
_theme = v;
} else if (Platform.isIOS) {
_theme = ThemeMap.cupertino;
_theme = AppThemeMap.cupertino;
} else {
_theme = ThemeMap.material;
_theme = AppThemeMap.material;
}

notifyListeners();
Expand All @@ -55,7 +55,7 @@ class ThemeModel with ChangeNotifier {
bool fullscreenDialog = false,
}) {
switch (theme) {
case ThemeMap.cupertino:
case AppThemeMap.cupertino:
Navigator.of(context).push(CupertinoPageRoute(
builder: builder,
fullscreenDialog: fullscreenDialog,
Expand All @@ -71,7 +71,7 @@ class ThemeModel with ChangeNotifier {

Future<bool> showConfirm(BuildContext context, String text) {
switch (theme) {
case ThemeMap.cupertino:
case AppThemeMap.cupertino:
return showCupertinoDialog(
context: context,
builder: (context) {
Expand Down Expand Up @@ -130,7 +130,7 @@ class ThemeModel with ChangeNotifier {
var cancelWidget = Text('Cancel');

switch (theme) {
case ThemeMap.cupertino:
case AppThemeMap.cupertino:
return showCupertinoDialog<T>(
context: context,
builder: (BuildContext context) {
Expand Down Expand Up @@ -181,4 +181,38 @@ class ThemeModel with ChangeNotifier {
);
}
}

Future<T> showPicker<T>(
BuildContext context, {
@required int initialItem,
@required List<Widget> children,
@required Function(int) onSelectedItemChanged,
}) {
switch (theme) {
case AppThemeMap.cupertino:
return showCupertinoModalPopup<T>(
context: context,
builder: (context) {
return Container(
height: 300,
child: CupertinoPicker(
backgroundColor: CupertinoColors.white,
children: children,
itemExtent: 40,
scrollController:
FixedExtentScrollController(initialItem: initialItem),
onSelectedItemChanged: onSelectedItemChanged,
),
);
},
);
default:
return showModalBottomSheet<T>(
context: context,
builder: (context) {
return null; // TODO:
},
);
}
}
}
2 changes: 1 addition & 1 deletion lib/scaffolds/list.dart
Expand Up @@ -184,7 +184,7 @@ class _ListScaffoldState<T, K> extends State<ListScaffold<T, K>> {
@override
Widget build(BuildContext context) {
switch (Provider.of<ThemeModel>(context).theme) {
case ThemeMap.cupertino:
case AppThemeMap.cupertino:
List<Widget> slivers = [
CupertinoSliverRefreshControl(onRefresh: _refresh)
];
Expand Down
2 changes: 1 addition & 1 deletion lib/scaffolds/long_list.dart
Expand Up @@ -181,7 +181,7 @@ class _LongListScaffoldState<T, K> extends State<LongListScaffold<T, K>> {
@override
Widget build(BuildContext context) {
switch (Provider.of<ThemeModel>(context).theme) {
case ThemeMap.cupertino:
case AppThemeMap.cupertino:
List<Widget> slivers = [
CupertinoSliverRefreshControl(onRefresh: _refresh)
];
Expand Down
2 changes: 1 addition & 1 deletion lib/scaffolds/refresh.dart
Expand Up @@ -80,7 +80,7 @@ class _RefreshScaffoldState<T> extends State<RefreshScaffold<T>> {
@override
Widget build(BuildContext context) {
switch (Provider.of<ThemeModel>(context).theme) {
case ThemeMap.cupertino:
case AppThemeMap.cupertino:
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: widget.title,
Expand Down
2 changes: 1 addition & 1 deletion lib/scaffolds/refresh_stateless.dart
Expand Up @@ -41,7 +41,7 @@ class RefreshStatelessScaffold extends StatelessWidget {
@override
Widget build(BuildContext context) {
switch (Provider.of<ThemeModel>(context).theme) {
case ThemeMap.cupertino:
case AppThemeMap.cupertino:
return CupertinoPageScaffold(
navigationBar:
CupertinoNavigationBar(middle: title, trailing: trailing),
Expand Down
8 changes: 2 additions & 6 deletions lib/scaffolds/simple.dart
Expand Up @@ -22,7 +22,7 @@ class SimpleScaffold extends StatelessWidget {
@override
Widget build(BuildContext context) {
switch (Provider.of<ThemeModel>(context).theme) {
case ThemeMap.cupertino:
case AppThemeMap.cupertino:
return CupertinoPageScaffold(
navigationBar:
CupertinoNavigationBar(middle: title, trailing: trailing),
Expand All @@ -32,11 +32,7 @@ class SimpleScaffold extends StatelessWidget {
);
default:
return Scaffold(
appBar: AppBar(
title: title,
actions: actions,
bottom: bottom,
),
appBar: AppBar(title: title, actions: actions, bottom: bottom),
body: SingleChildScrollView(child: bodyBuilder()),
);
}
Expand Down
105 changes: 105 additions & 0 deletions lib/screens/code_settings.dart
@@ -0,0 +1,105 @@
import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_highlight/flutter_highlight.dart';
import 'package:flutter_highlight/theme_map.dart';
import 'package:git_touch/models/code.dart';
import 'package:git_touch/models/theme.dart';
import 'package:git_touch/scaffolds/simple.dart';
import 'package:git_touch/utils/utils.dart';
import 'package:git_touch/widgets/app_bar_title.dart';
import 'package:git_touch/widgets/table_view.dart';
import 'package:provider/provider.dart';

class CodeSettingsScreen extends StatelessWidget {
final String code;
final String language;

CodeSettingsScreen(this.code, this.language);

static Timer _themeDebounce;

@override
Widget build(BuildContext context) {
var codeProvider = Provider.of<CodeModel>(context);

return SimpleScaffold(
title: AppBarTitle('Code theme'),
bodyBuilder: () {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
TableView(
items: [
TableViewItem(
text: Text('Syntax Highlighting'),
rightWidget: Text(codeProvider.theme),
onTap: () {
Provider.of<ThemeModel>(context).showPicker(
context,
children: CodeModel.themes.map((k) => Text(k)).toList(),
initialItem:
CodeModel.themes.indexOf(codeProvider.theme),
onSelectedItemChanged: (int value) {
if (_themeDebounce?.isActive ?? false)
_themeDebounce.cancel();
_themeDebounce =
Timer(const Duration(milliseconds: 500), () {
Provider.of<CodeModel>(context)
.setTheme(CodeModel.themes[value]);
});
},
);
}),
TableViewItem(
text: Text('Font Size'),
rightWidget: Text(codeProvider.fontSize.toString()),
onTap: () {
Provider.of<ThemeModel>(context).showPicker(
context,
children: CodeModel.fontSizes
.map((k) => Text(k.toString()))
.toList(),
initialItem:
CodeModel.fontSizes.indexOf(codeProvider.fontSize),
onSelectedItemChanged: (int value) {
if (_themeDebounce?.isActive ?? false)
_themeDebounce.cancel();
_themeDebounce =
Timer(const Duration(milliseconds: 500), () {
Provider.of<CodeModel>(context)
.setFontSize(CodeModel.fontSizes[value]);
});
},
);
},
),
TableViewItem(
text: Text('Font Family'),
rightWidget: Text(codeProvider.fontFamily.toString()),
onTap: () {
// TODO:
},
),
],
),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: HighlightView(
code,
language: language,
theme: themeMap[codeProvider.theme],
textStyle: TextStyle(
fontSize: codeProvider.fontSize.toDouble(),
fontFamily: monospaceFont,
),
padding: const EdgeInsets.all(10),
),
)
],
);
},
);
}
}
2 changes: 1 addition & 1 deletion lib/screens/notifications.dart
Expand Up @@ -184,7 +184,7 @@ $key: pullRequest(number: ${item.number}) {

Widget _buildTitle() {
switch (Provider.of<ThemeModel>(context).theme) {
case ThemeMap.cupertino:
case AppThemeMap.cupertino:
// var textStyle = DefaultTextStyle.of(context).style;
return DefaultTextStyle(
style: TextStyle(fontSize: 16),
Expand Down

0 comments on commit a47d84a

Please sign in to comment.