Skip to content

Commit

Permalink
✨ histories
Browse files Browse the repository at this point in the history
  • Loading branch information
niuhuan committed Nov 10, 2023
1 parent 45b7bf2 commit 639794d
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 25 deletions.
3 changes: 2 additions & 1 deletion lib/screens/app_screen.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:kobi/screens/books_screen.dart';
import 'rank_screen.dart';
import 'recommends_screen.dart';
import 'user_screen.dart';
Expand Down Expand Up @@ -86,7 +87,7 @@ class _AppScreenState extends State<AppScreen> {
Icons.filter_list_sharp,
),
AppScreenData(
DiscoveryScreen(),
BooksScreen(),
'书架',
Icons.book_outlined,
),
Expand Down
56 changes: 56 additions & 0 deletions lib/screens/books_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'dart:convert';

import 'package:flutter/material.dart';

import '../bridge_generated.dart';
import '../ffi.io.dart';
import 'components/comic_list.dart';
import 'components/comic_pager.dart';

class BooksScreen extends StatelessWidget {
const BooksScreen({super.key});

@override
Widget build(BuildContext context) {
final pager = ComicPager(fetcher: (offset, limit) async {
final result = await api.listComicViewLogs(offset: offset, limit: limit);
return CommonPage<CommonComicInfo>(
list: result.list
.map((e) =>
CommonComicInfo(
author: _mapAuthor(List.of(jsonDecode(e.comicAuthors)).cast()),
cover: e.comicCover,
imgType: 1,
name: e.comicName,
pathWord: e.comicPathWord,
popular: 0,
males: [],
females: [],
))
.toList(),
total: result.total,
limit: result.limit,
offset: result.offset,
);
});
return Scaffold(
appBar: AppBar(
title: const Text("历史记录"),
),
body: pager,
);
}
}

List<Author> _mapAuthor(List<Map> list) {
List<Author> result = [];
for (var value in list) {
if (value['name'] != null && value['path_word'] != null) {
result.add(Author(
name: value['name'],
pathWord: value['path_word'],
));
}
}
return result;
}
45 changes: 26 additions & 19 deletions lib/screens/components/comic_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,32 +98,39 @@ class CommonComicCard extends StatelessWidget {
color: Colors.red.shade300,
),
),
Container(
height: 5,
),
Text.rich(TextSpan(children: [
const WidgetSpan(
child: Icon(
Icons.local_fire_department,
size: 16,
color: Colors.grey,
),
),
TextSpan(
text: comic.popular.toString(),
style: const TextStyle(
fontSize: 12,
color: Colors.grey,
),
),
])),
..._popular(),
],
),
),
],
),
);
}

List<Widget> _popular() {
if (comic.popular == 0) return [];
return [
Container(
height: 5,
),
Text.rich(TextSpan(children: [
const WidgetSpan(
child: Icon(
Icons.local_fire_department,
size: 16,
color: Colors.grey,
),
),
TextSpan(
text: comic.popular.toString(),
style: const TextStyle(
fontSize: 12,
color: Colors.grey,
),
),
])),
];
}
}

class CommonComicInfo {
Expand Down
15 changes: 14 additions & 1 deletion native/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::database::cache::{image_cache, web_cache};
use crate::database::properties::property;
use crate::udto::{
UICacheImage, UIChapterData, UIComicData, UIComicInExplore, UIComicQuery, UIPageComicChapter,
UIPageComicInExplore, UIPageRankItem, UIPageUIComicInList, UITags, UIViewLog,
UIPageComicInExplore, UIPageRankItem, UIPageUIComicInList, UIPageUIViewLog, UITags, UIViewLog,
};
use crate::utils::{hash_lock, join_paths};
use crate::{get_image_cache_dir, CLIENT, RUNTIME};
Expand Down Expand Up @@ -224,6 +224,19 @@ pub fn find_comic_view_log(path_word: String) -> Result<Option<UIViewLog>> {
})
}

pub fn list_comic_view_logs(offset: i64, limit: i64) -> Result<UIPageUIViewLog> {
block_on(async move {
let count = comic_view_log::count().await?;
let list = comic_view_log::load_view_logs(offset as u64, limit as u64).await?;
Ok(UIPageUIViewLog {
total: count as i64,
limit,
offset,
list: list.into_iter().map(UIViewLog::from).collect(),
})
})
}

pub fn cache_image(
cache_key: String,
url: String,
Expand Down
13 changes: 10 additions & 3 deletions native/src/database/active/comic_view_log.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::database::active::ACTIVE_DATABASE;
use crate::database::{create_index, create_table_if_not_exists, index_exists};
use anyhow::anyhow;
use sea_orm::entity::prelude::*;
use sea_orm::QueryOrder;
use sea_orm::QuerySelect;
Expand Down Expand Up @@ -86,16 +87,22 @@ pub(crate) async fn view_page(model: Model) -> anyhow::Result<()> {
Ok(())
}

pub(crate) async fn load_view_logs(page: i64) -> anyhow::Result<Vec<Model>> {
pub(crate) async fn load_view_logs(offset: u64, limit: u64) -> anyhow::Result<Vec<Model>> {
let db = ACTIVE_DATABASE.get().unwrap().lock().await;
Ok(Entity::find()
.order_by_desc(Column::ViewTime)
.offset(page as u64 * 20)
.limit(20)
.offset(offset)
.limit(limit)
.all(db.deref())
.await?)
}

pub(crate) async fn count() -> anyhow::Result<u64> {
let db = ACTIVE_DATABASE.get().unwrap().lock().await;
let count = Entity::find().count(db.deref()).await?;
Ok(count)
}

pub(crate) async fn view_log_by_comic_path_word(
path_word: String,
) -> anyhow::Result<Option<Model>> {
Expand Down
2 changes: 1 addition & 1 deletion native/src/database/download/download_comic_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::convert::TryInto;
use std::ops::Deref;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "download_comic_chapter")]
#[sea_orm(table_name = "download_comic_group")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub comic_path_word: String,
Expand Down
8 changes: 8 additions & 0 deletions native/src/udto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,11 @@ impl From<ComicInExplore> for UIComicInExplore {
}
}
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UIPageUIViewLog {
pub list: Vec<UIViewLog>,
pub total: i64,
pub limit: i64,
pub offset: i64,
}

0 comments on commit 639794d

Please sign in to comment.