refactored chat archive and added search
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import 'package:bang_navigator/features/chat_archive/data/database/database.dart';
|
||||
import 'package:drift/drift.dart';
|
||||
|
||||
part 'search.g.dart';
|
||||
|
||||
@DriftAccessor()
|
||||
class SearchDao extends DatabaseAccessor<ChatSearchDatabase>
|
||||
with _$SearchDaoMixin {
|
||||
SearchDao(super.db);
|
||||
|
||||
Future<void> indexChats(Iterable<ChatCompanion> chats) {
|
||||
return db.chat.insertAll(chats);
|
||||
}
|
||||
|
||||
Future<int> deleteAllChats() {
|
||||
return db.chat.deleteAll();
|
||||
}
|
||||
|
||||
Future<int> deleteChat(String fileName) {
|
||||
return (db.chat.delete()..where((t) => t.fileName.equals(fileName))).go();
|
||||
}
|
||||
|
||||
Future<void> upsertChat(ChatCompanion chat) {
|
||||
return db.chat.insertOne(
|
||||
chat,
|
||||
onConflict: DoUpdate(
|
||||
(old) => ChatCompanion.custom(
|
||||
content: Variable(chat.content.value),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Selectable<ChatQueryResult> queryChats({
|
||||
required String matchPrefix,
|
||||
required String matchSuffix,
|
||||
required String ellipsis,
|
||||
required int snippetLength,
|
||||
required String searchString,
|
||||
}) {
|
||||
return db.chatQuery(
|
||||
query: db.buildQuery(searchString),
|
||||
snippetLength: snippetLength,
|
||||
beforeMatch: matchPrefix,
|
||||
afterMatch: matchSuffix,
|
||||
ellipsis: ellipsis,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'search.dart';
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
mixin _$SearchDaoMixin on DatabaseAccessor<ChatSearchDatabase> {}
|
||||
@@ -0,0 +1,22 @@
|
||||
import 'package:bang_navigator/features/chat_archive/data/database/daos/search.dart';
|
||||
import 'package:bang_navigator/features/query/domain/tokenizer.dart';
|
||||
import 'package:drift/drift.dart';
|
||||
|
||||
part 'database.g.dart';
|
||||
|
||||
@DriftDatabase(
|
||||
include: {'database.drift'},
|
||||
daos: [SearchDao],
|
||||
)
|
||||
class ChatSearchDatabase extends _$ChatSearchDatabase
|
||||
with TrigramQueryBuilderMixin {
|
||||
@override
|
||||
final int schemaVersion = 1;
|
||||
|
||||
@override
|
||||
final int ftsTokenLimit = 6;
|
||||
@override
|
||||
final int ftsMinTokenLength = 3;
|
||||
|
||||
ChatSearchDatabase(super.e);
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
CREATE TABLE chat (
|
||||
file_name TEXT PRIMARY KEY NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
content TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE VIRTUAL TABLE chat_fts
|
||||
USING fts5(
|
||||
title,
|
||||
content,
|
||||
content=chat,
|
||||
tokenize="trigram"
|
||||
);
|
||||
|
||||
-- Triggers to keep the FTS index up to date.
|
||||
CREATE TRIGGER chat_after_insert AFTER INSERT ON chat BEGIN
|
||||
INSERT INTO
|
||||
chat_fts(rowid, title, content)
|
||||
VALUES (new.rowid, new.title, new.content);
|
||||
END;
|
||||
CREATE TRIGGER chat_after_delete AFTER DELETE ON chat BEGIN
|
||||
INSERT INTO
|
||||
chat_fts(chat_fts, rowid, title, content)
|
||||
VALUES('delete', old.rowid, old.title, old.content);
|
||||
END;
|
||||
CREATE TRIGGER chat_after_update AFTER UPDATE ON chat BEGIN
|
||||
INSERT INTO
|
||||
chat_fts(chat_fts, rowid, title, content)
|
||||
VALUES('delete', old.rowid, old.title, old.content);
|
||||
INSERT INTO
|
||||
chat_fts(chat_fts, title, content)
|
||||
VALUES (new.rowid, new.title, new.content);
|
||||
END;
|
||||
|
||||
chatQuery:
|
||||
SELECT
|
||||
c.file_name,
|
||||
highlight(chat_fts, 0, :beforeMatch, :afterMatch) AS title,
|
||||
snippet(chat_fts, 1, :beforeMatch, :afterMatch, :ellipsis, :snippetLength) AS content_snippet
|
||||
FROM chat_fts(:query) fts
|
||||
INNER JOIN
|
||||
chat c ON c.rowid = fts.rowid
|
||||
ORDER BY RANK;
|
||||
@@ -0,0 +1,641 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'database.dart';
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
class Chat extends Table with TableInfo<Chat, ChatData> {
|
||||
@override
|
||||
final GeneratedDatabase attachedDatabase;
|
||||
final String? _alias;
|
||||
Chat(this.attachedDatabase, [this._alias]);
|
||||
late final GeneratedColumn<String> fileName = GeneratedColumn<String>(
|
||||
'file_name', aliasedName, false,
|
||||
type: DriftSqlType.string,
|
||||
requiredDuringInsert: true,
|
||||
$customConstraints: 'PRIMARY KEY NOT NULL');
|
||||
late final GeneratedColumn<String> title = GeneratedColumn<String>(
|
||||
'title', aliasedName, false,
|
||||
type: DriftSqlType.string,
|
||||
requiredDuringInsert: true,
|
||||
$customConstraints: 'NOT NULL');
|
||||
late final GeneratedColumn<String> content = GeneratedColumn<String>(
|
||||
'content', aliasedName, false,
|
||||
type: DriftSqlType.string,
|
||||
requiredDuringInsert: true,
|
||||
$customConstraints: 'NOT NULL');
|
||||
@override
|
||||
List<GeneratedColumn> get $columns => [fileName, title, content];
|
||||
@override
|
||||
String get aliasedName => _alias ?? actualTableName;
|
||||
@override
|
||||
String get actualTableName => $name;
|
||||
static const String $name = 'chat';
|
||||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => {fileName};
|
||||
@override
|
||||
ChatData map(Map<String, dynamic> data, {String? tablePrefix}) {
|
||||
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : '';
|
||||
return ChatData(
|
||||
fileName: attachedDatabase.typeMapping
|
||||
.read(DriftSqlType.string, data['${effectivePrefix}file_name'])!,
|
||||
title: attachedDatabase.typeMapping
|
||||
.read(DriftSqlType.string, data['${effectivePrefix}title'])!,
|
||||
content: attachedDatabase.typeMapping
|
||||
.read(DriftSqlType.string, data['${effectivePrefix}content'])!,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Chat createAlias(String alias) {
|
||||
return Chat(attachedDatabase, alias);
|
||||
}
|
||||
|
||||
@override
|
||||
bool get dontWriteConstraints => true;
|
||||
}
|
||||
|
||||
class ChatData extends DataClass implements Insertable<ChatData> {
|
||||
final String fileName;
|
||||
final String title;
|
||||
final String content;
|
||||
const ChatData(
|
||||
{required this.fileName, required this.title, required this.content});
|
||||
@override
|
||||
Map<String, Expression> toColumns(bool nullToAbsent) {
|
||||
final map = <String, Expression>{};
|
||||
map['file_name'] = Variable<String>(fileName);
|
||||
map['title'] = Variable<String>(title);
|
||||
map['content'] = Variable<String>(content);
|
||||
return map;
|
||||
}
|
||||
|
||||
factory ChatData.fromJson(Map<String, dynamic> json,
|
||||
{ValueSerializer? serializer}) {
|
||||
serializer ??= driftRuntimeOptions.defaultSerializer;
|
||||
return ChatData(
|
||||
fileName: serializer.fromJson<String>(json['file_name']),
|
||||
title: serializer.fromJson<String>(json['title']),
|
||||
content: serializer.fromJson<String>(json['content']),
|
||||
);
|
||||
}
|
||||
@override
|
||||
Map<String, dynamic> toJson({ValueSerializer? serializer}) {
|
||||
serializer ??= driftRuntimeOptions.defaultSerializer;
|
||||
return <String, dynamic>{
|
||||
'file_name': serializer.toJson<String>(fileName),
|
||||
'title': serializer.toJson<String>(title),
|
||||
'content': serializer.toJson<String>(content),
|
||||
};
|
||||
}
|
||||
|
||||
ChatData copyWith({String? fileName, String? title, String? content}) =>
|
||||
ChatData(
|
||||
fileName: fileName ?? this.fileName,
|
||||
title: title ?? this.title,
|
||||
content: content ?? this.content,
|
||||
);
|
||||
@override
|
||||
String toString() {
|
||||
return (StringBuffer('ChatData(')
|
||||
..write('fileName: $fileName, ')
|
||||
..write('title: $title, ')
|
||||
..write('content: $content')
|
||||
..write(')'))
|
||||
.toString();
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(fileName, title, content);
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
(other is ChatData &&
|
||||
other.fileName == this.fileName &&
|
||||
other.title == this.title &&
|
||||
other.content == this.content);
|
||||
}
|
||||
|
||||
class ChatCompanion extends UpdateCompanion<ChatData> {
|
||||
final Value<String> fileName;
|
||||
final Value<String> title;
|
||||
final Value<String> content;
|
||||
final Value<int> rowid;
|
||||
const ChatCompanion({
|
||||
this.fileName = const Value.absent(),
|
||||
this.title = const Value.absent(),
|
||||
this.content = const Value.absent(),
|
||||
this.rowid = const Value.absent(),
|
||||
});
|
||||
ChatCompanion.insert({
|
||||
required String fileName,
|
||||
required String title,
|
||||
required String content,
|
||||
this.rowid = const Value.absent(),
|
||||
}) : fileName = Value(fileName),
|
||||
title = Value(title),
|
||||
content = Value(content);
|
||||
static Insertable<ChatData> custom({
|
||||
Expression<String>? fileName,
|
||||
Expression<String>? title,
|
||||
Expression<String>? content,
|
||||
Expression<int>? rowid,
|
||||
}) {
|
||||
return RawValuesInsertable({
|
||||
if (fileName != null) 'file_name': fileName,
|
||||
if (title != null) 'title': title,
|
||||
if (content != null) 'content': content,
|
||||
if (rowid != null) 'rowid': rowid,
|
||||
});
|
||||
}
|
||||
|
||||
ChatCompanion copyWith(
|
||||
{Value<String>? fileName,
|
||||
Value<String>? title,
|
||||
Value<String>? content,
|
||||
Value<int>? rowid}) {
|
||||
return ChatCompanion(
|
||||
fileName: fileName ?? this.fileName,
|
||||
title: title ?? this.title,
|
||||
content: content ?? this.content,
|
||||
rowid: rowid ?? this.rowid,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Map<String, Expression> toColumns(bool nullToAbsent) {
|
||||
final map = <String, Expression>{};
|
||||
if (fileName.present) {
|
||||
map['file_name'] = Variable<String>(fileName.value);
|
||||
}
|
||||
if (title.present) {
|
||||
map['title'] = Variable<String>(title.value);
|
||||
}
|
||||
if (content.present) {
|
||||
map['content'] = Variable<String>(content.value);
|
||||
}
|
||||
if (rowid.present) {
|
||||
map['rowid'] = Variable<int>(rowid.value);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return (StringBuffer('ChatCompanion(')
|
||||
..write('fileName: $fileName, ')
|
||||
..write('title: $title, ')
|
||||
..write('content: $content, ')
|
||||
..write('rowid: $rowid')
|
||||
..write(')'))
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
|
||||
class ChatFts extends Table
|
||||
with TableInfo<ChatFts, ChatFt>, VirtualTableInfo<ChatFts, ChatFt> {
|
||||
@override
|
||||
final GeneratedDatabase attachedDatabase;
|
||||
final String? _alias;
|
||||
ChatFts(this.attachedDatabase, [this._alias]);
|
||||
late final GeneratedColumn<String> title = GeneratedColumn<String>(
|
||||
'title', aliasedName, false,
|
||||
type: DriftSqlType.string,
|
||||
requiredDuringInsert: true,
|
||||
$customConstraints: '');
|
||||
late final GeneratedColumn<String> content = GeneratedColumn<String>(
|
||||
'content', aliasedName, false,
|
||||
type: DriftSqlType.string,
|
||||
requiredDuringInsert: true,
|
||||
$customConstraints: '');
|
||||
@override
|
||||
List<GeneratedColumn> get $columns => [title, content];
|
||||
@override
|
||||
String get aliasedName => _alias ?? actualTableName;
|
||||
@override
|
||||
String get actualTableName => $name;
|
||||
static const String $name = 'chat_fts';
|
||||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => const {};
|
||||
@override
|
||||
ChatFt map(Map<String, dynamic> data, {String? tablePrefix}) {
|
||||
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : '';
|
||||
return ChatFt(
|
||||
title: attachedDatabase.typeMapping
|
||||
.read(DriftSqlType.string, data['${effectivePrefix}title'])!,
|
||||
content: attachedDatabase.typeMapping
|
||||
.read(DriftSqlType.string, data['${effectivePrefix}content'])!,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
ChatFts createAlias(String alias) {
|
||||
return ChatFts(attachedDatabase, alias);
|
||||
}
|
||||
|
||||
@override
|
||||
bool get dontWriteConstraints => true;
|
||||
@override
|
||||
String get moduleAndArgs =>
|
||||
'fts5(title, content, content=chat, tokenize="trigram")';
|
||||
}
|
||||
|
||||
class ChatFt extends DataClass implements Insertable<ChatFt> {
|
||||
final String title;
|
||||
final String content;
|
||||
const ChatFt({required this.title, required this.content});
|
||||
@override
|
||||
Map<String, Expression> toColumns(bool nullToAbsent) {
|
||||
final map = <String, Expression>{};
|
||||
map['title'] = Variable<String>(title);
|
||||
map['content'] = Variable<String>(content);
|
||||
return map;
|
||||
}
|
||||
|
||||
factory ChatFt.fromJson(Map<String, dynamic> json,
|
||||
{ValueSerializer? serializer}) {
|
||||
serializer ??= driftRuntimeOptions.defaultSerializer;
|
||||
return ChatFt(
|
||||
title: serializer.fromJson<String>(json['title']),
|
||||
content: serializer.fromJson<String>(json['content']),
|
||||
);
|
||||
}
|
||||
@override
|
||||
Map<String, dynamic> toJson({ValueSerializer? serializer}) {
|
||||
serializer ??= driftRuntimeOptions.defaultSerializer;
|
||||
return <String, dynamic>{
|
||||
'title': serializer.toJson<String>(title),
|
||||
'content': serializer.toJson<String>(content),
|
||||
};
|
||||
}
|
||||
|
||||
ChatFt copyWith({String? title, String? content}) => ChatFt(
|
||||
title: title ?? this.title,
|
||||
content: content ?? this.content,
|
||||
);
|
||||
@override
|
||||
String toString() {
|
||||
return (StringBuffer('ChatFt(')
|
||||
..write('title: $title, ')
|
||||
..write('content: $content')
|
||||
..write(')'))
|
||||
.toString();
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(title, content);
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
(other is ChatFt &&
|
||||
other.title == this.title &&
|
||||
other.content == this.content);
|
||||
}
|
||||
|
||||
class ChatFtsCompanion extends UpdateCompanion<ChatFt> {
|
||||
final Value<String> title;
|
||||
final Value<String> content;
|
||||
final Value<int> rowid;
|
||||
const ChatFtsCompanion({
|
||||
this.title = const Value.absent(),
|
||||
this.content = const Value.absent(),
|
||||
this.rowid = const Value.absent(),
|
||||
});
|
||||
ChatFtsCompanion.insert({
|
||||
required String title,
|
||||
required String content,
|
||||
this.rowid = const Value.absent(),
|
||||
}) : title = Value(title),
|
||||
content = Value(content);
|
||||
static Insertable<ChatFt> custom({
|
||||
Expression<String>? title,
|
||||
Expression<String>? content,
|
||||
Expression<int>? rowid,
|
||||
}) {
|
||||
return RawValuesInsertable({
|
||||
if (title != null) 'title': title,
|
||||
if (content != null) 'content': content,
|
||||
if (rowid != null) 'rowid': rowid,
|
||||
});
|
||||
}
|
||||
|
||||
ChatFtsCompanion copyWith(
|
||||
{Value<String>? title, Value<String>? content, Value<int>? rowid}) {
|
||||
return ChatFtsCompanion(
|
||||
title: title ?? this.title,
|
||||
content: content ?? this.content,
|
||||
rowid: rowid ?? this.rowid,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Map<String, Expression> toColumns(bool nullToAbsent) {
|
||||
final map = <String, Expression>{};
|
||||
if (title.present) {
|
||||
map['title'] = Variable<String>(title.value);
|
||||
}
|
||||
if (content.present) {
|
||||
map['content'] = Variable<String>(content.value);
|
||||
}
|
||||
if (rowid.present) {
|
||||
map['rowid'] = Variable<int>(rowid.value);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return (StringBuffer('ChatFtsCompanion(')
|
||||
..write('title: $title, ')
|
||||
..write('content: $content, ')
|
||||
..write('rowid: $rowid')
|
||||
..write(')'))
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
|
||||
abstract class _$ChatSearchDatabase extends GeneratedDatabase {
|
||||
_$ChatSearchDatabase(QueryExecutor e) : super(e);
|
||||
_$ChatSearchDatabaseManager get managers => _$ChatSearchDatabaseManager(this);
|
||||
late final Chat chat = Chat(this);
|
||||
late final ChatFts chatFts = ChatFts(this);
|
||||
late final Trigger chatAfterInsert = Trigger(
|
||||
'CREATE TRIGGER chat_after_insert AFTER INSERT ON chat BEGIN INSERT INTO chat_fts ("rowid", title, content) VALUES (new."rowid", new.title, new.content);END',
|
||||
'chat_after_insert');
|
||||
late final Trigger chatAfterDelete = Trigger(
|
||||
'CREATE TRIGGER chat_after_delete AFTER DELETE ON chat BEGIN INSERT INTO chat_fts (chat_fts, "rowid", title, content) VALUES (\'delete\', old."rowid", old.title, old.content);END',
|
||||
'chat_after_delete');
|
||||
late final Trigger chatAfterUpdate = Trigger(
|
||||
'CREATE TRIGGER chat_after_update AFTER UPDATE ON chat BEGIN INSERT INTO chat_fts (chat_fts, "rowid", title, content) VALUES (\'delete\', old."rowid", old.title, old.content);INSERT INTO chat_fts (chat_fts, title, content) VALUES (new."rowid", new.title, new.content);END',
|
||||
'chat_after_update');
|
||||
late final SearchDao searchDao = SearchDao(this as ChatSearchDatabase);
|
||||
Selectable<ChatQueryResult> chatQuery(
|
||||
{required String beforeMatch,
|
||||
required String afterMatch,
|
||||
required String ellipsis,
|
||||
required int snippetLength,
|
||||
required String query}) {
|
||||
return customSelect(
|
||||
'SELECT c.file_name, highlight(chat_fts, 0, ?1, ?2) AS title, snippet(chat_fts, 1, ?1, ?2, ?3, ?4) AS content_snippet FROM chat_fts(?5)AS fts INNER JOIN chat AS c ON c."rowid" = fts."rowid" ORDER BY RANK',
|
||||
variables: [
|
||||
Variable<String>(beforeMatch),
|
||||
Variable<String>(afterMatch),
|
||||
Variable<String>(ellipsis),
|
||||
Variable<int>(snippetLength),
|
||||
Variable<String>(query)
|
||||
],
|
||||
readsFrom: {
|
||||
chat,
|
||||
chatFts,
|
||||
}).map((QueryRow row) => ChatQueryResult(
|
||||
fileName: row.read<String>('file_name'),
|
||||
title: row.read<String>('title'),
|
||||
contentSnippet: row.read<String>('content_snippet'),
|
||||
));
|
||||
}
|
||||
|
||||
@override
|
||||
Iterable<TableInfo<Table, Object?>> get allTables =>
|
||||
allSchemaEntities.whereType<TableInfo<Table, Object?>>();
|
||||
@override
|
||||
List<DatabaseSchemaEntity> get allSchemaEntities =>
|
||||
[chat, chatFts, chatAfterInsert, chatAfterDelete, chatAfterUpdate];
|
||||
@override
|
||||
StreamQueryUpdateRules get streamUpdateRules => const StreamQueryUpdateRules(
|
||||
[
|
||||
WritePropagation(
|
||||
on: TableUpdateQuery.onTableName('chat',
|
||||
limitUpdateKind: UpdateKind.insert),
|
||||
result: [
|
||||
TableUpdate('chat_fts', kind: UpdateKind.insert),
|
||||
],
|
||||
),
|
||||
WritePropagation(
|
||||
on: TableUpdateQuery.onTableName('chat',
|
||||
limitUpdateKind: UpdateKind.delete),
|
||||
result: [
|
||||
TableUpdate('chat_fts', kind: UpdateKind.insert),
|
||||
],
|
||||
),
|
||||
WritePropagation(
|
||||
on: TableUpdateQuery.onTableName('chat',
|
||||
limitUpdateKind: UpdateKind.update),
|
||||
result: [
|
||||
TableUpdate('chat_fts', kind: UpdateKind.insert),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
typedef $ChatInsertCompanionBuilder = ChatCompanion Function({
|
||||
required String fileName,
|
||||
required String title,
|
||||
required String content,
|
||||
Value<int> rowid,
|
||||
});
|
||||
typedef $ChatUpdateCompanionBuilder = ChatCompanion Function({
|
||||
Value<String> fileName,
|
||||
Value<String> title,
|
||||
Value<String> content,
|
||||
Value<int> rowid,
|
||||
});
|
||||
|
||||
class $ChatTableManager extends RootTableManager<
|
||||
_$ChatSearchDatabase,
|
||||
Chat,
|
||||
ChatData,
|
||||
$ChatFilterComposer,
|
||||
$ChatOrderingComposer,
|
||||
$ChatProcessedTableManager,
|
||||
$ChatInsertCompanionBuilder,
|
||||
$ChatUpdateCompanionBuilder> {
|
||||
$ChatTableManager(_$ChatSearchDatabase db, Chat table)
|
||||
: super(TableManagerState(
|
||||
db: db,
|
||||
table: table,
|
||||
filteringComposer: $ChatFilterComposer(ComposerState(db, table)),
|
||||
orderingComposer: $ChatOrderingComposer(ComposerState(db, table)),
|
||||
getChildManagerBuilder: (p) => $ChatProcessedTableManager(p),
|
||||
getUpdateCompanionBuilder: ({
|
||||
Value<String> fileName = const Value.absent(),
|
||||
Value<String> title = const Value.absent(),
|
||||
Value<String> content = const Value.absent(),
|
||||
Value<int> rowid = const Value.absent(),
|
||||
}) =>
|
||||
ChatCompanion(
|
||||
fileName: fileName,
|
||||
title: title,
|
||||
content: content,
|
||||
rowid: rowid,
|
||||
),
|
||||
getInsertCompanionBuilder: ({
|
||||
required String fileName,
|
||||
required String title,
|
||||
required String content,
|
||||
Value<int> rowid = const Value.absent(),
|
||||
}) =>
|
||||
ChatCompanion.insert(
|
||||
fileName: fileName,
|
||||
title: title,
|
||||
content: content,
|
||||
rowid: rowid,
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
class $ChatProcessedTableManager extends ProcessedTableManager<
|
||||
_$ChatSearchDatabase,
|
||||
Chat,
|
||||
ChatData,
|
||||
$ChatFilterComposer,
|
||||
$ChatOrderingComposer,
|
||||
$ChatProcessedTableManager,
|
||||
$ChatInsertCompanionBuilder,
|
||||
$ChatUpdateCompanionBuilder> {
|
||||
$ChatProcessedTableManager(super.$state);
|
||||
}
|
||||
|
||||
class $ChatFilterComposer extends FilterComposer<_$ChatSearchDatabase, Chat> {
|
||||
$ChatFilterComposer(super.$state);
|
||||
ColumnFilters<String> get fileName => $state.composableBuilder(
|
||||
column: $state.table.fileName,
|
||||
builder: (column, joinBuilders) =>
|
||||
ColumnFilters(column, joinBuilders: joinBuilders));
|
||||
|
||||
ColumnFilters<String> get title => $state.composableBuilder(
|
||||
column: $state.table.title,
|
||||
builder: (column, joinBuilders) =>
|
||||
ColumnFilters(column, joinBuilders: joinBuilders));
|
||||
|
||||
ColumnFilters<String> get content => $state.composableBuilder(
|
||||
column: $state.table.content,
|
||||
builder: (column, joinBuilders) =>
|
||||
ColumnFilters(column, joinBuilders: joinBuilders));
|
||||
}
|
||||
|
||||
class $ChatOrderingComposer
|
||||
extends OrderingComposer<_$ChatSearchDatabase, Chat> {
|
||||
$ChatOrderingComposer(super.$state);
|
||||
ColumnOrderings<String> get fileName => $state.composableBuilder(
|
||||
column: $state.table.fileName,
|
||||
builder: (column, joinBuilders) =>
|
||||
ColumnOrderings(column, joinBuilders: joinBuilders));
|
||||
|
||||
ColumnOrderings<String> get title => $state.composableBuilder(
|
||||
column: $state.table.title,
|
||||
builder: (column, joinBuilders) =>
|
||||
ColumnOrderings(column, joinBuilders: joinBuilders));
|
||||
|
||||
ColumnOrderings<String> get content => $state.composableBuilder(
|
||||
column: $state.table.content,
|
||||
builder: (column, joinBuilders) =>
|
||||
ColumnOrderings(column, joinBuilders: joinBuilders));
|
||||
}
|
||||
|
||||
typedef $ChatFtsInsertCompanionBuilder = ChatFtsCompanion Function({
|
||||
required String title,
|
||||
required String content,
|
||||
Value<int> rowid,
|
||||
});
|
||||
typedef $ChatFtsUpdateCompanionBuilder = ChatFtsCompanion Function({
|
||||
Value<String> title,
|
||||
Value<String> content,
|
||||
Value<int> rowid,
|
||||
});
|
||||
|
||||
class $ChatFtsTableManager extends RootTableManager<
|
||||
_$ChatSearchDatabase,
|
||||
ChatFts,
|
||||
ChatFt,
|
||||
$ChatFtsFilterComposer,
|
||||
$ChatFtsOrderingComposer,
|
||||
$ChatFtsProcessedTableManager,
|
||||
$ChatFtsInsertCompanionBuilder,
|
||||
$ChatFtsUpdateCompanionBuilder> {
|
||||
$ChatFtsTableManager(_$ChatSearchDatabase db, ChatFts table)
|
||||
: super(TableManagerState(
|
||||
db: db,
|
||||
table: table,
|
||||
filteringComposer: $ChatFtsFilterComposer(ComposerState(db, table)),
|
||||
orderingComposer: $ChatFtsOrderingComposer(ComposerState(db, table)),
|
||||
getChildManagerBuilder: (p) => $ChatFtsProcessedTableManager(p),
|
||||
getUpdateCompanionBuilder: ({
|
||||
Value<String> title = const Value.absent(),
|
||||
Value<String> content = const Value.absent(),
|
||||
Value<int> rowid = const Value.absent(),
|
||||
}) =>
|
||||
ChatFtsCompanion(
|
||||
title: title,
|
||||
content: content,
|
||||
rowid: rowid,
|
||||
),
|
||||
getInsertCompanionBuilder: ({
|
||||
required String title,
|
||||
required String content,
|
||||
Value<int> rowid = const Value.absent(),
|
||||
}) =>
|
||||
ChatFtsCompanion.insert(
|
||||
title: title,
|
||||
content: content,
|
||||
rowid: rowid,
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
class $ChatFtsProcessedTableManager extends ProcessedTableManager<
|
||||
_$ChatSearchDatabase,
|
||||
ChatFts,
|
||||
ChatFt,
|
||||
$ChatFtsFilterComposer,
|
||||
$ChatFtsOrderingComposer,
|
||||
$ChatFtsProcessedTableManager,
|
||||
$ChatFtsInsertCompanionBuilder,
|
||||
$ChatFtsUpdateCompanionBuilder> {
|
||||
$ChatFtsProcessedTableManager(super.$state);
|
||||
}
|
||||
|
||||
class $ChatFtsFilterComposer
|
||||
extends FilterComposer<_$ChatSearchDatabase, ChatFts> {
|
||||
$ChatFtsFilterComposer(super.$state);
|
||||
ColumnFilters<String> get title => $state.composableBuilder(
|
||||
column: $state.table.title,
|
||||
builder: (column, joinBuilders) =>
|
||||
ColumnFilters(column, joinBuilders: joinBuilders));
|
||||
|
||||
ColumnFilters<String> get content => $state.composableBuilder(
|
||||
column: $state.table.content,
|
||||
builder: (column, joinBuilders) =>
|
||||
ColumnFilters(column, joinBuilders: joinBuilders));
|
||||
}
|
||||
|
||||
class $ChatFtsOrderingComposer
|
||||
extends OrderingComposer<_$ChatSearchDatabase, ChatFts> {
|
||||
$ChatFtsOrderingComposer(super.$state);
|
||||
ColumnOrderings<String> get title => $state.composableBuilder(
|
||||
column: $state.table.title,
|
||||
builder: (column, joinBuilders) =>
|
||||
ColumnOrderings(column, joinBuilders: joinBuilders));
|
||||
|
||||
ColumnOrderings<String> get content => $state.composableBuilder(
|
||||
column: $state.table.content,
|
||||
builder: (column, joinBuilders) =>
|
||||
ColumnOrderings(column, joinBuilders: joinBuilders));
|
||||
}
|
||||
|
||||
class _$ChatSearchDatabaseManager {
|
||||
final _$ChatSearchDatabase _db;
|
||||
_$ChatSearchDatabaseManager(this._db);
|
||||
$ChatTableManager get chat => $ChatTableManager(_db, _db.chat);
|
||||
$ChatFtsTableManager get chatFts => $ChatFtsTableManager(_db, _db.chatFts);
|
||||
}
|
||||
|
||||
class ChatQueryResult {
|
||||
final String fileName;
|
||||
final String title;
|
||||
final String contentSnippet;
|
||||
ChatQueryResult({
|
||||
required this.fileName,
|
||||
required this.title,
|
||||
required this.contentSnippet,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import 'package:bang_navigator/features/chat_archive/data/database/database.dart';
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:path_provider/path_provider.dart' as path_provider;
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:sqlite3/sqlite3.dart';
|
||||
import 'package:sqlite3_flutter_libs/sqlite3_flutter_libs.dart';
|
||||
import 'package:universal_io/io.dart';
|
||||
|
||||
part 'providers.g.dart';
|
||||
|
||||
@Riverpod()
|
||||
ChatSearchDatabase chatSearchDatabase(ChatSearchDatabaseRef ref) {
|
||||
return ChatSearchDatabase(
|
||||
LazyDatabase(() async {
|
||||
// Also work around limitations on old Android versions
|
||||
if (Platform.isAndroid) {
|
||||
await applyWorkaroundToOpenSqlite3OnOldAndroidVersions();
|
||||
}
|
||||
|
||||
// Make sqlite3 pick a more suitable location for temporary files - the
|
||||
// one from the system may be inaccessible due to sandboxing.
|
||||
final cachebase = (await path_provider.getTemporaryDirectory()).path;
|
||||
// We can't access /tmp on Android, which sqlite3 would try by default.
|
||||
// Explicitly tell it about the correct temporary directory.
|
||||
sqlite3.tempDirectory = cachebase;
|
||||
|
||||
return NativeDatabase.memory();
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'providers.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$chatSearchDatabaseHash() =>
|
||||
r'd7d2ad34264201d9196b28140b9b0ce0be6b7e1c';
|
||||
|
||||
/// See also [chatSearchDatabase].
|
||||
@ProviderFor(chatSearchDatabase)
|
||||
final chatSearchDatabaseProvider =
|
||||
AutoDisposeProvider<ChatSearchDatabase>.internal(
|
||||
chatSearchDatabase,
|
||||
name: r'chatSearchDatabaseProvider',
|
||||
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$chatSearchDatabaseHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef ChatSearchDatabaseRef = AutoDisposeProviderRef<ChatSearchDatabase>;
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member
|
||||
@@ -1,28 +0,0 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'chat_archive_file.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$chatArchiveFileRepositoryHash() =>
|
||||
r'50268506cf0c23ba1724ece6c9f44cb87be6299f';
|
||||
|
||||
/// See also [ChatArchiveFileRepository].
|
||||
@ProviderFor(ChatArchiveFileRepository)
|
||||
final chatArchiveFileRepositoryProvider = AutoDisposeNotifierProvider<
|
||||
ChatArchiveFileRepository, Raw<Stream<WatchEvent>>>.internal(
|
||||
ChatArchiveFileRepository.new,
|
||||
name: r'chatArchiveFileRepositoryProvider',
|
||||
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$chatArchiveFileRepositoryHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _$ChatArchiveFileRepository
|
||||
= AutoDisposeNotifier<Raw<Stream<WatchEvent>>>;
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member
|
||||
+18
-7
@@ -4,13 +4,13 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:universal_io/io.dart';
|
||||
import 'package:watcher/watcher.dart';
|
||||
|
||||
part 'chat_archive_file.g.dart';
|
||||
part 'file.g.dart';
|
||||
|
||||
@Riverpod()
|
||||
class ChatArchiveFileRepository extends _$ChatArchiveFileRepository {
|
||||
class ChatArchiveFileService extends _$ChatArchiveFileService {
|
||||
final Future<Directory> _archiveDirectoryFuture;
|
||||
|
||||
ChatArchiveFileRepository()
|
||||
ChatArchiveFileService()
|
||||
: _archiveDirectoryFuture =
|
||||
path_provider.getApplicationDocumentsDirectory().then(
|
||||
(documentDirectory) => Directory(
|
||||
@@ -19,7 +19,12 @@ class ChatArchiveFileRepository extends _$ChatArchiveFileRepository {
|
||||
);
|
||||
|
||||
Future<List<FileSystemEntity>> list() {
|
||||
return _archiveDirectoryFuture.then((value) => value.list().toList());
|
||||
return _archiveDirectoryFuture.then(
|
||||
(value) => value
|
||||
.list()
|
||||
.where((file) => path.extension(file.path) == '.md')
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> write(String fileName, String contents) async {
|
||||
@@ -49,13 +54,19 @@ class ChatArchiveFileRepository extends _$ChatArchiveFileRepository {
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Raw<Stream<WatchEvent>> build() async* {
|
||||
Stream<WatchEvent> _directoryStream() async* {
|
||||
final watcher = DirectoryWatcher(
|
||||
await _archiveDirectoryFuture
|
||||
.then((archiveDirectory) => archiveDirectory.absolute.path),
|
||||
);
|
||||
|
||||
yield* watcher.events;
|
||||
yield* watcher.events.where((event) => path.extension(event.path) == '.md');
|
||||
}
|
||||
|
||||
@override
|
||||
Raw<Stream<WatchEvent>> build() {
|
||||
//We return a Raw stream here and yield* doesnt support broadcast.
|
||||
//So it is required to use asBroadcastStream here
|
||||
return _directoryStream().asBroadcastStream();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'file.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$chatArchiveFileServiceHash() =>
|
||||
r'21a60ae20e01cfd4f25f7f744fb9bc0c5a465e09';
|
||||
|
||||
/// See also [ChatArchiveFileService].
|
||||
@ProviderFor(ChatArchiveFileService)
|
||||
final chatArchiveFileServiceProvider = AutoDisposeNotifierProvider<
|
||||
ChatArchiveFileService, Raw<Stream<WatchEvent>>>.internal(
|
||||
ChatArchiveFileService.new,
|
||||
name: r'chatArchiveFileServiceProvider',
|
||||
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$chatArchiveFileServiceHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _$ChatArchiveFileService = AutoDisposeNotifier<Raw<Stream<WatchEvent>>>;
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member
|
||||
Reference in New Issue
Block a user