Skip to content
This repository was archived by the owner on Jan 4, 2025. It is now read-only.

Commit efa1c18

Browse files
authored
修复导入在线书籍时的问题 (#42)
1 parent 3c891c5 commit efa1c18

File tree

11 files changed

+56
-23
lines changed

11 files changed

+56
-23
lines changed

src/CleanReader.App/Controls/Popups/BookInformationDialog.xaml.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ public override void OnPrimaryButtonClick()
3838
_viewModel.Book.Author = AuthorBox.Text;
3939
}
4040

41-
if (string.IsNullOrEmpty(CoverBox.Text))
42-
{
43-
_viewModel.Book.Cover = CoverBox.Text;
44-
}
41+
_viewModel.Cover = CoverBox.Text;
4542

4643
if (!string.IsNullOrEmpty(DescriptionBox.Text))
4744
{

src/CleanReader.App/Pages/MainPage.xaml.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,6 @@ private async void OnLoadedAsync(object sender, RoutedEventArgs e)
111111
{
112112
await _libraryViewModel.CheckContinueReadingAsync();
113113
}
114-
else
115-
{
116-
await _libraryViewModel.CheckOpenFileOrImportAsync();
117-
}
118114

119115
#if !DEBUG
120116
_viewModel.CheckGithubUpdateCommand.Execute().Subscribe();

src/CleanReader.App/Program.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
// Copyright (c) Richasy. All rights reserved.
22

33
using System;
4+
using System.Linq;
45
using System.Runtime.InteropServices;
56
using System.Threading;
67
using System.Threading.Tasks;
8+
using CleanReader.ViewModels.Desktop;
79
using Microsoft.UI.Dispatching;
810
using Microsoft.Windows.AppLifecycle;
11+
using Windows.ApplicationModel.Activation;
12+
using Windows.Storage;
913

1014
namespace CleanReader.App;
1115

@@ -41,15 +45,14 @@ private static bool DecideRedirection()
4145
var isRedirect = false;
4246

4347
var args = AppInstance.GetCurrent().GetActivatedEventArgs();
44-
var kind = args.Kind;
4548

4649
try
4750
{
4851
var keyInstance = AppInstance.FindOrRegisterForKey("CleanReader");
4952

5053
if (keyInstance.IsCurrent)
5154
{
52-
keyInstance.Activated += OnActivated;
55+
keyInstance.Activated += OnActivatedAsync;
5356
}
5457
else
5558
{
@@ -59,6 +62,7 @@ private static bool DecideRedirection()
5962
}
6063
catch (Exception)
6164
{
65+
isRedirect = true;
6266
}
6367

6468
return isRedirect;
@@ -78,9 +82,9 @@ private static bool DecideRedirection()
7882
private static void RedirectActivationTo(AppActivationArguments args, AppInstance keyInstance)
7983
{
8084
redirectEventHandle = CreateEvent(IntPtr.Zero, true, false, null);
81-
Task.Run(() =>
85+
Task.Run(async () =>
8286
{
83-
keyInstance.RedirectActivationToAsync(args).AsTask().Wait();
87+
await keyInstance.RedirectActivationToAsync(args);
8488
SetEvent(redirectEventHandle);
8589
});
8690
uint cWMO_DEFAULT = 0;
@@ -93,6 +97,17 @@ private static void RedirectActivationTo(AppActivationArguments args, AppInstanc
9397
out var handleIndex);
9498
}
9599

96-
private static void OnActivated(object sender, AppActivationArguments args)
97-
=> _ = args.Kind;
100+
private static async void OnActivatedAsync(object sender, AppActivationArguments args)
101+
{
102+
if (args.Kind == ExtendedActivationKind.File)
103+
{
104+
var fileArgs = args.Data as FileActivatedEventArgs;
105+
var file = fileArgs.Files.FirstOrDefault();
106+
if (file is StorageFile f)
107+
{
108+
AppViewModel.Instance.InitializeFilePath = f.Path;
109+
await LibraryViewModel.Instance.CheckOpenFileOrImportAsync();
110+
}
111+
}
112+
}
98113
}

src/Services/EpubService/EpubService.Epub.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ private async Task CreateOpfFileAsync()
5858
foreach (var file in _files.Where(p => !p.Equals(Configuration.TitlePagePath, StringComparison.OrdinalIgnoreCase)))
5959
{
6060
var pathName = Path.GetFileNameWithoutExtension(file);
61+
if (!_chapterNames.ContainsKey(pathName))
62+
{
63+
continue;
64+
}
65+
6166
var fileName = NormalizeTitle(_chapterNames[pathName]);
6267
manifestBuilder.AppendLine(new string(' ', 12) + $"<item id=\"ch{pathName}\" href=\"Pages/{pathName}.html\" media-type=\"application/xhtml+xml\"/>");
6368
spineBuilder.AppendLine(new string(' ', 12) + $"<itemref idref=\"ch{pathName}\"/>");

src/Services/EpubService/EpubService.Txt.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private static async Task GenerateHtmlFromTxtAsync(string title, string[] lines,
4848
.Replace("{{body}}", string.Join(string.Empty, lines.Select(p => $"<p>{p}</p>")));
4949
if (_chapterNames == null)
5050
{
51-
_chapterNames = new System.Collections.Generic.Dictionary<string, string>();
51+
_chapterNames = new Dictionary<string, string>();
5252
}
5353

5454
_chapterNames.Add(index.ToString("0000"), title);

src/Services/NovelService/Models/Source/SearchConfig.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,10 @@ public class SearchConfig : BookInformationConfigBase
2121
/// 是否需要进一步请求书籍详情.
2222
/// </summary>
2323
public bool NeedDetail { get; set; }
24+
25+
/// <summary>
26+
/// 是否需要对关键词进行Url编码.
27+
/// </summary>
28+
public bool EncodingKeyword { get; set; }
2429
}
2530
}

src/Services/NovelService/NovelService.Chapter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ public partial class NovelService
1414
{
1515
private static void InitializeChapter(Chapter chapter, ChapterConfig chapterConfig, HtmlNode node)
1616
{
17-
var titleNode = node.QuerySelector(chapterConfig.Title?.Rule);
17+
var titleNode = string.IsNullOrEmpty(chapterConfig.Title?.Rule)
18+
? node
19+
: node.QuerySelector(chapterConfig.Title?.Rule);
1820

1921
if (titleNode != null)
2022
{

src/Services/NovelService/NovelService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ public async Task<List<Book>> SearchBookAsync(string sourceId, string bookName)
8383
}
8484
else
8585
{
86-
var keyword = HttpUtility.UrlEncode(bookName, encoding);
86+
var keyword = searchConfig.EncodingKeyword
87+
? HttpUtility.UrlEncode(bookName, encoding)
88+
: bookName;
8789
var searchUrl = searchConfig.SearchUrl + keyword;
8890
doc = await GetHtmlDocumentAsync(searchUrl, encoding);
8991
}

src/ViewModels/ViewModels.Desktop/LibraryViewModel/LibraryViewModel.Import.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.IO;
55
using System.Linq;
6+
using System.Reactive.Linq;
67
using System.Text.RegularExpressions;
78
using System.Threading;
89
using System.Threading.Tasks;
@@ -129,8 +130,8 @@ private async Task<Book> GenerateBookEntryFromEpubFileAsync(string path)
129130
var progressDialog = ServiceLocator.Instance.GetService<ICustomDialog>(AppConstants.ProgressDialog);
130131
progressDialog.InjectData(StringResources.MovingFile);
131132
progressDialog.InjectTask(_fileToolkit.CopyAsync(path, destPath));
132-
await progressDialog.ShowAsync();
133133

134+
await progressDialog.ShowAsync();
134135
var book = GetBookEntryFromLocalPath(destPath);
135136

136137
try

src/ViewModels/ViewModels.Desktop/LibraryViewModel/LibraryViewModel.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,26 +257,31 @@ await Task.Run(async () =>
257257
{
258258
book = await LibraryContext.Books.FirstOrDefaultAsync(p => p.Path.Equals(Path.GetFileName(path)));
259259
});
260-
AppViewModel.Instance.RequestRead(book);
260+
261+
DispatcherQueue.TryEnqueue(() =>
262+
{
263+
AppViewModel.Instance.RequestRead(book);
264+
});
261265
}
262266
else
263267
{
264268
if (Path.GetExtension(path).Equals(".epub", StringComparison.OrdinalIgnoreCase))
265269
{
266270
GetBookEntryFromEpubFileCommand.Execute(path)
267-
.Subscribe(async b =>
271+
.ObserveOn(RxApp.MainThreadScheduler)
272+
.Subscribe(b =>
268273
{
269-
await InsertBookEntryAsync(b);
270-
271-
DispatcherQueue.TryEnqueue(() =>
274+
DispatcherQueue.TryEnqueue(async () =>
272275
{
276+
await InsertBookEntryAsync(b);
273277
AppViewModel.Instance.RequestRead(b);
274278
});
275279
});
276280
}
277281
else if (Path.GetExtension(path).Equals(".txt", StringComparison.OrdinalIgnoreCase))
278282
{
279283
GetBookEntryFromTxtFileCommand.Execute(path)
284+
.ObserveOn(RxApp.MainThreadScheduler)
280285
.Subscribe(async b =>
281286
{
282287
await InsertBookEntryAsync(b);

0 commit comments

Comments
 (0)