Skip to content

Commit

Permalink
chore: remove some codes
Browse files Browse the repository at this point in the history
  • Loading branch information
guchengxi1994 committed May 29, 2024
1 parent 7525ca4 commit aa7536d
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 189 deletions.
74 changes: 74 additions & 0 deletions lib/llm/editor/components/article_item.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import 'package:all_in_one/isar/article.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

typedef OnArticleSelected = void Function(Article);

class ArticleItem extends ConsumerStatefulWidget {
const ArticleItem(
{super.key, required this.article, required this.onArticleSelected});
final Article article;
final OnArticleSelected onArticleSelected;

@override
ConsumerState<ArticleItem> createState() => _ArticleItemState();
}

class _ArticleItemState extends ConsumerState<ArticleItem> {
bool isHovering = false;

@override
Widget build(BuildContext context) {
return MouseRegion(
cursor: SystemMouseCursors.click,
onEnter: (event) {
setState(() {
isHovering = true;
});
},
onExit: (event) {
setState(() {
isHovering = false;
});
},
child: GestureDetector(
onTap: () {
widget.onArticleSelected(widget.article);
},
child: Container(
height: 40,
decoration: BoxDecoration(
color: isHovering
? const Color.fromARGB(255, 197, 195, 227).withAlpha(100)
: Colors.transparent,
borderRadius: BorderRadius.circular(5)),
child: Row(
children: [
Expanded(
flex: 1,
child: Text(
widget.article.title,
maxLines: 1,
softWrap: true,
overflow: TextOverflow.ellipsis,
),
),
Expanded(
flex: 1,
child: Text(
DateTime.fromMillisecondsSinceEpoch(widget.article.createAt)
.toString()
.split(".")
.first,
maxLines: 1,
softWrap: true,
overflow: TextOverflow.ellipsis,
),
),
],
),
),
),
);
}
}
77 changes: 77 additions & 0 deletions lib/llm/editor/components/load_article_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import 'package:all_in_one/llm/editor/components/article_item.dart';
import 'package:all_in_one/llm/editor/notifiers/article_notifier.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class LoadArticleDialog extends ConsumerStatefulWidget {
const LoadArticleDialog({super.key});

@override
ConsumerState<LoadArticleDialog> createState() => _LoadArticleDialogState();
}

class _LoadArticleDialogState extends ConsumerState<LoadArticleDialog> {
@override
Widget build(BuildContext context) {
final state = ref.watch(articleProvider);

return Material(
elevation: 10,
borderRadius: BorderRadius.circular(20),
child: Container(
padding: const EdgeInsets.all(20),
width: MediaQuery.of(context).size.width * 0.8,
height: MediaQuery.of(context).size.height * 0.8,
decoration: BoxDecoration(borderRadius: BorderRadius.circular(20)),
child: state.when(
data: (v) {
if (v.isEmpty) {
return const Center(
child: Text("There is no template created."),
);
}

return Column(
children: [
const SizedBox(
height: 50,
child: Row(
children: [
Expanded(
flex: 1,
child: Text("name"),
),
Expanded(
flex: 1,
child: Text("create at"),
)
],
),
),
Expanded(
child: ListView.builder(
itemBuilder: (c, i) {
return ArticleItem(
article: v[i],
onArticleSelected: (p0) {
Navigator.of(context).pop(p0);
},
);
},
itemCount: v.length,
)),
],
);
},
error: (_, s) {
return Center(
child: Text(s.toString()),
);
},
loading: () => const Center(
child: CircularProgressIndicator(),
)),
),
);
}
}
85 changes: 83 additions & 2 deletions lib/llm/editor/compose_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import 'dart:convert';

import 'package:all_in_one/common/toast_utils.dart';
import 'package:all_in_one/isar/article.dart';
import 'package:all_in_one/isar/llm_history.dart';
import 'package:all_in_one/isar/llm_template.dart';
import 'package:all_in_one/llm/ai_client.dart';
import 'package:all_in_one/llm/editor/components/load_article_dialog.dart';
import 'package:all_in_one/llm/editor/models/datasource.dart';
import 'package:all_in_one/llm/editor/notifiers/article_notifier.dart';
import 'package:all_in_one/llm/global/components/load_template_dialog.dart';
import 'package:all_in_one/llm/global/components/loading_dialog.dart';
import 'package:all_in_one/llm/global/components/sidemenu.dart';
Expand Down Expand Up @@ -93,12 +96,90 @@ class _ComposeScreenState extends ConsumerState<ComposeScreen> {
SidemenuButton(
icon: Icons.file_open_outlined,
title: "Load last",
onTap: () {},
onTap: () async {
final Article? article =
await ref.read(articleProvider.notifier).getLast();
if (article != null) {
setState(() {
_widgetBuilder = (context) => Editor(
// jsonString: Future(() => _jsonString),
datasource: Datasource(
type: DatasourceType.json, content: ""),
onEditorStateChange: (editorState) {
_editorState = editorState;
},
showTemplateFeatures: false,
);
isTemplateLoaded = true;
});

Future.delayed(const Duration(milliseconds: 200)).then(
(value) {
setState(() {
_widgetBuilder = (context) => Editor(
// jsonString: Future(() => _jsonString),
datasource: Datasource(
type: DatasourceType.json,
content: article.content),
onEditorStateChange: (editorState) {
_editorState = editorState;
},
showTemplateFeatures: false,
);
isTemplateLoaded = true;
});
},
);
}
},
),
SidemenuButton(
icon: Icons.file_open,
title: "Load article...",
onTap: () {},
onTap: () async {
final Article? article = await showGeneralDialog(
barrierColor: Colors.transparent,
barrierDismissible: true,
barrierLabel: "load-article",
context: context,
pageBuilder: (c, _, __) {
return const Center(
child: LoadArticleDialog(),
);
});
if (article != null) {
setState(() {
_widgetBuilder = (context) => Editor(
// jsonString: Future(() => _jsonString),
datasource: Datasource(
type: DatasourceType.json, content: ""),
onEditorStateChange: (editorState) {
_editorState = editorState;
},
showTemplateFeatures: false,
);
isTemplateLoaded = true;
});

Future.delayed(const Duration(milliseconds: 200)).then(
(value) {
setState(() {
_widgetBuilder = (context) => Editor(
// jsonString: Future(() => _jsonString),
datasource: Datasource(
type: DatasourceType.json,
content: article.content),
onEditorStateChange: (editorState) {
_editorState = editorState;
},
showTemplateFeatures: false,
);
isTemplateLoaded = true;
});
},
);
}
},
),
SidemenuDivider(),
SidemenuLabel(title: "Template"),
Expand Down
31 changes: 31 additions & 0 deletions lib/llm/editor/notifiers/article_notifier.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'dart:async';

import 'package:all_in_one/isar/article.dart';
import 'package:all_in_one/isar/database.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:isar/isar.dart';

class ArticleNotifier extends AutoDisposeAsyncNotifier<List<Article>> {
final IsarDatabase isarDatabase = IsarDatabase();

@override
FutureOr<List<Article>> build() async {
final articles = await isarDatabase.isar!.articles
.where()
.sortByCreateAtDesc()
.findAll();
return articles;
}

Future<Article?> getLast() async {
return await isarDatabase.isar!.articles
.where()
.sortByCreateAtDesc()
.findFirst();
}
}

final articleProvider =
AutoDisposeAsyncNotifierProvider<ArticleNotifier, List<Article>>(() {
return ArticleNotifier();
});
Loading

0 comments on commit aa7536d

Please sign in to comment.