Skip to content

Commit

Permalink
Switch settings to riverpod (maybe change to one obj later)
Browse files Browse the repository at this point in the history
  • Loading branch information
evolvedlight committed Sep 9, 2024
1 parent 72df858 commit e99419d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 48 deletions.
7 changes: 4 additions & 3 deletions lib/src/providers/jiraapi_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ Future<Issue> 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',
},
);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/providers/jiraapi_provider.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 35 additions & 32 deletions lib/src/providers/settings_provider.dart
Original file line number Diff line number Diff line change
@@ -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<SettingsProvider>((ref) {
var s = SettingsProvider();
s.loadSettings();
return s;
});
38 changes: 38 additions & 0 deletions lib/src/providers/settings_provider.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions lib/src/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ class HomePageState extends riverpod.ConsumerState<HomePage> 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),
Expand All @@ -59,16 +58,18 @@ class HomePageState extends riverpod.ConsumerState<HomePage> with PageMixin {
);
}

uploadWorklogs(JiraProvider jiraModel, SettingsProvider settings) async {
uploadWorklogs(riverpod.WidgetRef ref, JiraProvider jiraModel) async {
Future<http.Response> 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);
Expand Down
13 changes: 6 additions & 7 deletions lib/src/screens/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
}

0 comments on commit e99419d

Please sign in to comment.