Skip to content

Commit

Permalink
Merge pull request #2 from bapaws/dev
Browse files Browse the repository at this point in the history
[fix] Api url can not saved
  • Loading branch information
bapaws committed Mar 16, 2023
2 parents c7e6245 + 45847c7 commit af8c340
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 45 deletions.
4 changes: 4 additions & 0 deletions lib/app/app_translation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class AppTranslation extends Translations {
'type_your_tokens': '请输入您的 @name',
'must_type_tokens': '请先点击我的头像,输入您的 @tokens',
'network_error': '网络出现了问题,请稍后重试~',
'save': '保存',
'reset': '重置',
};

static const Map<String, String> en_US = {
Expand All @@ -48,5 +50,7 @@ class AppTranslation extends Translations {
'must_type_tokens': 'Please click on my avatar,type your @tokens',
'network_error':
'Oops! The network is unavailable. \n\nPlease try again later.',
'save': 'Save',
'reset': 'Reset',
};
}
6 changes: 3 additions & 3 deletions lib/app/core/app/app_http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class AppHttp {
if (kDebugMode) HttpOverrides.global = httpOverrides;

final options = BaseOptions(
connectTimeout: const Duration(seconds: 30),
receiveTimeout: const Duration(seconds: 30),
sendTimeout: const Duration(seconds: 30),
connectTimeout: const Duration(seconds: 60),
receiveTimeout: const Duration(seconds: 60),
sendTimeout: const Duration(seconds: 60),
headers: {
'Content-Type': 'application/json',
},
Expand Down
1 change: 1 addition & 0 deletions lib/app/core/app/app_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class AppTheme {
? const Color(0xFF181818)
: const Color(0xFFCBCBCB),
),
toolbarTextStyle: textTheme.titleMedium,
);

BottomNavigationBarThemeData _getBottomNavigationBarTheme(
Expand Down
8 changes: 5 additions & 3 deletions lib/app/data/db/app_database.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:answer/app/data/db/service_providers_dao.dart';
import 'package:answer/app/data/db/service_tokens_dao.dart';
import 'package:flutter/foundation.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';

Expand Down Expand Up @@ -37,7 +37,7 @@ class AppDatabase {
path,
onCreate: instance._onCreate,
onUpgrade: instance._onUpgrade,
version: 1,
version: 2,
);
}

Expand All @@ -47,5 +47,7 @@ class AppDatabase {
ServiceTokensDao.onCreate(db);
}

FutureOr<void> _onUpgrade(Database db, int oldVersion, int newVersion) {}
FutureOr<void> _onUpgrade(Database db, int oldVersion, int newVersion) {
ServiceProvidersDao.onUpgrade(db, oldVersion, newVersion);
}
}
21 changes: 21 additions & 0 deletions lib/app/data/db/service_providers_dao.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,32 @@ class ServiceProvidersDao {
group_id INTEGER,
help TEXT,
help_url TEXT,
hello TEXT,
block INTEGER DEFAULT 0
);
''');
}

static Future<void> onUpgrade(
Database db,
int oldVersion,
int newVersion,
) async {
// new version is 2
final hello = await db.rawQuery(
'select * from sqlite_master where name="$table" and sql like "%hello%";',
);
if (hello.isEmpty) {
await db.execute('ALTER TABLE $table ADD COLUMN hello TEXT;');
}
final desc = await db.rawQuery(
'select * from sqlite_master where name="$table" and sql like "%desc%";',
);
if (desc.isEmpty) {
await db.execute('ALTER TABLE $table ADD COLUMN desc TEXT;');
}
}

Future<List<Map<String, Object?>>> getAll({required int groupId}) async {
return (await db.query(table, where: 'group_id = ?', whereArgs: [groupId]));
}
Expand Down
61 changes: 36 additions & 25 deletions lib/app/modules/service/controllers/service_controller.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:answer/app/data/db/app_database.dart';
import 'package:answer/app/data/models/service_token.dart';
import 'package:answer/app/providers/service_provider_manager.dart';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../core/app/app_controller_mixin.dart';
Expand All @@ -12,19 +12,22 @@ class ServiceController extends GetxController with AppControllerMixin {
late final String serviceId = Get.arguments;
ServiceProvider? provider;

final apiUrlFocusNode = FocusNode();
final apiUrlTextEditingController = TextEditingController();
final Map<String, TextEditingController> textEditingControllers = {};
final Map<String, FocusNode> focusNodes = {};

static const obscureText = '••••••';

bool editing = false;

@override
Future<void> onReady() async {
provider = await ServiceProviderManager.to.get(
id: serviceId,
);
if (provider?.apiUrl != null) {
apiUrlTextEditingController.text = provider!.apiUrl!;
if (provider?.url != null) {
apiUrlTextEditingController.text = provider!.url!;
}
if (provider?.tokens != null) {
for (final item in provider!.tokens) {
Expand Down Expand Up @@ -55,42 +58,50 @@ class ServiceController extends GetxController with AppControllerMixin {
textEditingControllers[token.id]?.text == obscureText;

void onObscured(ServiceToken token) {
if (isObscure(token)) {
if (isObscure(token) || editing) {
textEditingControllers[token.id]?.text = token.value;
} else {
textEditingControllers[token.id]?.text = '••••••';
}
update();
}

Future<void> onBlocked(bool value) async {
await ServiceProviderManager.to.block(provider, value);
void onEdited() {
editing = true;
for (final token in provider!.tokens) {
textEditingControllers[token.id]?.text = token.value;
}
update();
// delay is working
Future.delayed(const Duration(milliseconds: 100), () {
apiUrlFocusNode.requestFocus();
});
}

Future<void> onApiUrlSubmitted(ServiceToken token, String value) async {
final index = provider?.tokens.indexWhere(
(element) => element.id == token.id,
);
if (index == null || index == -1) return;
void onReset() {
if (provider == null) return;
}

final newToken = token.copyWith(value: value);
provider?.tokens[index] = newToken;
update();
Future<void> onSaved() async {
if (provider == null) return;

await AppDatabase.instance.serviceTokensDao.create(newToken);
}
for (int index = 0; index < provider!.tokens.length; index++) {
final token = provider!.tokens[index];
provider?.tokens[index] = token.copyWith(
value: textEditingControllers[token.id]?.text,
);
}
provider?.editApiUrl = apiUrlTextEditingController.text;

Future<void> onTokenSubmitted(ServiceToken token, String value) async {
final index = provider?.tokens.indexWhere(
(element) => element.id == token.id,
);
if (index == null || index == -1) return;
editing = false;

final newToken = token.copyWith(value: value);
provider?.tokens[index] = newToken;
update();

await AppDatabase.instance.serviceTokensDao.create(newToken);
await AppDatabase.instance.serviceProvidersDao.create(provider!);
}

Future<void> onBlocked(bool value) async {
await ServiceProviderManager.to.block(provider, value);
update();
}
}
4 changes: 3 additions & 1 deletion lib/app/modules/service/views/service_token_item_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ServiceTokenItemView extends StatelessWidget {
required this.obscured,
this.onSubmitted,
required this.focusNode,
this.textInputAction,
});

final ServiceToken item;
Expand All @@ -23,6 +24,7 @@ class ServiceTokenItemView extends StatelessWidget {
final VoidCallback onObscured;
final bool obscured;
final ValueChanged<String>? onSubmitted;
final TextInputAction? textInputAction;

@override
Widget build(BuildContext context) {
Expand All @@ -46,7 +48,7 @@ class ServiceTokenItemView extends StatelessWidget {
hintText: 'type_your_tokens'.trParams({'name': item.name}),
),
onSubmitted: onSubmitted,
textInputAction: TextInputAction.done,
textInputAction: textInputAction ?? TextInputAction.done,
),
),
trailing: IconButton(
Expand Down
39 changes: 30 additions & 9 deletions lib/app/modules/service/views/service_view.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:answer/app/views/app_cell.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:url_launcher/url_launcher_string.dart';

Expand All @@ -16,14 +16,32 @@ class ServiceView extends StatelessWidget with AppViewMixin<ServiceController> {

@override
PreferredSizeWidget? buildAppBar(BuildContext context) {
return AppBar();
return AppBar(
actions: controller.editing
? [
TextButton(
onPressed: controller.onSaved,
child: Text(
'save'.tr,
style: Theme.of(context).textTheme.titleMedium,
),
),
]
: [
IconButton(
onPressed: controller.onEdited,
icon: const Icon(Icons.edit),
),
],
);
}

@override
Widget buildBody(BuildContext context) {
if (controller.provider == null) return Container();

return ListView(
keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
children: [
ServiceInfo(
provider: controller.provider!,
Expand Down Expand Up @@ -73,14 +91,17 @@ class ServiceView extends StatelessWidget with AppViewMixin<ServiceController> {
child: TextField(
minLines: 1,
maxLines: 1,
enabled: true,
autocorrect: false,
enabled: controller.editing,
focusNode: controller.apiUrlFocusNode,
controller: controller.apiUrlTextEditingController,
style: Theme.of(context).textTheme.bodyMedium,
decoration: InputDecoration.collapsed(
hintText: 'type_your_tokens'.trParams({'name': 'API URL'}),
),
// onSubmitted: onSubmitted,
textInputAction: TextInputAction.done,
textInputAction: controller.provider!.tokens.isEmpty
? TextInputAction.done
: TextInputAction.next,
),
),
),
Expand All @@ -106,11 +127,11 @@ class ServiceView extends StatelessWidget with AppViewMixin<ServiceController> {
onObscured: () {
controller.onObscured(item);
},
enabled: !controller.isObscure(item),
enabled: controller.editing,
obscured: controller.isObscure(item),
onSubmitted: (value) {
controller.onTokenSubmitted(item, value);
},
textInputAction: item == controller.provider!.tokens.last
? TextInputAction.done
: TextInputAction.next,
),
const SizedBox(
height: 8,
Expand Down
2 changes: 1 addition & 1 deletion lib/app/providers/open_ai/chat_gpt.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ChatGpt extends ServiceProvider {
messages.add({'role': 'user', 'content': message.content!});

final response = await AppHttp.post(
apiUrl!,
url!,
data: {
'model': 'gpt-3.5-turbo-0301',
'messages': messages,
Expand Down
5 changes: 3 additions & 2 deletions lib/app/providers/service_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class ServiceProvider {

Message? currentRequestMessage;

String? get url => editApiUrl ?? apiUrl;

ServiceProvider({
required this.id,
required this.name,
Expand Down Expand Up @@ -91,7 +93,6 @@ class ServiceProvider {
ServiceProviderCallback? onReceived,
String? help,
String? helpUrl,
String? welcome,
String? hello,
bool? block,
String? editApiUrl,
Expand Down Expand Up @@ -148,7 +149,7 @@ class ServiceProvider {
"help": help,
"help_url": helpUrl,
"hello": hello,
"editApiUrl": editApiUrl,
"edit_api_url": editApiUrl,
"block": block ? 1 : 0,
"tokens": List<dynamic>.from(tokens.map((x) => x.toJson())),
};
Expand Down
2 changes: 2 additions & 0 deletions lib/flavors/env_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ class EnvConfig {
final String dbName;
final bool shouldCollectCrashLog;
final HttpOverrides? httpOverrides;
final String? openAIUrl;
final String? openAIApiKey;

EnvConfig({
required this.appName,
required this.dbName,
this.shouldCollectCrashLog = false,
this.httpOverrides,
this.openAIUrl,
this.openAIApiKey,
});
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: answer
version: 1.0.0+1
version: 1.0.1+7
publish_to: none
description: A new Flutter project.
environment:
Expand Down

0 comments on commit af8c340

Please sign in to comment.