Skip to content

Commit

Permalink
feat: migrate from SharedPreferences to CSV based configuration file (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
brainwo authored Apr 28, 2024
1 parent 700fb43 commit 188f5f1
Show file tree
Hide file tree
Showing 6 changed files with 464 additions and 104 deletions.
8 changes: 6 additions & 2 deletions lib/helper/command_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'dart:io';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:youtube_api/youtube_api.dart';

import '../model/config.dart';

List<String> _defaultData(final Object fromObject, final String command) {
late final String url;
late final String title;
Expand Down Expand Up @@ -89,9 +91,11 @@ Future<void> playFromYoutubeVideo(
await prefs.setStringList('history', historyQueue.toList());
}

final config = await UserConfig.load();

final commands = switch (mode) {
PlayMode.play => prefs.getStringList('video_play_commands'),
PlayMode.listen => prefs.getStringList('video_listen_commands'),
PlayMode.play => config.videoPlayCommand,
PlayMode.listen => config.videoListenCommand,
};

if (commands == null) return;
Expand Down
18 changes: 10 additions & 8 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:youtube_api/youtube_api.dart';

import 'const.dart';
import 'helper/command_parser.dart';
import 'intent.dart';
import 'locale/en_us.dart';
import 'model/config.dart';
import 'model/setting_options.dart';
import 'model/state.dart';
import 'model/theme.dart';
Expand Down Expand Up @@ -39,10 +39,11 @@ class _ThemedAppState extends ConsumerState<ThemedApp> {
void initState() {
WidgetsBinding.instance.addPostFrameCallback((final _) async {
final intialBrightnessMode = ref.read(brightnessModeProvider);
final prefs = await SharedPreferences.getInstance()
.then((final value) => value.getString('theme_brightness'));
if (prefs != null && prefs != intialBrightnessMode.name) {
ref.read(brightnessModeProvider.notifier).switchModeFromString(prefs);
final prefs = await UserConfig.load();
final userBrightness = prefs.theme.brightness;

if (userBrightness != intialBrightnessMode) {
ref.read(brightnessModeProvider.notifier).switchMode(userBrightness);
}
});

Expand Down Expand Up @@ -227,9 +228,10 @@ class _HomePageState extends State<HomePage> {
return;
}

final prefs = await SharedPreferences.getInstance();
final apiKey = prefs.getString('youtube_api_key');
final maxResults = prefs.getInt('youtube_result_per_search');
final prefs = await UserConfig.load();

final apiKey = prefs.youtube?.apiKey;
final maxResults = prefs.youtube?.resultPerSearch;

if (apiKey == null) {
// throw error
Expand Down
Loading

0 comments on commit 188f5f1

Please sign in to comment.