Skip to content

Commit

Permalink
feat: edit cfg name (#89)
Browse files Browse the repository at this point in the history
Fixes #88
  • Loading branch information
lollipopkit committed Jul 16, 2024
1 parent c6a24d5 commit eb05c10
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 79 deletions.
6 changes: 6 additions & 0 deletions lib/data/model/chat/config.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:gpt_box/data/res/l10n.dart';
import 'package:gpt_box/data/store/all.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:shortid/shortid.dart';
Expand Down Expand Up @@ -66,6 +67,11 @@ final class ChatConfig {
transcribeModel: _kTranscribeModel,
);

String get displayName => switch (id) {
defaultId when name.isEmpty => l10n.defaulT,
_ => name,
};

void save() => Stores.config.put(this);

bool shouldUpdateRelavance(ChatConfig old) {
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"chatHistoryTip": "Use as chat context",
"choose": "Choose",
"clear": "Clear",
"clickSwitch": "Click to switch",
"clickToCheck": "Click to check",
"clipboard": "Clipboard",
"codeBlock": "Code block",
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/app_zh.arb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"chatHistoryTip": "用作聊天上下文",
"choose": "选择",
"clear": "清除",
"clickSwitch": "点击切换",
"clickToCheck": "点击检查",
"clipboard": "剪切板",
"codeBlock": "代码区块",
Expand Down
148 changes: 69 additions & 79 deletions lib/view/page/profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:gpt_box/data/model/chat/config.dart';
import 'package:gpt_box/data/res/l10n.dart';
import 'package:gpt_box/data/res/openai.dart';
import 'package:gpt_box/data/store/all.dart';
import 'package:shortid/shortid.dart';

final class ProfilePage extends StatefulWidget {
const ProfilePage({super.key, Never? args});
Expand Down Expand Up @@ -107,89 +106,80 @@ final class _ProfilePageState extends State<ProfilePage> {
}

Widget _buildSwitchCfg(ChatConfig cfg) {
final vals = Stores.config.box
.toJson<ChatConfig>(includeInternal: false)
.values
.toList();
final children = <Widget>[];
for (int idx = 0; idx < vals.length; idx++) {
final value = vals[idx];
final delBtn = IconButton(
icon: const Icon(Icons.delete),
onPressed: () {
if (value.id == ChatConfig.defaultId) return;
context.showRoundDialog(
title: l10n.attention,
child: Text(l10n.delFmt(value.name, l10n.profile)),
actions: Btns.oks(
onTap: () {
Stores.config.delete(value.id);
_cfgRN.notify();
context.pop();
if (cfg.id == value.id) {
OpenAICfg.switchToDefault(context);
_cfgRN.notify();
}
},
red: true,
),
);
},
);
children.add(ListTile(
leading: Checkbox(
value: cfg.id == value.id,
onChanged: (val) {
if (val != true) return;
OpenAICfg.setTo(value);
_cfgRN.notify();
},
),
title: Text(value.name.isEmpty ? l10n.defaulT : value.name),
onTap: () {
if (cfg.id == value.id) return;
OpenAICfg.setTo(value);
_cfgRN.notify();
},
trailing: value.id != ChatConfig.defaultId ? delBtn : null,
));
}
children.add(ListTile(
return ListTile(
leading: const Icon(Icons.switch_account),
title: Text(l10n.profile),
subtitle: Text(
'${cfg.displayName}, ${l10n.clickSwitch}',
style: UIs.textGrey,
),
onTap: () async {
final ctrl = TextEditingController();
final name = await context.showRoundDialog(
title: l10n.add,
child: Input(
controller: ctrl,
hint: l10n.name,
icon: Icons.text_fields,
),
actions: Btns.oks(
onTap: () => context.pop(ctrl.text),
),
final vals = Stores.config.box
.toJson<ChatConfig>(includeInternal: false)
.values
.toList();
final newCfg = await context.showPickSingleDialog(
items: vals,
initial: cfg,
title: l10n.profile,
name: (p0) => p0.displayName,
);
if (name == null) return;
final cfg = ChatConfig.defaultOne.copyWith(
id: shortid.generate(),
name: name,
)..save();
OpenAICfg.setTo(cfg);

if (newCfg == null) return;
OpenAICfg.setTo(newCfg);
_cfgRN.notify();
},
title: Text(l10n.add),
trailing: const Icon(Icons.add),
));
return ExpandTile(
leading: const Icon(Icons.switch_account),
title: Text(l10n.profile),
subtitle: Text(
'${l10n.current}: ${switch ((cfg.id, cfg.name)) {
(ChatConfig.defaultId, _) => l10n.defaulT,
(_, final String name) => name,
}}',
style: UIs.text13Grey,
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
// Delete
if (cfg.id != ChatConfig.defaultId)
IconButton(
icon: const Icon(Icons.delete),
onPressed: () {
if (cfg.id == ChatConfig.defaultId) return;
context.showRoundDialog(
title: l10n.attention,
child: Text(l10n.delFmt(cfg.name, l10n.profile)),
actions: Btns.oks(
onTap: () {
Stores.config.delete(cfg.id);
_cfgRN.notify();
context.pop();
if (cfg.id == cfg.id) {
OpenAICfg.switchToDefault(context);
_cfgRN.notify();
}
},
red: true,
),
);
},
),
// Rename
IconButton(
icon: const Icon(Icons.edit),
onPressed: () {
final ctrl = TextEditingController(text: cfg.name);
context.showRoundDialog(
title: l10n.edit,
child: Input(controller: ctrl, label: l10n.name),
actions: Btns.oks(
onTap: () {
final name = ctrl.text;
if (name.isEmpty) return;
final newCfg = cfg.copyWith(name: name);
newCfg.save();
OpenAICfg.setTo(newCfg);
_cfgRN.notify();
context.pop();
},
),
);
},
),
],
),
children: children,
);
}

Expand Down

0 comments on commit eb05c10

Please sign in to comment.