Skip to content

Commit

Permalink
feat: persist sydney settings
Browse files Browse the repository at this point in the history
  • Loading branch information
PeronGH committed Dec 9, 2023
1 parent 11f209a commit 1381471
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 21 deletions.
3 changes: 2 additions & 1 deletion lib/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ class Controller extends GetxController {
isGenerating.value = true;

// Add prompt to message list as user message
final userPrompt = prompt.value;
messages.add(Message(
role: Message.roleUser,
type: Message.typeMessage,
content: prompt.value));
content: userPrompt));

prompt.value = '';

Expand Down
20 changes: 12 additions & 8 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:sydney_webui/pages/chat_page.dart';

void main() => runApp(
GetMaterialApp(
home: ChatPage(),
theme: ThemeData(
colorSchemeSeed: Colors.teal, brightness: Brightness.light),
darkTheme: ThemeData(
colorSchemeSeed: Colors.teal, brightness: Brightness.dark)),
);
void main() async {
await GetStorage.init();
runApp(
GetMaterialApp(
home: ChatPage(),
theme: ThemeData(
colorSchemeSeed: Colors.teal, brightness: Brightness.light),
darkTheme: ThemeData(
colorSchemeSeed: Colors.teal, brightness: Brightness.dark)),
);
}
33 changes: 27 additions & 6 deletions lib/services/sydney_service.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:sydney_webui/utils/http.dart';

class SydneyService {
Expand All @@ -7,15 +9,35 @@ class SydneyService {
static const _defaultCookies = String.fromEnvironment("API_COOKIES");

// Instance fields
var baseUrl = Uri.tryParse(_defaultBaseUrl);
var accessToken = _defaultAccessToken;
var cookies = _defaultCookies;
final baseUrl = ''.obs;
final accessToken = ''.obs;
final cookies = ''.obs;

Map<String, dynamic> _conversation = {};

// Initializer
SydneyService() {
// Persist settings
final box = GetStorage();

// Load settings from storage
baseUrl.value = box.read('baseUrl') ?? _defaultBaseUrl;
accessToken.value = box.read('accessToken') ?? _defaultAccessToken;
cookies.value = box.read('cookies') ?? _defaultCookies;

// Save settings when they change
ever(baseUrl, (baseUrl) => box.write('baseUrl', baseUrl));
ever(accessToken, (accessToken) => box.write('accessToken', accessToken));
ever(cookies, (cookies) => box.write('cookies', cookies));
}

// Getters
Uri? get _createConversationUrl => baseUrl?.resolve("/conversation/new");
Uri? get _askStreamUrl => baseUrl?.resolve("/chat/stream");
Uri? get _createConversationUrl =>
Uri.tryParse(baseUrl.value)?.resolve("/conversation/new");

Uri? get _askStreamUrl =>
Uri.tryParse(baseUrl.value)?.resolve("/chat/stream");

Map<String, String> get _authHeaders =>
{"Authorization": "Bearer $accessToken"};

Expand All @@ -26,7 +48,6 @@ class SydneyService {
}

// Methods

Stream<MessageEvent> askStream(
{required String prompt, required String context}) async* {
if (_conversation.isEmpty) {
Expand Down
12 changes: 6 additions & 6 deletions lib/widgets/settings_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class SettingsDialog extends StatelessWidget {
Widget build(BuildContext context) {
final controller = Get.find<Controller>();

var apiBaseUrl = controller.sydneyService.baseUrl?.toString() ?? '';
var apiAccessToken = controller.sydneyService.accessToken;
var apiCookies = controller.sydneyService.cookies;
var apiBaseUrl = controller.sydneyService.baseUrl.value;
var apiAccessToken = controller.sydneyService.accessToken.value;
var apiCookies = controller.sydneyService.cookies.value;

return AlertDialog(
title: const Text('Settings'),
Expand Down Expand Up @@ -54,9 +54,9 @@ class SettingsDialog extends StatelessWidget {
TextButton(
child: const Text('Save'),
onPressed: () {
controller.sydneyService.baseUrl = Uri.tryParse(apiBaseUrl);
controller.sydneyService.accessToken = apiAccessToken;
controller.sydneyService.cookies = apiCookies;
controller.sydneyService.baseUrl.value = apiBaseUrl;
controller.sydneyService.accessToken.value = apiAccessToken;
controller.sydneyService.cookies.value = apiCookies;
Get.back();
},
),
Expand Down
72 changes: 72 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.6.6"
get_storage:
dependency: "direct main"
description:
name: get_storage
sha256: "39db1fffe779d0c22b3a744376e86febe4ade43bf65e06eab5af707dc84185a2"
url: "https://pub.dev"
source: hosted
version: "2.1.1"
lints:
dependency: transitive
description:
Expand Down Expand Up @@ -168,6 +176,62 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.8.3"
path_provider:
dependency: transitive
description:
name: path_provider
sha256: a1aa8aaa2542a6bc57e381f132af822420216c80d4781f7aa085ca3229208aaa
url: "https://pub.dev"
source: hosted
version: "2.1.1"
path_provider_android:
dependency: transitive
description:
name: path_provider_android
sha256: e595b98692943b4881b219f0a9e3945118d3c16bd7e2813f98ec6e532d905f72
url: "https://pub.dev"
source: hosted
version: "2.2.1"
path_provider_foundation:
dependency: transitive
description:
name: path_provider_foundation
sha256: "19314d595120f82aca0ba62787d58dde2cc6b5df7d2f0daf72489e38d1b57f2d"
url: "https://pub.dev"
source: hosted
version: "2.3.1"
path_provider_linux:
dependency: transitive
description:
name: path_provider_linux
sha256: f7a1fe3a634fe7734c8d3f2766ad746ae2a2884abe22e241a8b301bf5cac3279
url: "https://pub.dev"
source: hosted
version: "2.2.1"
path_provider_platform_interface:
dependency: transitive
description:
name: path_provider_platform_interface
sha256: "94b1e0dd80970c1ce43d5d4e050a9918fce4f4a775e6142424c30a29a363265c"
url: "https://pub.dev"
source: hosted
version: "2.1.1"
path_provider_windows:
dependency: transitive
description:
name: path_provider_windows
sha256: "8bc9f22eee8690981c22aa7fc602f5c85b497a6fb2ceb35ee5a5e5ed85ad8170"
url: "https://pub.dev"
source: hosted
version: "2.2.1"
platform:
dependency: transitive
description:
name: platform
sha256: "0a279f0707af40c890e80b1e9df8bb761694c074ba7e1d4ab1bc4b728e200b59"
url: "https://pub.dev"
source: hosted
version: "3.1.3"
plugin_platform_interface:
dependency: transitive
description:
Expand Down Expand Up @@ -253,6 +317,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "5.1.1"
xdg_directories:
dependency: transitive
description:
name: xdg_directories
sha256: "589ada45ba9e39405c198fe34eb0f607cddb2108527e658136120892beac46d2"
url: "https://pub.dev"
source: hosted
version: "1.0.3"
sdks:
dart: ">=3.2.3 <4.0.0"
flutter: ">=3.13.0"
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies:
flutter:
sdk: flutter
get:
get_storage:
file_picker: ^6.1.1
flutter_markdown:

Expand Down

0 comments on commit 1381471

Please sign in to comment.