Skip to content

Commit

Permalink
feat: modify settings
Browse files Browse the repository at this point in the history
  • Loading branch information
PeronGH committed Dec 9, 2023
1 parent 6cf0349 commit 11f209a
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 27 deletions.
4 changes: 4 additions & 0 deletions lib/controller.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sydney_webui/models/message.dart';
import 'package:sydney_webui/services/sydney_service.dart';

class Controller extends GetxController {
// Constants
static const String idMessageList = 'messageList';

// Services
final sydneyService = SydneyService();

// Controllers
final promptController = TextEditingController();

Expand Down
9 changes: 9 additions & 0 deletions lib/pages/chat_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:get/get.dart';
import 'package:sydney_webui/controller.dart';
import 'package:sydney_webui/widgets/message_list.dart';
import 'package:sydney_webui/widgets/prompt_input.dart';
import 'package:sydney_webui/widgets/settings_dialog.dart';

class ChatPage extends StatelessWidget {
final controller = Get.put(Controller());
Expand All @@ -14,6 +15,14 @@ class ChatPage extends StatelessWidget {
return Scaffold(
appBar: AppBar(
title: const Text('Sydney Chat'),
actions: [
IconButton(
onPressed: () => showDialog(
barrierDismissible: false,
context: context,
builder: const SettingsDialog().build),
icon: const Icon(Icons.settings))
],
),
body: const Padding(
padding: EdgeInsets.all(16.0),
Expand Down
34 changes: 7 additions & 27 deletions lib/services/sydney_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,25 @@ class SydneyService {
static const _defaultCookies = String.fromEnvironment("API_COOKIES");

// Instance fields
var _baseUrl = () {
try {
return Uri.parse(_defaultBaseUrl);
} catch (_) {
return null;
}
}();
var _accessToken = _defaultAccessToken;
var _cookies = _defaultCookies;
var baseUrl = Uri.tryParse(_defaultBaseUrl);
var accessToken = _defaultAccessToken;
var cookies = _defaultCookies;

Map<String, dynamic> _conversation = {};

// Getters
Uri? get _createConversationUrl => _baseUrl?.resolve("/conversation/new");
Uri? get _askStreamUrl => _baseUrl?.resolve("/chat/stream");
Uri? get _createConversationUrl => baseUrl?.resolve("/conversation/new");
Uri? get _askStreamUrl => baseUrl?.resolve("/chat/stream");
Map<String, String> get _authHeaders =>
{"Authorization": "Bearer $_accessToken"};
{"Authorization": "Bearer $accessToken"};

// Helper Methods
Future<void> _resetConversation() async {
_conversation = await postAndDecodeJson(_createConversationUrl!,
data: {"cookies": _cookies}, headers: _authHeaders);
data: {"cookies": cookies}, headers: _authHeaders);
}

// Methods
SydneyService setBaseUrl(Uri baseUrl) {
_baseUrl = baseUrl;
return this;
}

SydneyService setAccessToken(String accessToken) {
_accessToken = accessToken;
return this;
}

SydneyService setCookies(String cookies) {
_cookies = cookies;
return this;
}

Stream<MessageEvent> askStream(
{required String prompt, required String context}) async* {
Expand Down
66 changes: 66 additions & 0 deletions lib/widgets/settings_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sydney_webui/controller.dart';

class SettingsDialog extends StatelessWidget {
const SettingsDialog({Key? key}) : super(key: key);

@override
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;

return AlertDialog(
title: const Text('Settings'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
TextFormField(
initialValue: apiBaseUrl,
decoration: const InputDecoration(
labelText: 'API Base URL',
),
onChanged: (value) => apiBaseUrl = value,
),
const SizedBox(height: 8.0), // Adds spacing between fields
TextFormField(
initialValue: apiAccessToken,
decoration: const InputDecoration(
labelText: 'API Access Token',
),
onChanged: (value) => apiAccessToken = value,
),
const SizedBox(height: 8.0),
TextFormField(
initialValue: apiCookies,
decoration: const InputDecoration(
labelText: 'API Cookies',
),
onChanged: (value) => apiCookies = value,
),
],
),
),
actions: <Widget>[
TextButton(
child: const Text('Cancel'),
onPressed: () {
Get.back();
},
),
TextButton(
child: const Text('Save'),
onPressed: () {
controller.sydneyService.baseUrl = Uri.tryParse(apiBaseUrl);
controller.sydneyService.accessToken = apiAccessToken;
controller.sydneyService.cookies = apiCookies;
Get.back();
},
),
],
);
}
}

0 comments on commit 11f209a

Please sign in to comment.