Преглед на файлове

feat: #2003 Entering an OpenAI key should auto-save

Lucas.Xu преди 2 години
родител
ревизия
1a3701feaf

+ 24 - 0
frontend/appflowy_flutter/lib/util/debounce.dart

@@ -0,0 +1,24 @@
+import 'dart:async';
+
+import 'package:flutter/material.dart';
+
+class Debounce {
+  final Duration duration;
+  Timer? _timer;
+
+  Debounce({
+    this.duration = const Duration(milliseconds: 1000),
+  });
+
+  void call(VoidCallback action) {
+    dispose();
+    _timer = Timer(duration, () {
+      action();
+    });
+  }
+
+  void dispose() {
+    _timer?.cancel();
+    _timer = null;
+  }
+}

+ 25 - 5
frontend/appflowy_flutter/lib/workspace/presentation/settings/widgets/settings_user_view.dart

@@ -1,4 +1,6 @@
 import 'package:appflowy/startup/startup.dart';
+import 'package:appflowy/util/debounce.dart';
+import 'package:appflowy_backend/log.dart';
 import 'package:flowy_infra/size.dart';
 import 'package:flowy_infra_ui/style_widget/text.dart';
 import 'package:flutter/material.dart';
@@ -98,11 +100,20 @@ class _OpenaiKeyInput extends StatefulWidget {
 
 class _OpenaiKeyInputState extends State<_OpenaiKeyInput> {
   bool visible = false;
+  final textEditingController = TextEditingController();
+  final debounce = Debounce();
+
+  @override
+  void initState() {
+    super.initState();
+
+    textEditingController.text = widget.openAIKey;
+  }
 
   @override
   Widget build(BuildContext context) {
     return TextField(
-      controller: TextEditingController()..text = widget.openAIKey,
+      controller: textEditingController,
       obscureText: !visible,
       decoration: InputDecoration(
         labelText: 'OpenAI Key',
@@ -120,13 +131,22 @@ class _OpenaiKeyInputState extends State<_OpenaiKeyInput> {
           },
         ),
       ),
-      onSubmitted: (val) {
-        context
-            .read<SettingsUserViewBloc>()
-            .add(SettingsUserEvent.updateUserOpenAIKey(val));
+      onChanged: (value) {
+        debounce.call(() {
+          Log.debug('SettingsUserViewBloc');
+          context
+              .read<SettingsUserViewBloc>()
+              .add(SettingsUserEvent.updateUserOpenAIKey(value));
+        });
       },
     );
   }
+
+  @override
+  void dispose() {
+    debounce.dispose();
+    super.dispose();
+  }
 }
 
 class _CurrentIcon extends StatelessWidget {