Skip to content

Commit 0621c9c

Browse files
committed
Prepare to fix more bugs.
1 parent 917c5df commit 0621c9c

15 files changed

+225
-204
lines changed

electron/db.sqlite

0 Bytes
Binary file not shown.

src/app/home/components/book-list.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<button mat-button *ngIf="book.downloaded" (click)="openReadmeDialog(book.desc)">
3232
<ion-icon name="compass-outline"></ion-icon>README.md
3333
</button>
34-
<button mat-button *ngIf="book.downloaded" (click)="book.open(book)">
34+
<button mat-button *ngIf="book.downloaded" (click)="openBook(book)">
3535
<ion-icon name="book-outline"></ion-icon>阅读
3636
</button>
3737
<button id="{{ 'download-book-' + book.id }}" mat-button

src/app/home/components/book-list.component.ts

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {
22
Component,
33
OnInit,
4+
OnChanges,
5+
SimpleChanges,
46
Input
57
} from '@angular/core';
68

@@ -22,22 +24,30 @@ import { ReadmeDialog } from './readme-dialog.component';
2224
import {
2325
sortBy,
2426
IFilter,
25-
} from '../../vendor';
26-
27-
import {
2827
IDeleteBookDialogResData,
28+
IFilterAction,
29+
IFilterItem,
2930
TBookSortBy,
3031
filterFn,
31-
} from '../vendor';
32+
} from '../../vendor';
3233

3334
@Component({
3435
selector: 'app-book-list',
3536
templateUrl: './book-list.component.html',
3637
styleUrls: ['./book-list.component.scss'],
3738
})
38-
export class BookListComponent implements OnInit {
39-
@Input() filter: IFilter;
40-
@Input() sortBy: TBookSortBy;
39+
export class BookListComponent implements OnInit, OnChanges {
40+
@Input() sortBy: string;
41+
@Input() displayRecycled: boolean;
42+
@Input() beenOpened: boolean;
43+
44+
filter: IFilter = {
45+
displayRecycled: this.displayRecycled,
46+
isOpened: this.beenOpened,
47+
filterList: []
48+
};
49+
50+
bookList: Array<Book>;
4151

4252
constructor(
4353
private crud: CrudService,
@@ -46,14 +56,47 @@ export class BookListComponent implements OnInit {
4656
private opMessage: OpMessageService
4757
) {}
4858

49-
ngOnInit() {}
50-
51-
get bookList () {
52-
return sortBy(this.book.list
53-
.filter(b => filterFn(b, this.filter)),
54-
this.sortBy);
59+
loadBookList = async () => {
60+
const bookList = await this.book.getList(this.filter).slice();
61+
return sortBy(bookList, this.sortBy);
62+
};
63+
64+
ngOnInit() {
65+
this.loadBookList().then(list => this.bookList = list.slice());
5566
}
5667

68+
ngOnChanges (changes: SimpleChanges) {
69+
if ('sortBy' in changes) {
70+
if(changes.displayRecycled) this.filter.displayRecycled = changes.displayRecycled.currentValue;
71+
if(changes.beenOpened) this.filter.isOpened = changes.beenOpened.currentValue;
72+
73+
this.loadBookList().then(list => this.bookList = list.slice());
74+
}
75+
}
76+
77+
changeFilter = (filterAction: IFilterAction) => {
78+
let index: number;
79+
switch (filterAction.action){
80+
case 'add':
81+
index = this.filter.filterList.findIndex((filterItem: IFilterItem) => {
82+
const key = Object.keys(filterItem)[0];
83+
const _key = Object.keys(filterAction.filterItem)[0];
84+
return key === _key && filterItem[key].id === filterAction[key].id;
85+
});
86+
87+
if(index < 0)this.filter.filterList.push(filterAction.filterItem);
88+
break;
89+
case 'remove':
90+
index = this.filter.filterList.findIndex((filterItem: IFilterItem) => {
91+
const key = Object.keys(filterItem)[0];
92+
const _key = Object.keys(filterAction.filterItem)[0];
93+
return key === _key && filterItem[key].id === filterAction[key].id;
94+
});
95+
96+
if(index >= 0)this.filter.filterList.splice(index, 1);
97+
break;
98+
}
99+
}
57100
startDownload = (book: Book) => {
58101
this.crud.ipcRenderer.send('download-book', book);
59102
document
@@ -75,6 +118,10 @@ export class BookListComponent implements OnInit {
75118
});
76119
}
77120

121+
openBook = (book: Book) => {
122+
this.book.open(book);
123+
}
124+
78125
openEditBookCateListDialog = (book: Book) => {
79126
const dialogRef = this.dialog.open(EditBookCateListDialog, {
80127
width: '480px',

src/app/home/components/delete-book-dialog.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212

1313
import {
1414
IDeleteBookDialogResData,
15-
} from '../vendor';
15+
} from '../../vendor';
1616

1717
import { Book } from '../../models';
1818

@@ -43,6 +43,7 @@ export class DeleteBookDialog implements OnInit{
4343
remove: false,
4444
book: this.data
4545
}
46+
4647
ngOnInit() {}
4748

4849
onNoClick(): void {

src/app/home/components/new-book-dialog.component.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,12 @@ import {
1616
import { MatAutocompleteSelectedEvent, MatAutocomplete} from '@angular/material/autocomplete';
1717
import { MatChipInputEvent } from '@angular/material/chips';
1818

19-
import {
20-
Category,
21-
} from '../../models';
22-
19+
import { Category } from '../../models';
2320
import {
2421
IsQualifiedAndNotExistedGitRepoValidatorFn,
2522
REGEXP_ZH,
26-
} from '../../vendor';
27-
28-
import {
2923
IAddBookDialogResData,
30-
} from '../vendor';
24+
} from '../../vendor';
3125

3226
import { BookService } from '../services/book.service';
3327
import { CateService } from '../services/cate.service';

src/app/home/home.page.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
<!-- 正在看的书, 打开次数大于 0 的书 -->
1818
<ion-col>
1919
<ion-badge color="success" style="position: absolute; right: 0px; top: 0px; overflow: visible!important;">
20-
{{bookListCurrentlyReading.length}}
20+
{{currentlyReadingNumber}}
2121
</ion-badge>
2222
<ion-fab-button
23-
color="light" (click)="displayCurrentlyReadingBookList()" matTooltip="正在看的书">
23+
color="light" (click)="displayBookListCurrentlyReading()" matTooltip="正在看的书">
2424
<ion-avatar id="avatar-currently-reading">
2525
<img src="assets/images/currently-reading-books.png" />
2626
</ion-avatar>
@@ -30,7 +30,7 @@
3030
<!-- 书架上的书, 打开 0 次的书 -->
3131
<ion-col>
3232
<ion-badge color="success" style="position: absolute; right: 0px; top: 0px; overflow: visible!important;">
33-
{{bookListOnShelf.length}}
33+
{{onShelfNumber}}
3434
</ion-badge>
3535
<ion-fab-button color="light" (click)="displayBookListOnShelf()" matTooltip="书架上的书">
3636
<ion-avatar id="avatar-on-shelf">
@@ -42,10 +42,10 @@
4242
<!-- 准备删除的书 -->
4343
<ion-col>
4444
<ion-badge color="warning" style="position: absolute; right: 0px; top: 0px; overflow: visible!important;">
45-
{{bookListRecycled.length}}
45+
{{recycledNumber}}
4646
</ion-badge>
4747

48-
<ion-fab-button color="light" (click)="displayRecycledBookList()" matTooltip="暂存在回收站的书">
48+
<ion-fab-button color="light" (click)="displayBookListRecycled()" matTooltip="暂存在回收站的书">
4949
<ion-avatar id="avatar-recycled">
5050
<img src="assets/images/trash_full_512x512.png" />
5151
</ion-avatar>
@@ -58,6 +58,6 @@
5858
</ion-header>
5959
<ion-content padding>
6060
<section class="book-list">
61-
<app-book-list [bookListDisplay]="bookListDisplay" [cateList]="cateList"></app-book-list>
61+
<app-book-list [sortBy]="sortBy" [beenOpened]="beenOpened" [displayRecycled]="displayRecycled"></app-book-list>
6262
</section>
6363
</ion-content>

src/app/home/home.page.ts

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Component,
2-
ChangeDetectorRef,
32
OnInit } from '@angular/core';
43

54
import {
@@ -21,16 +20,13 @@ import { SnackbarComponent } from './components/snackbar.component';
2120
import { NewBookDialog } from './components/new-book-dialog.component';
2221

2322
import {
24-
IFilterItem,
25-
IFilter,
2623
IProgressMessage,
27-
IFilterAction,
24+
IAddBookDialogResData,
25+
IFilter,
26+
TAvatarIds,
27+
TBookSortBy,
2828
} from '../vendor';
2929

30-
import { IAddBookDialogResData } from './vendor';
31-
32-
import { TAvatarIds } from './vendor';
33-
3430
@Component({
3531
selector: 'app-home',
3632
templateUrl: './home.page.html',
@@ -42,23 +38,18 @@ export class HomePage implements OnInit {
4238

4339
downloadingList: Array<number> = [];
4440

45-
private _filter: IFilter = {
46-
displayRecycled: false,
47-
isOpened: false,
48-
filterList: []
49-
};
50-
51-
get filter () {
52-
return this._filter;
53-
}
54-
41+
sortBy: TBookSortBy = 'openCount';
42+
displayRecycled: boolean = false;
43+
beenOpened: boolean = true;
44+
5545
constructor(
5646
private crud: CrudService,
5747
private snackbar: MatSnackBar,
5848
private dialog: MatDialog,
5949
private book: BookService,
6050
) {}
6151

52+
6253
private changeFabButton = (button: TAvatarIds) => {
6354
const buttonList = ['currently-reading', 'on-shelf', 'recycled'];
6455
document.getElementById(`avatar-${button}`)
@@ -73,6 +64,29 @@ export class HomePage implements OnInit {
7364
});
7465
}
7566

67+
get currentlyReadingNumber () {
68+
const currentlyReadingFilter: IFilter = {
69+
displayRecycled: false,
70+
isOpened: true,
71+
}
72+
return this.book.getList(currentlyReadingFilter).length
73+
}
74+
75+
get onShelfNumber () {
76+
const onShelfFilter: IFilter = {
77+
displayRecycled: false,
78+
isOpened: true,
79+
}
80+
return this.book.getList(onShelfFilter).length
81+
}
82+
83+
get recycledNumber () {
84+
const recycledFilter: IFilter = {
85+
displayRecycled: true,
86+
}
87+
return this.book.getList(recycledFilter).length;
88+
}
89+
7690
ngOnInit() {
7791
this.crud.ipcRenderer.on('error-occured', (ev, book: Book) => {
7892
this.book.bookUpdated(book);
@@ -109,29 +123,30 @@ export class HomePage implements OnInit {
109123
});
110124
}
111125

112-
changeFilter = (filterAction: IFilterAction) => {
113-
let index: number;
114-
switch (filterAction.action){
115-
case 'add':
116-
index = this.filter.filterList.findIndex((filterItem: IFilterItem) => {
117-
const key = Object.keys(filterItem)[0];
118-
const _key = Object.keys(filterAction.filterItem)[0];
119-
return key === _key && filterItem[key].id === filterAction[key].id;
120-
});
126+
displayBookListCurrentlyReading = () => {
127+
this.displayRecycled = false;
128+
this.beenOpened = true;
121129

122-
if(index < 0)this.filter.filterList.push(filterAction.filterItem);
123-
break;
124-
case 'remove':
125-
index = this.filter.filterList.findIndex((filterItem: IFilterItem) => {
126-
const key = Object.keys(filterItem)[0];
127-
const _key = Object.keys(filterAction.filterItem)[0];
128-
return key === _key && filterItem[key].id === filterAction[key].id;
129-
});
130+
this.sortBy = 'openCount';
130131

131-
if(index >= 0)this.filter.filterList.splice(index, 1);
132-
break;
133-
}
132+
this.changeFabButton('currently-reading');
133+
}
134+
135+
displayBookListOnShelf = () => {
136+
this.displayRecycled = false;
137+
this.beenOpened = false;
138+
139+
this.sortBy = 'dateCreated';
140+
141+
this.changeFabButton('on-shelf');
142+
}
143+
144+
displayBookListRecycled = () => {
145+
this.displayRecycled = true;
146+
147+
this.sortBy = 'dateUpdated';
134148

149+
this.changeFabButton('recycled');
135150
}
136151

137152
openAddBookDialog = () => {

src/app/home/services/book.service.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,28 @@ import { WebsiteService } from './website.service';
88
import { CateService } from './cate.service';
99

1010
import {
11+
IFilter,
12+
filterFn,
1113
IQuery,
14+
IQueryResult,
1215
REGEXP_SITE,
1316
REGEXP_LOC,
14-
IQueryResult,
15-
IFilterItem,
16-
IFilter,
17-
IProgressMessage,
18-
IFilterAction,
19-
IFind,
20-
IMessage
21-
} from '../../vendor';
22-
23-
import { IAddBookDialogResData } from '../vendor';
24-
25-
import {
17+
IAddBookDialogResData,
2618
IDeleteBookDialogResData,
27-
} from '../vendor';
19+
} from '../../vendor';
2820

2921
@Injectable({
3022
providedIn: 'root'
3123
})
3224
export class BookService {
3325
private _list: Array<Book>;
3426

27+
private _filter: IFilter = {
28+
displayRecycled: false,
29+
isOpened: false,
30+
filterList: []
31+
};
32+
3533
constructor(
3634
private crud: CrudService,
3735
private opMessage: OpMessageService,
@@ -57,6 +55,10 @@ export class BookService {
5755
this._list.push(book);
5856
}
5957

58+
getList = (filter: IFilter) => {
59+
return this.list.filter(b => filterFn(b, filter));
60+
}
61+
6062
bookDeleted = (book: Book) => {
6163
const index = this._list.findIndex(b => b.id === book.id);
6264
this._list.splice(index, 1);

0 commit comments

Comments
 (0)