51 lines
1.5 KiB
Dart
51 lines
1.5 KiB
Dart
|
|
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'dart:io';
|
|
|
|
class SettingsProvider extends ChangeNotifier {
|
|
|
|
String _openRouterApiKey = "";
|
|
String get openRouterApiKey => _openRouterApiKey;
|
|
|
|
late String _applicationStorageLocation;
|
|
String get applicationStorageLocation => _applicationStorageLocation;
|
|
|
|
static SettingsProvider of(BuildContext context) {
|
|
return Provider.of<SettingsProvider>(context, listen: false);
|
|
}
|
|
|
|
Future<void> load() async {
|
|
final Directory appDocDir = await getApplicationDocumentsDirectory();
|
|
_applicationStorageLocation = appDocDir.path;
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
_openRouterApiKey = prefs.getString('openrouter_api_key') ?? prefs.getString('openai_api_key') ?? _openRouterApiKey;
|
|
_applicationStorageLocation = prefs.getString('application_storage_location') ?? _applicationStorageLocation;
|
|
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> save() async {
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
await prefs.setString('openrouter_api_key', _openRouterApiKey);
|
|
await prefs.setString('application_storage_location', _applicationStorageLocation);
|
|
}
|
|
|
|
void setOpenRouterApiKey(String key) {
|
|
_openRouterApiKey = key;
|
|
save();
|
|
notifyListeners();
|
|
}
|
|
|
|
void setApplicationStorageLocation(String path) {
|
|
_applicationStorageLocation = path;
|
|
save();
|
|
notifyListeners();
|
|
}
|
|
|
|
}
|