提交 e68a6ef4 authored 作者: yueweilu's avatar yueweilu

1、目录保持开闭状态

2、离线时目录跳转阅读页逻辑
上级 1cf7f0d8
......@@ -112,7 +112,17 @@ class _BuildItemState extends State<BuildItem> {
/// 章节名称容器
GestureDetector(
onTap: (){
widget.onTap(chapterModel);
if(chapterModel.children!.isEmpty){
widget.onTap(chapterModel);
}
else{
setState(() {
chapterModel.selected = !chapterModel.selected;
});
}
// setState(() {
// chapterModel.selected = !chapterModel.selected;
// });
// // 如果章下面没有节 点击才会跳转
// if (widget.model.children!.isEmpty){
// widget.onTap(widget.model);
......@@ -156,7 +166,7 @@ class _BuildItemState extends State<BuildItem> {
chapterModel.selected = !chapterModel.selected;
});
},
child: Container(
child: SizedBox(
width: 20.w,
height: 20.w,
// color: Colors.red,
......
......@@ -48,36 +48,26 @@ class BookDetailController extends GetxController with GetSingleTickerProviderSt
}
}
/// 写入特定数据
void writeData(List<ChapterModel> listA, List<ChapterModel> listB){
for(int i = 0; i < listA.length;i++){
ChapterModel aModel = listA[i];
ChapterModel bModel = listB[i];
if(aModel.id == bModel.id){
// aModel.seen = bModel.seen;
bModel.selected = aModel.selected;
}
writeData(aModel.children!, bModel.children!);
}
}
/// 获取目录信息
void getChapters() async {
final result = await LibraryAPI.chapters(bookId: bookId);
writeData(chapters, result);
// final result = await LibraryAPI.chapters(bookId: bookId);
// for(int index = 0;index < chapters.length;index++){
// ChapterModel oModel = chapters[index];
// ChapterModel nModel = result[index];
// if()
// }
chapters = await LibraryAPI.chapters(bookId: bookId);
// chapters = [
// ChapterModel(name: '第一章',selected: false, children: [
// ChapterModel(name: '第一节',children: [
// ChapterModel(name: '第一小节',children: []),
// ]),
// ChapterModel(name: '第二节',children: [
// ChapterModel(name: '第二小节',children: []),
// ])
// ]),
// ChapterModel(name: '第二章',children: [
// ChapterModel(name: '第一节',children: [
// ChapterModel(name: '第1小节',children: []),
// ]),
// ChapterModel(name: '第一节',children: [
// ChapterModel(name: '第1小节',children: []),
// ])
// ]),
// ];
chapters = result;
update();
}
......@@ -85,12 +75,16 @@ class BookDetailController extends GetxController with GetSingleTickerProviderSt
void getBookDetails() async {
bookDetails = await LibraryAPI.details(bookId:bookId);
// 将阅读最后章节写入到数据库
try {
final result = await SqlManager.updateReadHistoryByBookId(int.parse(bookId), bookDetails.chapterId!.toInt());
Console.log('Sql-------存入数据库读到的章节----------------book_id:$bookId-----chapterId:${bookDetails.chapterId!.toInt()}---------result:$result--');
}
catch(err){
Console.log('Sql--------------err------------------$err');
bool netStatus = await Tools.checkCurrentNetStatus();
// 有网络的时候才会 存储 其余不存储
if (netStatus){
try {
final result = await SqlManager.updateReadHistoryByBookId(int.parse(bookId), bookDetails.chapterId!.toInt());
Console.log('Sql-------存入数据库读到的章节----------------book_id:$bookId-----chapterId:${bookDetails.chapterId!.toInt()}---------result:$result--');
}
catch(err){
Console.log('Sql--------------err------------------$err');
}
}
update();
......@@ -108,14 +102,77 @@ class BookDetailController extends GetxController with GetSingleTickerProviderSt
}
bool result = await CommonAPI.love(
bookId: bookId, love: isCollection.toString());
Console.log('================================$result');
if (result) {
getBookDetails();
}
}
// 获取当前的章节id
Future<String> getCurrentChapterId() async{
String chapterId = '';
bool netStatus = await Tools.checkCurrentNetStatus();
final exist = await _isExistFile(bookId);
if (!netStatus && exist){
chapterId = await SqlManager.queryReadHistoryByBookId(int.parse(bookId));
}
else{
chapterId = bookDetails.chapterId.toString();
}
return chapterId;
}
// 获取当前的章节名称
Future<String> getCurrentChapterName() async{
String chapterName = '';
bool netStatus = await Tools.checkCurrentNetStatus();
final exist = await _isExistFile(bookId);
if (!netStatus && exist){
String chapterId = await SqlManager.queryReadHistoryByBookId(int.parse(bookId));
chapterName = _getChapterName(chapterId);
}
else{
chapterName = bookDetails.chapterName.toString();
}
Console.log('getCurrentChapterName-------------------------------$chapterName-');
return chapterName;
}
// 判断是否存在离线文件
Future<bool> _isExistFile(String bookId) async {
bool existDownFile= false;
String directoryPath = await Tools.getDirectory();
Directory directory = Directory(directoryPath);
bool directoryExists = await directory.exists();
if (directoryExists) {
Console.log('存在名为 "$bookId" 的文件夹');
existDownFile = await Directory('${directory.path}/$bookId').exists();
}
else {
Console.log('不存在名为 "$bookId" 的文件夹');
existDownFile = false;
}
return existDownFile;
}
String _getChapterName(String chapterId){
for (ChapterModel model in chapters){
if ('${model.id}' == chapterId){
return model.name??'';
}
if (model.children !=null){
for (ChapterModel subModel in model.children!){
if ('${subModel.id}' == chapterId){
return subModel.name??'';
}
}
}
}
return '';
}
......
library book_detail;
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_book/apis/index.dart';
import 'package:flutter_book/theme.dart';
......
......@@ -209,7 +209,9 @@ class _BookDetailPageState extends State<BookDetailPage> with SingleTickerProvid
// 1免费 0 不免费
if(controller.bookDetails.isFree == 1){
final result = await context.pushNamed(Routes.web,queryParameters: {'book_id': controller.bookDetails.bookId.toString(),'chapter_id': controller.bookDetails.chapterId.toString(),'chapter_name':controller.bookDetails.chapterName.toString(),'note_id':'0'},extra: controller.bookDetails);
final String chapterId = await controller.getCurrentChapterId();
final String chapterName = await controller.getCurrentChapterName();
final result = await context.pushNamed(Routes.web,queryParameters: {'book_id': controller.bookDetails.bookId.toString(),'chapter_id': chapterId,'chapter_name':chapterName,'note_id':'0'},extra: controller.bookDetails);
if (result == true){
controller.getChapters();
}
......@@ -238,14 +240,25 @@ class _BookDetailPageState extends State<BookDetailPage> with SingleTickerProvid
}
}
else{
final result = await context.pushNamed(Routes.web,queryParameters: {'book_id': controller.bookDetails.bookId.toString(),'chapter_id': controller.bookDetails.chapterId.toString(),'chapter_name':controller.bookDetails.chapterName.toString(),'note_id':'0'},extra: controller.bookDetails);
final String chapterId = await controller.getCurrentChapterId();
final String chapterName = await controller.getCurrentChapterName();
final result = await context.pushNamed(Routes.web,queryParameters: {'book_id': controller.bookDetails.bookId.toString(),'chapter_id': chapterId,'chapter_name':chapterName,'note_id':'0'},extra: controller.bookDetails);
if (result == true){
controller.getChapters();
}
}
}
else{
final result = await context.pushNamed(Routes.web,queryParameters: {'book_id': controller.bookDetails.bookId.toString(),'chapter_id': controller.bookDetails.chapterId.toString(),'chapter_name':controller.bookDetails.chapterName.toString(),'note_id':'0'},extra: controller.bookDetails);
// bool netStatus = await Tools.checkCurrentNetStatus();
// final exist = await _isExistFile(bookId);
// if (!netStatus && exist){
// String chapterId = await SqlManager.queryReadHistoryByBookId(int.parse(bookId));
// }
final String chapterId = await controller.getCurrentChapterId();
final String chapterName = await controller.getCurrentChapterName();
final result = await context.pushNamed(Routes.web,queryParameters: {'book_id': controller.bookDetails.bookId.toString(),'chapter_id': chapterId,'chapter_name':chapterName,'note_id':'0'},extra: controller.bookDetails);
if (result == true){
controller.getChapters();
}
......@@ -276,5 +289,4 @@ class _BookDetailPageState extends State<BookDetailPage> with SingleTickerProvid
}
}
......@@ -85,6 +85,9 @@ class ReadController extends FullLifeCycleController with GetSingleTickerProvide
late SearchAllModel sModel = SearchAllModel(bookId: '0');
// 本地文件地址
String localHtml5Path = '';
///------------------------------------------ 页面 生命周期--------------------------------------------------------
......@@ -104,18 +107,12 @@ class ReadController extends FullLifeCycleController with GetSingleTickerProvide
netStatus = await Tools.checkCurrentNetStatus();
final exist = await _isExistFile(bookId);
// chapterId = await SqlManager.queryReadHistoryByBookId(int.parse(bookId));
// Console.log('数据库中得到最后阅读的章节----------------------$chapterId----------');
if (!netStatus && exist){
// 1、从数据库中获取读到那个章节
chapterId = await SqlManager.queryReadHistoryByBookId(int.parse(bookId));
Console.log('Sql-----------数据库中得到最后阅读的章节----------------------$chapterId----------');
chapterName = getChapterName(chapterId);
// 2、通过 chapterId 获取 对应离线的 html路径
// 1、通过 chapterId 获取 对应离线的 html路径
String toReadHtmlPath = await getLocalReadHtml(chapterId);
// 3、开始读书
// 2、开始读书
read(toReadHtmlPath);
// 获取当前 文件名称
......@@ -689,10 +686,16 @@ class ReadController extends FullLifeCycleController with GetSingleTickerProvide
File file = File(writeFilePath);
// 7、将HTML内容写入文件
await file.writeAsString(htmlStr);
localHtml5Path = writeFilePath;
webViewController.loadUrl(urlRequest: URLRequest(
// url: Uri.parse(writeFilePath),
url: WebUri.uri(Uri.parse(writeFilePath))
));
final result = await SqlManager.updateReadHistoryByBookId(int.parse(bookId), int.parse(chapterId));
Console.log('Sql----readread---存入数据库读到的章节----------------book_id:$bookId-----chapterId:$chapterId---------result:$result--');
queryLocalNote();
}
......
......@@ -30,6 +30,7 @@ class _ReadPageState extends State<ReadPage> {
return WillPopScope(
onWillPop: () async {
context.pop(true);
CustomToast.dismiss();
return false;
},
child: GetBuilder<ReadController>(
......@@ -69,9 +70,9 @@ class _ReadPageState extends State<ReadPage> {
child: Stack(
children: [
InAppWebView(
initialUrlRequest: readController.netStatus == false?null:URLRequest(
initialUrlRequest:URLRequest(
// url: Uri.parse(kReadBook),
url: WebUri.uri(Uri.parse(kReadBook))
url: readController.localHtml5Path.isNotEmpty?WebUri.uri(Uri.parse(readController.localHtml5Path)): WebUri.uri(Uri.parse(kReadBook))
// url: Uri.parse("/storage/emulated/0/Android/data/com.zijin.book.flutter_book/files/174/7-325.html"),
),
initialSettings:InAppWebViewSettings(
......
......@@ -98,7 +98,18 @@ class _BuildItemState extends State<BuildItem> {
/// 章节名称容器
GestureDetector(
onTap: (){
widget.onTapChapter(chapterModel);
if(chapterModel.children!.isEmpty){
widget.onTapChapter(chapterModel);
}
else{
setState(() {
chapterModel.selected = !chapterModel.selected;
});
}
// // widget.onTapChapter(chapterModel);
// setState(() {
// chapterModel.selected = !chapterModel.selected;
// });
// // 如果章下面没有节 点击才会跳转
// if (widget.model.children!.isEmpty){
// widget.onTap(widget.model);
......
......@@ -53,8 +53,10 @@ class SqlManager {
static Future<Database?> getCurrentDatabase() async {
if (_database == null) {
Console.log('Sql-------------getCurrentDatabase---------------_database == null----');
await init();
}
Console.log('Sql-------------getCurrentDatabase---------------$_database');
return _database;
}
......@@ -146,18 +148,29 @@ class SqlManager {
}
// 根据 book_id 查询当前读到的 章节
static Future<String> queryReadHistoryByBookId(int bookId) async {
Database? db = await SqlManager.getCurrentDatabase();
List<Map<String, dynamic>>? results = await db?.query(
'read_history',
where: 'book_id = ?',
whereArgs: [bookId],
);
return results?.first['chapter_id'].toString() ?? '';
try {
Database? db = await SqlManager.getCurrentDatabase();
if (!db!.isOpen){
await init();
}
List<Map<String, dynamic>>? results = await db?.query(
'read_history',
where: 'book_id = ?',
whereArgs: [bookId],
);
return results?.first['chapter_id'].toString() ?? '';
}
catch(e){
Console.log('Error querying read history by book id: $e');
return '';
}
}
// 根据 book_id 写入当前读到的 章节
static Future<int> updateReadHistoryByBookId(int bookId ,int chapterId) async {
Database? db = await SqlManager.getCurrentDatabase();
final queryResult = await queryReadHistoryByBookId(bookId);
if (queryResult.isEmpty){
Console.log('Sql----------没有当前书籍的数据----------------------');
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论