-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
452 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:chatgpt_desktop/entity/Model.dart'; | ||
import 'package:chatgpt_desktop/utils/Util.dart'; | ||
import 'package:get/get.dart'; | ||
|
||
class ModelController extends GetxController{ | ||
|
||
RxList<Model> items = RxList<Model>([]); | ||
Rx<Model> selectModel = Model(displayName: 'GPT-3.5', name: 'gpt-3.5-turbo').obs; | ||
|
||
void addModel(displayName, name) => { | ||
items.add(Model(displayName: displayName, name: name)), | ||
Util.writeFile('models.json', jsonEncode(items)), | ||
update() | ||
}; | ||
|
||
void removeModel(Model model) => { | ||
items.remove(model), | ||
Util.writeFile('models.json', jsonEncode(items)), | ||
update() | ||
}; | ||
|
||
@override | ||
void onReady() { | ||
// 从本地存储中获取 | ||
try { | ||
Util.readFile('models.json').then((value) { | ||
if(value.isEmpty){ | ||
value = jsonEncode([ | ||
Model(displayName: 'GPT-3.5', name: 'gpt-3.5-turbo'), | ||
Model(displayName: 'GPT-4', name: 'gpt-4-turbo'), | ||
]); | ||
Util.writeFile('models.json', value); | ||
} | ||
jsonDecode(value).map<Model>((item) => Model.fromJson(item)).toList().forEach((element) { | ||
items.add(element); | ||
}); | ||
selectModel.value = items[0]; | ||
update(); // 通知 Flutter 刷新 UI | ||
}); | ||
} catch (e) { | ||
items.add(Model(displayName: 'GPT-3.5', name: 'gpt-3.5-turbo'),); | ||
items.add(Model(displayName: 'GPT-4', name: 'gpt-4-turbo'),); | ||
selectModel.value = items[0]; | ||
Util.writeFile('models.json', jsonEncode(items)); | ||
} | ||
super.onReady(); | ||
} | ||
|
||
|
||
void updateChatName(int index, String displayName, String name){ | ||
items[index].displayName = displayName; | ||
items[index].name = name; | ||
items.refresh(); | ||
Util.writeFile('models.json', jsonEncode(items)); | ||
update(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,4 +162,4 @@ class ChatMessageWidget extends StatelessWidget { | |
// ), | ||
// ); | ||
// } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Model { | ||
|
||
String displayName; | ||
String name; | ||
|
||
Model({ | ||
required this.name, | ||
required this.displayName, | ||
}); | ||
|
||
// toJson | ||
Map<String, dynamic> toJson() => { | ||
'name': name, | ||
'displayName': displayName, | ||
}; | ||
|
||
// fromJson | ||
Model.fromJson(Map<String, dynamic> json) | ||
: name = json['name'], | ||
displayName = json['displayName']; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.