Skip to content

Commit

Permalink
Merge pull request #43 from geeker-ai/grouped-bottom-switch
Browse files Browse the repository at this point in the history
feat: The model selection interface supports grouping and disabling f…
  • Loading branch information
zmhu authored Nov 30, 2023
2 parents 30b0b30 + 8673be4 commit b3f98a2
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/components/settings/geekerchat_active.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class GeekerChatSettingsComponent extends StatelessWidget {

submitActive(SettingsServerController controller, BuildContext context) {
if (controller.defaultServer.license.isNotEmpty) {
if (controller.needReactive) {
if (controller.needReactive || !controller.defaultServer.isActived) {
controller
.activeLicense(controller.defaultServer.license,
settingsController.settings.uuid, settingsController.lang)
Expand Down
123 changes: 123 additions & 0 deletions lib/components/settings/grouped_bottom_sheet_switcher.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import 'package:flutter/material.dart';
import 'package:geek_chat/controller/settings.dart';
import 'package:geek_chat/models/bottom_switcher_model.dart';
import 'package:get/get.dart';
import 'package:logger/logger.dart';

// ignore: must_be_immutable
class GroupedBottomSheetSwitcherComponent extends StatelessWidget {
GroupedBottomSheetSwitcherComponent(
{super.key,
required this.title,
required this.subTitle,
required this.selectedValue,
required this.options,
required this.leadingIcon,
required this.onTapCallback});

String title;
String subTitle;
String selectedValue;
Logger logger = Get.find();
// List<Map<String, String>> options;
List<GroupedBottomSwitcherOption> options;
Function onTapCallback;
IconData leadingIcon;

List<Widget> buildOptionsList(BuildContext context) {
List<Widget> widgets = [];
widgets.add(ListTile(
dense: true,
title: Text(
title.tr,
style: const TextStyle(fontWeight: FontWeight.bold),
),
tileColor: Theme.of(context).scaffoldBackgroundColor,
));
for (GroupedBottomSwitcherOption group in options) {
widgets.add(ListTile(
dense: true,
title: Text(group.groupName),
));
for (BottomSwitcherOption option in group.options) {
if (option.disabled) {
widgets.add(ListTile(
title: Row(
children: [
Radio(
value: option.id,
groupValue: selectedValue,
onChanged: null,
),
Text(
option.name.tr,
),
],
),
));
} else {
widgets.add(ListTile(
title: Row(
children: [
Radio(
value: option.id,
groupValue: selectedValue,
onChanged: (value) {
onTapCallback(value);
Navigator.pop(context);
},
),
Text(
option.name.tr,
),
],
),
onTap: () {
///
onTapCallback(option.id);
Navigator.pop(context);
},
));
}
}
}

widgets.add(Container(
padding: const EdgeInsets.only(top: 20),
));
return widgets;
}

@override
Widget build(BuildContext context) {
logger.d("options: $options");
return GetBuilder<SettingsController>(builder: ((controller) {
return ListTile(
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(title.tr),
Text(
subTitle.tr,
style: const TextStyle(fontSize: 12),
)
],
),
leading: Icon(leadingIcon),
trailing: const Icon(Icons.chevron_right_outlined),
onTap: () {
showModalBottomSheet(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0)),
context: context,
builder: (context) {
return SafeArea(
child: Wrap(
children: buildOptionsList(context),
));
});
},
);
}));
}
}
25 changes: 25 additions & 0 deletions lib/controller/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ class SettingsController extends GetxController {

DeviceType deviceType = DeviceType.wide;

final List<AiGroup> _aiGroups = [
AiGroup(
aitype: AiType.chatgpt,
groupName: "ChatGPT",
groupDesc: "OpenAI ChatGPT"),
AiGroup(
aitype: AiType.bard,
groupName: "Google Vertex AI",
groupDesc: "Google Vertex AI")
];

List<AiGroup> get aiGroups {
return _aiGroups;
}

List<AiModel> getModelsByType(AiType aiType) {
List<AiModel> models = [];
for (AiModel aiModel in _aiModels) {
if (aiModel.aiType == aiType) {
models.add(aiModel);
}
}
return models;
}

final List<AiModel> _aiModels = [
AiModel(
modelName: 'gpt-3.5-turbo',
Expand Down
20 changes: 20 additions & 0 deletions lib/models/bottom_switcher_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class BottomSwitcherOption {
BottomSwitcherOption({
required this.id,
required this.name,
required this.additionalText,
required this.disabled,
});
String id;
String name;
String additionalText;
bool disabled;
}

class GroupedBottomSwitcherOption {
GroupedBottomSwitcherOption(
{required this.groupName, required this.groupDesc});
String groupName;
String groupDesc;
List<BottomSwitcherOption> options = [];
}
11 changes: 11 additions & 0 deletions lib/models/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ enum AiType {

enum ModelType { chat, text, image }

class AiGroup {
AiGroup({
required this.aitype,
required this.groupName,
required this.groupDesc,
});
String groupName;
AiType aitype;
String groupDesc;
}

class AiModel {
String modelName;
String alias;
Expand Down
36 changes: 32 additions & 4 deletions lib/pages/chat_edit.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import 'package:flutter/material.dart';
import 'package:geek_chat/components/settings/bottom_sheet_switcher.dart';
// import 'package:geek_chat/components/settings/bottom_sheet_switcher.dart';
import 'package:geek_chat/components/settings/grouped_bottom_sheet_switcher.dart';
import 'package:geek_chat/controller/chat_list_controller.dart';
import 'package:geek_chat/controller/settings.dart';
import 'package:geek_chat/controller/settings_server_controller.dart';
import 'package:geek_chat/models/bottom_switcher_model.dart';
import 'package:geek_chat/models/model.dart';
import 'package:get/get.dart';
import 'package:logger/logger.dart';

// ignore: must_be_immutable
class ChatEditPage extends StatelessWidget {
ChatEditPage({super.key});

SettingsController settingsController = Get.find<SettingsController>();
SettingsServerController settingsServerController = Get.find();
Logger logger = Get.find();

List<Map<String, String>> getModelOptions() {
List<Map<String, String>> options = [];
Expand All @@ -24,6 +28,29 @@ class ChatEditPage extends StatelessWidget {
return options;
}

List<GroupedBottomSwitcherOption> getGroupedModelOptions() {
List<GroupedBottomSwitcherOption> options = [];
for (AiGroup group in settingsController.aiGroups) {
// options.add(value)
GroupedBottomSwitcherOption gOptions = GroupedBottomSwitcherOption(
groupName: group.groupName, groupDesc: group.groupDesc);
for (AiModel model in settingsController.getModelsByType(group.aitype)) {
bool disabled = false;
if (settingsServerController.defaultServer.provider != "geekerchat" &&
model.aiType == AiType.bard) {
disabled = true;
}
gOptions.options.add(BottomSwitcherOption(
id: model.modelName,
name: model.modelName,
additionalText: "",
disabled: disabled));
}
options.add(gOptions);
}
return options;
}

String getTitle(String? opt) {
String title = "New Chat";
if (opt == 'edit') {
Expand All @@ -37,7 +64,8 @@ class ChatEditPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
var data = Get.parameters;
var options = getModelOptions();
// var options = getModelOptions();

if (data['opt'] == 'new') {
chatListController.createNewSession();
} else {
Expand Down Expand Up @@ -159,11 +187,11 @@ class ChatEditPage extends StatelessWidget {
style: const TextStyle(fontWeight: FontWeight.bold),
),
),
BottomSheetSwitcherComponent(
GroupedBottomSheetSwitcherComponent(
title: 'Model',
subTitle: controller.currentSession.model,
selectedValue: controller.currentSession.model,
options: options,
options: getGroupedModelOptions(),
leadingIcon: Icons.smart_toy_outlined,
onTapCallback: (value) {
controller.currentSession.model = value;
Expand Down
2 changes: 1 addition & 1 deletion macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 236401fc2c932af29a9fcf0e97baeeb2d750d367

COCOAPODS: 1.14.1
COCOAPODS: 1.13.0

0 comments on commit b3f98a2

Please sign in to comment.