Skip to content

Commit 1a3701f

Browse files
committed
feat: #2003 Entering an OpenAI key should auto-save
1 parent 1b86587 commit 1a3701f

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import 'dart:async';
2+
3+
import 'package:flutter/material.dart';
4+
5+
class Debounce {
6+
final Duration duration;
7+
Timer? _timer;
8+
9+
Debounce({
10+
this.duration = const Duration(milliseconds: 1000),
11+
});
12+
13+
void call(VoidCallback action) {
14+
dispose();
15+
_timer = Timer(duration, () {
16+
action();
17+
});
18+
}
19+
20+
void dispose() {
21+
_timer?.cancel();
22+
_timer = null;
23+
}
24+
}

frontend/appflowy_flutter/lib/workspace/presentation/settings/widgets/settings_user_view.dart

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import 'package:appflowy/startup/startup.dart';
2+
import 'package:appflowy/util/debounce.dart';
3+
import 'package:appflowy_backend/log.dart';
24
import 'package:flowy_infra/size.dart';
35
import 'package:flowy_infra_ui/style_widget/text.dart';
46
import 'package:flutter/material.dart';
@@ -98,11 +100,20 @@ class _OpenaiKeyInput extends StatefulWidget {
98100

99101
class _OpenaiKeyInputState extends State<_OpenaiKeyInput> {
100102
bool visible = false;
103+
final textEditingController = TextEditingController();
104+
final debounce = Debounce();
105+
106+
@override
107+
void initState() {
108+
super.initState();
109+
110+
textEditingController.text = widget.openAIKey;
111+
}
101112

102113
@override
103114
Widget build(BuildContext context) {
104115
return TextField(
105-
controller: TextEditingController()..text = widget.openAIKey,
116+
controller: textEditingController,
106117
obscureText: !visible,
107118
decoration: InputDecoration(
108119
labelText: 'OpenAI Key',
@@ -120,13 +131,22 @@ class _OpenaiKeyInputState extends State<_OpenaiKeyInput> {
120131
},
121132
),
122133
),
123-
onSubmitted: (val) {
124-
context
125-
.read<SettingsUserViewBloc>()
126-
.add(SettingsUserEvent.updateUserOpenAIKey(val));
134+
onChanged: (value) {
135+
debounce.call(() {
136+
Log.debug('SettingsUserViewBloc');
137+
context
138+
.read<SettingsUserViewBloc>()
139+
.add(SettingsUserEvent.updateUserOpenAIKey(value));
140+
});
127141
},
128142
);
129143
}
144+
145+
@override
146+
void dispose() {
147+
debounce.dispose();
148+
super.dispose();
149+
}
130150
}
131151

132152
class _CurrentIcon extends StatelessWidget {

0 commit comments

Comments
 (0)