Skip to content

Commit 518b692

Browse files
committed
Bugs fixed.
1 parent 8b65b57 commit 518b692

File tree

9 files changed

+100
-86
lines changed

9 files changed

+100
-86
lines changed

electron/db.sqlite

0 Bytes
Binary file not shown.

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { Book, Category } from '../../models';
1616
import { BookService } from '../services/book.service';
1717
import { CateService } from '../services/cate.service';
1818
import { OpMessageService } from '../services/op-message.service';
19+
import { MessageService } from '../../services/message.service';
1920

2021
import { DeleteBookDialog } from './delete-book-dialog.component';
2122
import { EditBookCateListDialog } from './edit-book-cate-list.component';
@@ -25,6 +26,7 @@ import {
2526
sortBy,
2627
IFilter,
2728
IDeleteBookDialogResData,
29+
IMessage,
2830
IFilterAction,
2931
IFilterItem,
3032
TBookSortBy,
@@ -37,40 +39,45 @@ import {
3739
styleUrls: ['./book-list.component.scss'],
3840
})
3941
export class BookListComponent implements OnInit, OnChanges {
40-
@Input() sortBy: string;
41-
@Input() displayRecycled: boolean;
42-
@Input() beenOpened: boolean;
42+
@Input() sortBy: string = 'openCount';
43+
@Input() displayRecycled: boolean = false;
44+
@Input() beenOpened: boolean = true;
4345

4446
filter: IFilter = {
4547
displayRecycled: this.displayRecycled,
46-
isOpened: this.beenOpened,
48+
beenOpened: this.beenOpened,
4749
filterList: []
4850
};
4951

50-
bookList: Array<Book>;
52+
private _bookList: Array<Book>;
5153

5254
constructor(
5355
private crud: CrudService,
5456
private dialog: MatDialog,
5557
private book: BookService,
58+
private msgService: MessageService,
5659
private opMessage: OpMessageService
57-
) {}
60+
) {
61+
this._bookList = this.book.list;
62+
}
5863

59-
loadBookList = async () => {
60-
const bookList = await this.book.getList(this.filter).slice();
64+
get bookList () {
65+
const bookList = this._bookList.filter(b => filterFn(b, this.filter));
6166
return sortBy(bookList, this.sortBy);
62-
};
67+
}
6368

6469
ngOnInit() {
65-
this.loadBookList().then(list => this.bookList = list.slice());
70+
this.msgService.getMessage().subscribe((msg: IMessage) => {
71+
if(msg.event === 'book-list-updated'){
72+
this._bookList = msg.data as Array<Book>;
73+
}
74+
});
6675
}
6776

6877
ngOnChanges (changes: SimpleChanges) {
6978
if ('sortBy' in changes) {
7079
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());
80+
if(changes.beenOpened) this.filter.beenOpened = changes.beenOpened.currentValue;
7481
}
7582
}
7683

src/app/home/home.page.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<!-- 正在看的书, 打开次数大于 0 的书 -->
1818
<ion-col>
1919
<ion-badge color="success" style="position: absolute; right: 0px; top: 0px; overflow: visible!important;">
20-
{{currentlyReadingNumber}}
20+
{{currentlyReading}}
2121
</ion-badge>
2222
<ion-fab-button
2323
color="light" (click)="displayBookListCurrentlyReading()" matTooltip="正在看的书">
@@ -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-
{{onShelfNumber}}
33+
{{onShelf}}
3434
</ion-badge>
3535
<ion-fab-button color="light" (click)="displayBookListOnShelf()" matTooltip="书架上的书">
3636
<ion-avatar id="avatar-on-shelf">
@@ -42,7 +42,7 @@
4242
<!-- 准备删除的书 -->
4343
<ion-col>
4444
<ion-badge color="warning" style="position: absolute; right: 0px; top: 0px; overflow: visible!important;">
45-
{{recycledNumber}}
45+
{{recycled}}
4646
</ion-badge>
4747

4848
<ion-fab-button color="light" (click)="displayBookListRecycled()" matTooltip="暂存在回收站的书">

src/app/home/home.page.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import { Book } from '../models';
1616
import { CrudService } from '../services/crud.service';
1717
import { BookService } from './services/book.service';
18+
import { MessageService } from '../services/message.service';
1819

