Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Commit

Permalink
feat: rewrite config + new option
Browse files Browse the repository at this point in the history
  • Loading branch information
Tricked-dev committed Mar 2, 2022
1 parent e303878 commit 1247326
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 111 deletions.
112 changes: 68 additions & 44 deletions lib/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// You should have received a copy of the license along with this
// work. If not, see <http://creativecommons.org/licenses/by-nc-nd/3.0/>.

import 'dart:convert';
import 'dart:io';

import 'package:flutter/foundation.dart';
Expand All @@ -16,68 +17,91 @@ import 'package:tmodinstaller/src/models/models.dart';
import 'package:tmodinstaller/src/utils.dart';

class Config {
static SharedPreferences? preferences;
static String _directory = "";
static String _appDir = "";
static bool _icons = true;
static bool _newMenu = true;
static File _configFile = File("");
static String _appdir = "";
static Map _config = json.decode(_configFile.readAsStringSync());
static late Isar isar;

static Future<void> initializePreference() async {
preferences = await SharedPreferences.getInstance();
var dir = Config.preferences?.getString("modfolder");
if (dir == null) {
_directory = defaultMinecraft[defaultTargetPlatform]!;
Config.preferences?.setString("modfolder", _directory);
} else {
_directory = dir;
}
_newMenu = preferences?.getBool("new_menu") ?? true;
_icons = preferences?.getBool("icons") ?? true;

var appdir = Config.preferences?.getString("appdir");
if (appdir == null) {
_appDir = defaultDirectories[defaultTargetPlatform] ??
(await getApplicationSupportDirectory()).path;
Directory(_appDir).createSync(recursive: true);
} else {
_appDir = appdir;
}
}

static Future<void> initDb() async {
isar = await Isar.open(
schemas: [InstalledModSchema, VersionSchema],
name: "data",
directory: Config.appDir,
inspector: true,
directory: _appdir,
inspector: false,
);
}

static set directory(String v) {
Config.preferences?.setString("modfolder", v);
_directory = v;
static init() async {
_appdir = defaultDirectories[TargetPlatform.linux] ??
(await getApplicationSupportDirectory()).path;
_configFile = File("$_appdir/settings.json");

if (!_configFile.existsSync()) {
_configFile.createSync(recursive: true);
_configFile.writeAsStringSync(json.encode({}));
}
await initDb();
}

Config(File configFile) {
_configFile = configFile;
_config = json.decode(configFile.readAsStringSync());
}

static String get directory {
return _directory;
static final Map defaultConfigMap = {
"icons": true,
"theme": 0,
"color": -1,
"use_top_nav": false,
"mod_repos": ["std"],
"mod_folder": "${defaultMinecraft[defaultTargetPlatform]}/mods",
};

static void change(String key, value) {
Config(_configFile).Change(key, value);
}

static set appDir(String v) {
Config.preferences?.setString("appdir", v);
_appDir = v;
void Change(String key, value) {
_config[key] = value;
Save();
}

static String get appDir {
return _appDir;
static Map toMap() {
return Config(_configFile).ToMap();
}

Map ToMap() {
return _config;
}

static dynamic getValue(String key, {dynamic defaultValue}) {
return Config(_configFile).GetValue(key, defaultValue: defaultValue);
}

dynamic GetValue(String key, {dynamic defaultValue}) {
if (!_config.containsKey(key)) {
_config[key] = defaultConfigMap[key] ?? defaultValue;
Save();
}
return _config[key] ?? defaultValue;
}

static set icons(bool v) {
Config.preferences?.setBool("icons", v);
_icons = v;
static void save() {
Config(_configFile).Save();
}

static bool get icons {
return _icons;
void Save() {
try {
_configFile.writeAsStringSync(json.encode(_config));
} on FileSystemException {}
}

static set appDir(String v) {
Config.change("app_dir", v);
_appdir = v;
}

static String get appDir {
return _appdir;
}
}
37 changes: 19 additions & 18 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void main(List<String> args) async {
WidgetsFlutterBinding.ensureInitialized();

setPathUrlStrategy();
await Config.initializePreference();
await Config.init();
await Future.wait([
flutter_acrylic.Window.initialize(),
fetchData(),
Expand All @@ -32,14 +32,10 @@ void main(List<String> args) async {

var parser = ArgParser();
parser.addOption("moddir",
abbr: "d", callback: (v) => v != null ? Config.directory = v : null);
parser.addFlag("icon", abbr: "i", callback: (v) => Config.icons = v);
parser.addOption("appdir",
abbr: "a", callback: (v) => v != null ? Config.appDir = v : null);
abbr: "d",
callback: (v) => v != null ? Config.change("mod_folder", v) : null);
parser.parse(args);

await Config.initDb();

windowManager.waitUntilReadyToShow().then((_) async {
// Hide window title bar

Expand All @@ -65,16 +61,19 @@ class TModInstallerApp extends StatelessWidget {
builder: (context, _) {
final appTheme = context.watch<AppTheme>();
//Quick and dirty way to set the color!
var color = Config.preferences?.getInt("color");
var color = Config.getValue("color");
if (color != null) {
if (color == -1) {
appTheme.rawColor = systemAccentColor;
} else {
appTheme.rawColor = Colors.accentColors[color];
}
}
if (Config.getValue("use_top_nav", defaultValue: false)) {
appTheme.rawDisplayMode = PaneDisplayMode.top;
}

var theme = Config.preferences?.getInt("theme");
var theme = Config.getValue("theme");
if (theme != null) {
appTheme.rawMode = ThemeMode.values[theme];
}
Expand Down Expand Up @@ -198,15 +197,17 @@ class _TModInstallerPageState extends State<TModInstallerPage> {
openMinWidth: 250,
openMaxWidth: 320,
),
header: Container(
height: kOneLineTileHeight,
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: SvgPicture.asset("assets/Logo.svg")
// child: const FlutterLogo(
// style: FlutterLogoStyle.horizontal,
// size: 100,
// ),
),
header: !Config.getValue("use_top_nav", defaultValue: false)
? Container(
height: kOneLineTileHeight,
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: SvgPicture.asset("assets/Logo.svg")
// child: const FlutterLogo(
// style: FlutterLogoStyle.horizontal,
// size: 100,
// ),
)
: null,
displayMode: appTheme.displayMode,
// indicatorBuilder: () {
// switch (appTheme.indicator) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/screens/launcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class _LauncherState extends State<Launcher> {
style: FluentTheme.of(context).typography.subtitle),
spacer,
flutter.SelectableText(
"Current directory $_modfolder ${_modfolder == Config.directory ? "(Default)" : ""}",
"Current directory $_modfolder ${_modfolder == Config.getValue("mod_folder") ? "(Default)" : ""}",
),
spacer,
FilledButton(
Expand Down
9 changes: 1 addition & 8 deletions lib/src/screens/mod.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@
// You should have received a copy of the license along with this
// work. If not, see <http://creativecommons.org/licenses/by-nc-nd/3.0/>.

// TMOD Installer (c) by tricked
//
// TMOD Installer is licensed under a
// Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
//
// You should have received a copy of the license along with this
// work. If not, see <http://creativecommons.org/licenses/by-nc-nd/3.0/>.
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:markdown/markdown.dart' as md;
import 'package:fluent_ui/fluent_ui.dart';
Expand Down Expand Up @@ -74,7 +67,7 @@ class _ModScreenState extends State<ModScreen> {
children: <Widget>[
Row(
children: [
if (Config.icons)
if (Config.getValue("icons"))
Padding(
padding: const EdgeInsets.only(right: 14),
child: Image.network(
Expand Down
2 changes: 1 addition & 1 deletion lib/src/screens/modlist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class _ModLists extends State<ModListsPage> {
),
child: Row(children: <Widget>[
const SizedBox(height: 100),
if (Config.icons)
if (Config.getValue("icons"))
Padding(
padding: const EdgeInsets.only(right: 14),
child: Image.network(
Expand Down
39 changes: 31 additions & 8 deletions lib/src/screens/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class _SettingsState extends State<Settings> {
String _modfolder = "";
@override
Widget build(BuildContext context) {
var modfolder = Config.preferences?.getString("modfolder");
var modfolder = Config.getValue("mod_folder");

final appTheme = context.watch<AppTheme>();
final tooltipThemeData = TooltipThemeData(decoration: () {
Expand Down Expand Up @@ -103,7 +103,7 @@ class _SettingsState extends State<Settings> {
checked: appTheme.mode == mode,
onChanged: (value) {
if (value) {
Config.preferences?.setInt("theme", index);
Config.change("theme", index);
appTheme.mode = mode;
}
},
Expand Down Expand Up @@ -150,7 +150,7 @@ class _SettingsState extends State<Settings> {
suffix: IconButton(
icon: const Icon(FluentIcons.add_to),
onPressed: () async {
Config.preferences?.setStringList("repos", current.split(","));
Config.change("mod_repos", current.split(","));
fetchData();
setState(() {});
// _clearController.clear();
Expand All @@ -167,17 +167,39 @@ class _SettingsState extends State<Settings> {
Row(
children: [
Checkbox(
checked: !Config.icons,
checked: !Config.getValue("icons"),
onChanged: (value) => setState(() {
if (value != null && value == false) {
Config.icons = true;
Config.change("icons", true);
} else {
Config.icons = false;
Config.change("icons", false);
}
}),
),
],
),
biggerSpacer,
Text("Use top nav", style: FluentTheme.of(context).typography.subtitle),
const flutter.SelectableText(
"You shouldn't want this",
),
spacer,
Row(
children: [
Checkbox(
checked: Config.getValue("use_top_nav"),
onChanged: (value) => setState(() {
if (value != null) {
value == true
? appTheme.displayMode = PaneDisplayMode.top
: appTheme.displayMode = PaneDisplayMode.auto;
Config.change("use_top_nav", value);
}
}),
),
],
),

biggerSpacer,
Text("Mod folder", style: FluentTheme.of(context).typography.subtitle),
flutter.SelectableText(
Expand All @@ -194,7 +216,8 @@ class _SettingsState extends State<Settings> {
var r = await Directory(dir).exists();
if (r) {
setState(() {
Config.preferences?.setString("modfolder", dir);
Config.change("mod_folder", dir);
// Config.preferences?.setString("/modfolder", dir);
});
setState(() {});
} else {
Expand Down Expand Up @@ -286,7 +309,7 @@ class _SettingsState extends State<Settings> {
padding: const EdgeInsets.all(2.0),
child: Button(
onPressed: () {
Config.preferences?.setInt("color", index);
Config.change("color", index);
appTheme.color = color;
},
style: ButtonStyle(padding: ButtonState.all(EdgeInsets.zero)),
Expand Down
3 changes: 2 additions & 1 deletion lib/src/screens/updater.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ class _UpdaterState extends State<Updater> {

child: Row(children: <Widget>[
const SizedBox(height: 100),
if (Config.icons && foundMod != null)
if (Config.getValue("icons", defaultValue: true) &&
foundMod != null)
Padding(
padding: const EdgeInsets.only(right: 14),
child: Image.network(
Expand Down
Loading

0 comments on commit 1247326

Please sign in to comment.