From e189e55f00d42b6873117b071301f2fd1dc31756 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?lollipopkit=F0=9F=8F=B3=EF=B8=8F=E2=80=8D=E2=9A=A7?=
 =?UTF-8?q?=EF=B8=8F?= <10864310+lollipopkit@users.noreply.github.com>
Date: Sat, 15 Jun 2024 14:03:07 +0800
Subject: [PATCH] opt.: switch to previous chat after deleting a chat (#26)
 (#27)

---
 ios/Runner.xcodeproj/project.pbxproj |  6 +++---
 lib/data/res/build.dart              |  5 +----
 lib/view/page/home/ctrl.dart         | 30 ++++++++++++++++++++--------
 pubspec.lock                         |  4 ++--
 pubspec.yaml                         |  4 ++--
 5 files changed, 30 insertions(+), 19 deletions(-)

diff --git a/ios/Runner.xcodeproj/project.pbxproj b/ios/Runner.xcodeproj/project.pbxproj
index 5e8c1be..0edf926 100644
--- a/ios/Runner.xcodeproj/project.pbxproj
+++ b/ios/Runner.xcodeproj/project.pbxproj
@@ -363,7 +363,7 @@
 				ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
 				CLANG_ENABLE_MODULES = YES;
 				CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements;
-				CURRENT_PROJECT_VERSION = 208;
+				CURRENT_PROJECT_VERSION = 209;
 				DEVELOPMENT_TEAM = BA88US33G6;
 				ENABLE_BITCODE = NO;
 				INFOPLIST_FILE = Runner/Info.plist;
@@ -494,7 +494,7 @@
 				ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
 				CLANG_ENABLE_MODULES = YES;
 				CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements;
-				CURRENT_PROJECT_VERSION = 208;
+				CURRENT_PROJECT_VERSION = 209;
 				DEVELOPMENT_TEAM = BA88US33G6;
 				ENABLE_BITCODE = NO;
 				INFOPLIST_FILE = Runner/Info.plist;
@@ -519,7 +519,7 @@
 				ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
 				CLANG_ENABLE_MODULES = YES;
 				CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements;
-				CURRENT_PROJECT_VERSION = 208;
+				CURRENT_PROJECT_VERSION = 209;
 				DEVELOPMENT_TEAM = BA88US33G6;
 				ENABLE_BITCODE = NO;
 				INFOPLIST_FILE = Runner/Info.plist;
diff --git a/lib/data/res/build.dart b/lib/data/res/build.dart
index 9457b2d..65ae6af 100644
--- a/lib/data/res/build.dart
+++ b/lib/data/res/build.dart
@@ -2,8 +2,5 @@
 
 class Build {
   static const String name = "GPTBox";
-  static const int build = 208;
-  static const String engine = "3.22.2";
-  static const String buildAt = "2024-06-12 21:35:08";
-  static const int modifications = 6;
+  static const int build = 209;
 }
diff --git a/lib/view/page/home/ctrl.dart b/lib/view/page/home/ctrl.dart
index 6a549d0..9f87b22 100644
--- a/lib/view/page/home/ctrl.dart
+++ b/lib/view/page/home/ctrl.dart
@@ -47,15 +47,29 @@ void _switchChat([String? id]) {
 }
 
 void _switchPreviousChat() {
-  final idx = _allHistories.keys.toList().indexOf(_curChatId);
-  if (idx < 0 || idx >= _allHistories.length - 1) return;
-  _switchChat(_allHistories.keys.elementAt(idx + 1));
+  final iter = _allHistories.keys.iterator;
+  bool next = false;
+  while (iter.moveNext()) {
+    if (next) {
+      _switchChat(iter.current);
+      return;
+    }
+    if (iter.current == _curChatId) next = true;
+  }
 }
 
 void _switchNextChat() {
-  final idx = _allHistories.keys.toList().indexOf(_curChatId);
-  if (idx <= 0) return;
-  _switchChat(_allHistories.keys.elementAt(idx - 1));
+  final iter = _allHistories.keys.iterator;
+  String? last;
+  while (iter.moveNext()) {
+    if (iter.current == _curChatId) {
+      if (last != null) {
+        _switchChat(last);
+        return;
+      }
+    }
+    last = iter.current;
+  }
 }
 
 void _storeChat(
@@ -145,10 +159,10 @@ void _onTapDeleteChat(String chatId, BuildContext context) {
 
 void _onDeleteChat(String chatId) {
   Stores.history.delete(chatId);
-  _allHistories.remove(chatId);
   if (_curChatId == chatId) {
-    _switchChat();
+    _switchPreviousChat();
   }
+  _allHistories.remove(chatId);
   _historyRN.build();
 }
 
diff --git a/pubspec.lock b/pubspec.lock
index e890cac..3493469 100644
--- a/pubspec.lock
+++ b/pubspec.lock
@@ -406,8 +406,8 @@ packages:
     dependency: "direct dev"
     description:
       path: "."
-      ref: "v1.0.17"
-      resolved-ref: bcb7019cc8bb3ab8c9c36653cd13936909c6e075
+      ref: "v1.0.24"
+      resolved-ref: b7e3698aa0ad2fcf0629d140b134276b9e42dac0
       url: "https://github.com/lppcg/fl_build.git"
     source: git
     version: "1.0.0"
diff --git a/pubspec.yaml b/pubspec.yaml
index aaa353d..b8279e7 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,7 +1,7 @@
 name: gpt_box
 description: "A third-party GPT Client for OpenAI API."
 publish_to: 'none'
-version: 1.0.209+209
+version: 1.0.210+210
 
 environment:
   sdk: '>=3.3.0 <4.0.0'
@@ -45,7 +45,7 @@ dev_dependencies:
   fl_build:
     git:
       url: https://github.com/lppcg/fl_build.git
-      ref: v1.0.17
+      ref: v1.0.24
     # path: ../fl_build
 
 flutter: