Skip to content

Commit

Permalink
AI RECO: added the setting panel
Browse files Browse the repository at this point in the history
  • Loading branch information
YuJuncen committed Jul 27, 2023
1 parent cf28b80 commit 28e36b0
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 16 deletions.
39 changes: 33 additions & 6 deletions lib/components/setting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import 'dart:developer';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:moyubie/components/tags_info.dart';
import 'package:moyubie/controller/chat_room.dart';
import 'package:moyubie/controller/settings.dart';
import 'package:get/get.dart';
import 'package:moyubie/repository/chat_room.dart';
import 'package:moyubie/repository/tags.dart';
import 'package:moyubie/utils/tag_collector.dart';
import 'package:uuid/uuid.dart';

class SettingPage extends StatefulWidget {
Expand Down Expand Up @@ -366,6 +368,26 @@ class _SettingPageState extends State<SettingPage> {
child: const Text("Clear remote messages")),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
const Text("AI Recommendation"),
Tooltip(
message: "Control how LLM try to guess things you love.",
child: IconButton(
iconSize: 10.0,
splashRadius: 10,
color: Theme.of(context).colorScheme.primary,
onPressed: () {},
icon: const Icon(Icons.question_mark),
),
),
],
),
divider,
sizedBoxSpace,
TagsInfo(Get.find<TagCollector>()),

// DEBUGGER
if (kDebugMode) ...[
Row(
Expand Down Expand Up @@ -394,18 +416,23 @@ class _SettingPageState extends State<SettingPage> {
onPressed: () {
final repo = Get.find<TagsRepository>();
repo
.addNewTags(List.generate(10, (index) => Uuid().v4()))
.then((value) => log("DONE?", name: "moyubie::tags"))
.catchError((err) => log("ERROR! [$err]", name: "moyubie::tags"));
.addNewTags(
List.generate(10, (index) => Uuid().v4()))
.then((value) =>
log("DONE?", name: "moyubie::tags"))
.catchError((err) =>
log("ERROR! [$err]", name: "moyubie::tags"));
},
child: const Text("Add some random tags for you!")),
ElevatedButton(
onPressed: () {
final repo = Get.find<TagsRepository>();
repo
.fetchMostPopularTags(5)
.then((value) => log("DONE? [$value]", name: "moyubie::tags"))
.catchError((err) => log("ERROR! [$err]", name: "moyubie::tags"));
.fetchMostPopularTags(10)
.then((value) =>
log("DONE? [$value]", name: "moyubie::tags"))
.catchError((err) =>
log("ERROR! [$err]", name: "moyubie::tags"));
},
child: const Text("Fetch tags of you!")),
],
Expand Down
65 changes: 65 additions & 0 deletions lib/components/tags_info.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import 'dart:developer';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:moyubie/utils/tag_collector.dart';

class TagsInfo extends StatelessWidget {
final TagCollector _coll;
final Rx<List<String>?> _$tags = Rx(null);

TagsInfo(this._coll, {super.key});

@override
Widget build(BuildContext context) {
return Column(
children: [
Obx(() => SwitchListTile(
title: _coll.enabled.value
? const Text("Tag collector is enabled.")
: const Text("Tag collector is disabled."),
subtitle: _coll.enabled.value
? const Text(
"Once you are asking AI, we will try to get things you love, for recommending more interesting news for you.")
: const Text("We won't try to collect your interest point."),
value: _coll.enabled.value,
onChanged: (open) {
_coll.enabled.value = open;
},
)),
const SizedBox(height: 4,),
ExpansionTile(
title: const Text("Your Interests"),
onExpansionChanged: (exp) {
if (exp && _$tags.value == null) {
_coll.repo.fetchMostPopularTags(10, waitSync: true).then((value) {
_$tags.value = value;
});
}
},
children: [
Obx(() => _$tags.value == null
? Center(
child: Container(
margin: const EdgeInsets.symmetric(vertical: 8),
child: const CircularProgressIndicator()))
: Wrap(
alignment: WrapAlignment.start,
children: _$tags.value!
.map((element) => Container(
margin: const EdgeInsets.only(left: 8),
child: ActionChip(
avatar: const Icon(Icons.tag),
label: Text(element),
onPressed: null,
),
))
.toList(growable: false),
))
])
],
);
}
}
9 changes: 7 additions & 2 deletions lib/repository/tags.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class TagsRepository {
});
}

Future<List<String>> fetchMostPopularTags(int limit) async {
Future<List<String>> fetchMostPopularTags(int limit, {bool waitSync = false}) async {
// Firt return what we have in local
final db = await ChatRoomRepository().getLocalDb();
final List<Map<String, dynamic>> maps = await db.rawQuery('''
Expand All @@ -84,7 +84,7 @@ class TagsRepository {

// Then start async task to synchronize remote to local
final remoteDB = ChatRoomRepository().getMyRemoteDb();
remoteDB.then((remoteDB_) async {
final fut = remoteDB.then((remoteDB_) async {
if (remoteDB_ == null) {
return;
}
Expand All @@ -111,6 +111,11 @@ class TagsRepository {
await _addNewTags(remoteTags);
});

if (waitSync) {
await fut;
return fetchMostPopularTags(limit);
}

return localTags;
}
}
14 changes: 6 additions & 8 deletions lib/utils/tag_collector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,32 @@ import 'package:moyubie/controller/settings.dart';
import 'package:moyubie/repository/tags.dart';
import 'package:moyubie/utils/ai_recommend.dart';

enum TagCollState {
Running,
Stopped
}

class TagCollector extends DisposableInterface {
final TagsRepository repo;
TagCollState state;
SettingsController sctl;

Future<void>? _bgTask;
List<String> _batching = [];

RxList<Object> bgErrs = RxList([]);
RxInt droppedMsgs = 0.obs;
RxBool enabled = true.obs;

AIContext get _ai_ctx => AIContext(api_key: sctl.openAiKey.value, model: sctl.gptModel.value);

TagCollector({required this.state, required this.repo, required this.sctl});
TagCollector({required this.repo, required this.sctl});

factory TagCollector.create({ TagsRepository? repo, SettingsController? sctl }) {
final theRepo = repo ?? Get.find<TagsRepository>();
final theSctl = sctl ?? Get.find<SettingsController>();
var coll = TagCollector(state: TagCollState.Running, repo: theRepo, sctl: theSctl);
var coll = TagCollector(repo: theRepo, sctl: theSctl);
return coll;
}

void accept(String s) {
if (enabled.isFalse) {
return;
}
log("Getting message $s from user.", name: "TagCollector");
_batching.add(s);
if (_bgTask != null) {
Expand Down

0 comments on commit 28e36b0

Please sign in to comment.