1920
import { SnackbarComponent } from './components/snackbar.component';
2021
import { NewBookDialog } from './components/new-book-dialog.component';
@@ -23,7 +24,9 @@ import {
2324
IProgressMessage,
2425
IAddBookDialogResData,
2526
IFilter,
27+
IMessage,
2628
TAvatarIds,
29+
filterFn,
2730
TBookSortBy,
2831
} from '../vendor';
2932

@@ -41,14 +44,17 @@ export class HomePage implements OnInit {
4144
sortBy: TBookSortBy = 'openCount';
4245
displayRecycled: boolean = false;
4346
beenOpened: boolean = true;
44-
47+
private bookList: Array<Book>;
48+
4549
constructor(
4650
private crud: CrudService,
4751
private snackbar: MatSnackBar,
4852
private dialog: MatDialog,
53+
private msgSevice: MessageService,
4954
private book: BookService,
50-
) {}
51-
55+
) {
56+
this.bookList = this.book.list;
57+
}
5258

5359
private changeFabButton = (button: TAvatarIds) => {
5460
const buttonList = ['currently-reading', 'on-shelf', 'recycled'];
@@ -64,32 +70,38 @@ export class HomePage implements OnInit {
6470
});
6571
}
6672

67-
get currentlyReadingNumber () {
68-
const currentlyReadingFilter: IFilter = {
73+
get currentlyReading () {
74+
const filter: IFilter = {
6975
displayRecycled: false,
70-
isOpened: true,
76+
beenOpened: true,
7177
}
72-
return this.book.getList(currentlyReadingFilter).length
78+
return this.bookList.filter(b => filterFn(b, filter)).length;
7379
}
7480

75-
get onShelfNumber () {
76-
const onShelfFilter: IFilter = {
81+
get onShelf () {
82+
const filter = {
7783
displayRecycled: false,
78-
isOpened: true,
84+
beenOpened: false,
7985
}
80-
return this.book.getList(onShelfFilter).length
86+
return this.bookList.filter(b => filterFn(b, filter)).length;
8187
}
8288

83-
get recycledNumber () {
84-
const recycledFilter: IFilter = {
89+
get recycled () {
90+
const filter = {
8591
displayRecycled: true,
8692
}
87-
return this.book.getList(recycledFilter).length;
93+
return this.bookList.filter(b => filterFn(b, filter)).length;
8894
}
8995

9096
ngOnInit() {
97+
this.msgSevice.getMessage().subscribe((msg: IMessage) => {
98+
if(msg.event === 'book-list-updated'){
99+
this.bookList = msg.data as Array<Book>;
100+
}
101+
});
102+
91103
this.crud.ipcRenderer.on('error-occured', (ev, book: Book) => {
92-
this.book.bookUpdated(book);
104+
this.book.listUpdated(book);
93105
});
94106

95107
this.crud.ipcRenderer.on('new-downloading-progress', (ev, msg: IProgressMessage) => {
@@ -115,7 +127,7 @@ export class HomePage implements OnInit {
115127

116128
this.downloadingList.splice(index, 1);
117129

118-
this.book.bookUpdated(msg.book);
130+
this.book.listUpdated(msg.book);
119131

120132
if(this.downloadingList.length === 0){
121133
this.snackbar.dismiss();

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

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { OpMessageService } from './op-message.service';
66
import { WriterService } from './writer.service';
77
import { WebsiteService } from './website.service';
88
import { CateService } from './cate.service';
9+
import { MessageService } from '../../services/message.service';
910

1011
import {
1112
IFilter,
@@ -14,6 +15,7 @@ import {
1415
IQueryResult,
1516
REGEXP_SITE,
1617
REGEXP_LOC,
18+
IMessage,
1719
IAddBookDialogResData,
1820
IDeleteBookDialogResData,
1921
} from '../../vendor';
@@ -22,47 +24,47 @@ import {
2224
providedIn: 'root'
2325
})
2426
export class BookService {
25-
private _filter: IFilter = {
26-
displayRecycled: false,
27-
isOpened: false,
28-
filterList: []
29-
};
27+
list: Array<Book>;
3028

3129
constructor(
3230
private crud: CrudService,
3331
private opMessage: OpMessageService,
3432
private website: WebsiteService,
33+
private msgService: MessageService,
3534
private writer: WriterService,
3635
private cate: CateService
3736
) {
38-
}
39-
40-
getList = async () => {
41-
let list: Array<Book>;
42-
43-
await this.crud.getItems({table: 'Book'})
37+
this.crud.getItems({table: 'Book'})
4438
.subscribe((res: IQueryResult) => {
4539
this.opMessage.newMsg(res.message);
4640
const books = res.data as Book[];
47-
list = books.slice();
41+
this.list = books.slice();
4842
});
43+
}
4944

50-
return list;
45+
filterdList = async (filter: IFilter) => {
46+
return this.list.filter(b => filterFn(b, filter));
5147
}
5248

53-
getFilterdList = async (filter: IFilter) => {
54-
const list = await this.getList();
55-
return list.filter(b => filterFn(b, filter));
49+
listUpdated = (book: Book, kept: boolean = true) => {
50+
const index = this.list.findIndex(b => b.id === book.id);
51+
this.list.splice(index, 1);
52+
if(kept) this.list.push(book);
53+
54+
const msg: IMessage = {
55+
event: 'book-list-updated',
56+
data: this.list
57+
}
5658
}
5759

5860
save = async (res: IAddBookDialogResData) => {
5961
const newBook = new Book();
60-
newBook.cateList = [];
6162

6263
const newBookUri = res.bookUri;
6364

6465
const site = newBookUri.match(REGEXP_SITE)[0];
6566
const [ writerName, name ] = newBookUri.replace(REGEXP_SITE, '').match(REGEXP_LOC)[0].split('/');
67+
6668
const re = new RegExp(/\.git$/)
6769
newBook.name = re.test(name) ? name.replace(re, '') : name;
6870

@@ -74,8 +76,10 @@ export class BookService {
7476
table: 'Book',
7577
item: newBook
7678
}
79+
7780
this.crud.addItem(query).subscribe((res: IQueryResult) => {
7881
this.opMessage.newMsg(res.message);
82+
this.list.push(res.data as Book);
7983
});
8084
}
8185

@@ -91,6 +95,7 @@ export class BookService {
9195

9296
this.crud.updateItem(query).subscribe((queryRes: IQueryResult) => {
9397
this.opMessage.newMsg(queryRes.message);
98+
this.listUpdated(queryRes.data as Book);
9499
});
95100
}
96101

@@ -105,6 +110,7 @@ export class BookService {
105110
}
106111
this.crud.updateItem(query).subscribe((queryRes: IQueryResult) => {
107112
this.opMessage.newMsg(queryRes.message);
113+
this.listUpdated(queryRes.data as Book);
108114
});
109115
}
110116

@@ -119,6 +125,7 @@ export class BookService {
119125

120126
await this.crud.deleteItem(query).subscribe((queryRes: IQueryResult) => {
121127
this.opMessage.newMsg(queryRes.message);
128+
this.listUpdated(queryRes.data as Book, false);
122129
});
123130

124131
return 0;
@@ -139,6 +146,7 @@ export class BookService {
139146

140147
this.crud.updateItem(query).subscribe((queryRes: IQueryResult) => {
141148
this.opMessage.newMsg(queryRes.message);
149+
this.listUpdated(queryRes.data as Book);
142150
});
143151
}
144152
}

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,26 @@ import {
1313
providedIn: 'root'
1414
})
1515
export class CateService {
16+
list: Array<Category>;
17+
1618
constructor(
1719
private crud: CrudService,
1820
private opMessage: OpMessageService,
1921
) {
20-
}
21-
22-
getList = async () => {
23-
let list: Array<Category>;
24-
25-
await this.crud.getItems({table: 'Category'})
22+
this.crud.getItems({table: 'Category'})
2623
.subscribe((res: IQueryResult) => {
2724
this.opMessage.newMsg(res.message);
2825
const cates = res.data as Category[];
29-
list = cates.slice();
26+
this.list = cates.slice();
3027
});
31-
32-
return list;
3328
}
3429

3530
saveList = async (cateList: Array<Category>) => {
3631
const tempList: Array<Category> = [];
37-
const list = await this.getList();
3832

3933
await cateList.map(c => {
40-
const cate = list.find(cate => cate.name === c.name);
34+
const cate = this.list.find(cate => cate.name === c.name);
35+
4136
if (cate) tempList.push(cate);
4237
else {
4338
const _cate = new Category();

0 commit comments

Comments
 (0)