From e99419d5a76e845c79f033d9af6492cf03a60e82 Mon Sep 17 00:00:00 2001 From: Stephen Brown Date: Mon, 9 Sep 2024 22:51:28 +0200 Subject: [PATCH] Switch settings to riverpod (maybe change to one obj later) --- lib/src/providers/jiraapi_provider.dart | 7 ++- lib/src/providers/jiraapi_provider.g.dart | 2 +- lib/src/providers/settings_provider.dart | 67 +++++++++++----------- lib/src/providers/settings_provider.g.dart | 38 ++++++++++++ lib/src/screens/home.dart | 11 ++-- lib/src/screens/settings.dart | 13 ++--- 6 files changed, 90 insertions(+), 48 deletions(-) create mode 100644 lib/src/providers/settings_provider.g.dart diff --git a/lib/src/providers/jiraapi_provider.dart b/lib/src/providers/jiraapi_provider.dart index 5bf63c6..c8a2dfc 100644 --- a/lib/src/providers/jiraapi_provider.dart +++ b/lib/src/providers/jiraapi_provider.dart @@ -13,12 +13,13 @@ Future issue(IssueRef ref, String jiraId) async { // Using package:http, we fetch a random activity from the Bored API. print('Fetching issue $jiraId'); - var settings = ref.watch(settingsProvider); - var url = '${settings.jiraUrl}/rest/api/2/issue/$jiraId'; + var jiraUrl = ref.watch(jiraUrlProvider); + var jiraPat = ref.watch(jiraPatProvider); + var url = '$jiraUrl/rest/api/2/issue/$jiraId'; final response = await http.get( Uri.parse(url), headers: { - 'Authorization': 'Bearer ${settings.jiraPat}', + 'Authorization': 'Bearer $jiraPat', 'Content-Type': 'application/json', }, ); diff --git a/lib/src/providers/jiraapi_provider.g.dart b/lib/src/providers/jiraapi_provider.g.dart index 659fa6f..8e3cd9e 100644 --- a/lib/src/providers/jiraapi_provider.g.dart +++ b/lib/src/providers/jiraapi_provider.g.dart @@ -6,7 +6,7 @@ part of 'jiraapi_provider.dart'; // RiverpodGenerator // ************************************************************************** -String _$issueHash() => r'2c7e8bda5dc0c11f287ba06cb7614f674f59474b'; +String _$issueHash() => r'e4d5047c4b063fa082c29def02f9dee7f222d37e'; /// Copied from Dart SDK class _SystemHash { diff --git a/lib/src/providers/settings_provider.dart b/lib/src/providers/settings_provider.dart index c161934..558bee6 100644 --- a/lib/src/providers/settings_provider.dart +++ b/lib/src/providers/settings_provider.dart @@ -1,47 +1,50 @@ -import 'package:flutter/foundation.dart'; -import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:shared_preferences/shared_preferences.dart'; +import 'package:riverpod_annotation/riverpod_annotation.dart'; +import 'package:worklog_assistant/src/storage/prefs.dart'; -class SettingsProvider with ChangeNotifier { - String _jiraUrl = ""; - String get jiraUrl => _jiraUrl; +part 'settings_provider.g.dart'; - SettingsProvider() { - loadSettings(); - } +@riverpod +class JiraUrl extends _$JiraUrl { + @override + String? build() { + final prefs = ref.watch(prefsProvider).requireValue; - Future loadSettings() async { - SharedPreferences prefs = await SharedPreferences.getInstance(); + // Load the saved theme mode setting from shared preferences. + final jiraUrl = prefs.getString('jiraUrl'); - _jiraUrl = prefs.getString("jiraUrl") ?? ""; - _jiraPat = prefs.getString("jiraPat") ?? ""; - notifyListeners(); + // Return [ThemeMode] based on the saved setting, or [ThemeMode.system] + // if there's no saved setting yet. + return jiraUrl; } - updateJiraUrl(String url) async { - _jiraUrl = url; - SharedPreferences prefs = await SharedPreferences.getInstance(); + void set(String jiraUrl) { + final prefs = ref.read(prefsProvider).requireValue; - prefs.setString("jiraUrl", url); + prefs.setString('jiraUrl', jiraUrl); - notifyListeners(); + ref.invalidateSelf(); } +} + +@riverpod +class JiraPat extends _$JiraPat { + @override + String? build() { + final prefs = ref.watch(prefsProvider).requireValue; - String _jiraPat = ""; - String get jiraPat => _jiraPat; + // Load the saved theme mode setting from shared preferences. + final jiraPat = prefs.getString('jiraPat'); - updateJiraPat(String pat) async { - _jiraPat = pat; - SharedPreferences prefs = await SharedPreferences.getInstance(); + // Return [ThemeMode] based on the saved setting, or [ThemeMode.system] + // if there's no saved setting yet. + return jiraPat; + } + + void set(String jiraPat) { + final prefs = ref.read(prefsProvider).requireValue; - prefs.setString("jiraPat", pat); + prefs.setString('jiraPat', jiraPat); - notifyListeners(); + ref.invalidateSelf(); } } - -final settingsProvider = ChangeNotifierProvider((ref) { - var s = SettingsProvider(); - s.loadSettings(); - return s; -}); diff --git a/lib/src/providers/settings_provider.g.dart b/lib/src/providers/settings_provider.g.dart new file mode 100644 index 0000000..3288982 --- /dev/null +++ b/lib/src/providers/settings_provider.g.dart @@ -0,0 +1,38 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'settings_provider.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +String _$jiraUrlHash() => r'2511c3fe1710f57fd3e419e36855a6d650863840'; + +/// See also [JiraUrl]. +@ProviderFor(JiraUrl) +final jiraUrlProvider = AutoDisposeNotifierProvider.internal( + JiraUrl.new, + name: r'jiraUrlProvider', + debugGetCreateSourceHash: + const bool.fromEnvironment('dart.vm.product') ? null : _$jiraUrlHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef _$JiraUrl = AutoDisposeNotifier; +String _$jiraPatHash() => r'fe1034879d74a7703bd14fe549d530956d6039f4'; + +/// See also [JiraPat]. +@ProviderFor(JiraPat) +final jiraPatProvider = AutoDisposeNotifierProvider.internal( + JiraPat.new, + name: r'jiraPatProvider', + debugGetCreateSourceHash: + const bool.fromEnvironment('dart.vm.product') ? null : _$jiraPatHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef _$JiraPat = AutoDisposeNotifier; +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member diff --git a/lib/src/screens/home.dart b/lib/src/screens/home.dart index 6965973..683ae90 100644 --- a/lib/src/screens/home.dart +++ b/lib/src/screens/home.dart @@ -31,14 +31,13 @@ class HomePageState extends riverpod.ConsumerState with PageMixin { assert(debugCheckHasFluentTheme(context)); final jiraProviderRef = ref.watch(jiraProvider); - final settingsProviderRef = ref.watch(settingsProvider); return ScaffoldPage( header: PageHeader( title: const Text('Worklog Assistant'), commandBar: Row(mainAxisAlignment: MainAxisAlignment.end, children: [ FilledButton( - onPressed: () => uploadWorklogs(jiraProviderRef, settingsProviderRef), + onPressed: () => uploadWorklogs(ref, jiraProviderRef), child: Row( children: [ Icon(FluentIcons.cloud_upload), @@ -59,16 +58,18 @@ class HomePageState extends riverpod.ConsumerState with PageMixin { ); } - uploadWorklogs(JiraProvider jiraModel, SettingsProvider settings) async { + uploadWorklogs(riverpod.WidgetRef ref, JiraProvider jiraModel) async { Future submitWorklogs(String jiraId, Duration timeLogged, DateTime startTime) { - var url = '${settings.jiraUrl}/rest/api/2/issue/$jiraId/worklog?adjustEstimate=leave'; + var jiraUrl = ref.watch(jiraUrlProvider); + var jiraPat = ref.watch(jiraPatProvider); + var url = '$jiraUrl/rest/api/2/issue/$jiraId/worklog?adjustEstimate=leave'; var body = jsonEncode({"started": formatForJiraTime(startTime), "timeSpentSeconds": max(timeLogged.inSeconds, 60)}); print(body); return http.post(Uri.parse(url), headers: { - 'Authorization': 'Bearer ${settings.jiraPat}', + 'Authorization': 'Bearer $jiraPat', 'Content-Type': 'application/json', }, body: body); diff --git a/lib/src/screens/settings.dart b/lib/src/screens/settings.dart index 13173fb..5335de2 100644 --- a/lib/src/screens/settings.dart +++ b/lib/src/screens/settings.dart @@ -15,9 +15,10 @@ class SettingsView extends riverpod.ConsumerWidget { @override Widget build(BuildContext context, riverpod.WidgetRef ref) { - var settings = ref.watch(settingsProvider); - jiraUrlController.text = settings.jiraUrl; - patController.text = settings.jiraPat; + var jiraUrl = ref.watch(jiraUrlProvider); + var jiraPat = ref.watch(jiraPatProvider); + jiraUrlController.text = jiraUrl ?? ""; + patController.text = jiraPat ?? ""; return ScaffoldPage.scrollable(header: const PageHeader(title: Text('Settings')), children: [ Padding( @@ -159,9 +160,7 @@ class SettingsView extends riverpod.ConsumerWidget { } saveSettings(riverpod.WidgetRef ref) { - var settings = ref.watch(settingsProvider.notifier); - - settings.updateJiraUrl(jiraUrlController.text); - settings.updateJiraPat(patController.text); + ref.read(jiraUrlProvider.notifier).set(jiraUrlController.text); + ref.read(jiraPatProvider.notifier).set(patController.text); } }