|
|
@@ -25,7 +25,6 @@ export default class RemindersHandler {
|
|
|
* Returns the list of reminders added so far.
|
|
|
*/
|
|
|
public get reminders(): Reminder[] {
|
|
|
- // 返回 _reminders 數組的副本
|
|
|
return [...this._reminders];
|
|
|
}
|
|
|
|
|
|
@@ -37,7 +36,6 @@ export default class RemindersHandler {
|
|
|
public addReminder(description: string, tag: string): void {
|
|
|
const newReminder = new Reminder(description, tag);
|
|
|
|
|
|
- // 將新的提醒添加到陣列中
|
|
|
this._reminders.push(newReminder);
|
|
|
}
|
|
|
|
|
|
@@ -79,19 +77,14 @@ export default class RemindersHandler {
|
|
|
* @param tag - The keyword used to help categorize reminder
|
|
|
*/
|
|
|
public modifyReminder(index: number, description: string): void {
|
|
|
- // 檢查索引是否有效
|
|
|
if (!this.isIndexValid(index)) {
|
|
|
- return; // 如果索引無效,直接返回
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- // 獲取特定索引處的提醒
|
|
|
const reminder = this._reminders[index];
|
|
|
|
|
|
- // 更新提醒的描述
|
|
|
reminder.description = description;
|
|
|
|
|
|
- // 注意:不需要顯式地將更新後的提醒存回數組,
|
|
|
- // 因為 JavaScript 中對象是通過引用傳遞的
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -100,19 +93,14 @@ export default class RemindersHandler {
|
|
|
* @param index - The index of the reminder
|
|
|
*/
|
|
|
public toggleCompletion(index: number): void {
|
|
|
- // 檢查索引是否有效
|
|
|
if (!this.isIndexValid(index)) {
|
|
|
- return; // 如果索引無效,直接返回
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- // 獲取特定索引處的提醒
|
|
|
const reminder = this._reminders[index];
|
|
|
|
|
|
- // 切換提醒的完成狀態
|
|
|
reminder.toggleCompletion();
|
|
|
|
|
|
- // 同樣地,由於 JavaScript 中對象是通過引用傳遞的,
|
|
|
- // 更改將會直接反映在 `_reminders` 數組中的對應對象上
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -125,10 +113,8 @@ export default class RemindersHandler {
|
|
|
public search(keyword: string): Reminder[] {
|
|
|
const keywordLower = keyword.toLowerCase();
|
|
|
|
|
|
- // 首先根據標籤搜索
|
|
|
let results = this.searchTags(keywordLower);
|
|
|
|
|
|
- // 如果沒有找到匹配的標籤,則根據描述搜索
|
|
|
if (results.length === 0) {
|
|
|
results = this.searchDescriptions(keywordLower);
|
|
|
}
|
|
|
@@ -158,17 +144,13 @@ export default class RemindersHandler {
|
|
|
|
|
|
*/
|
|
|
|
|
|
- // 遍歷所有提醒
|
|
|
for (const reminder of this._reminders) {
|
|
|
- // 獲取標籤,並將其轉換為小寫以實現不區分大小寫
|
|
|
const tag = reminder.tag.toLowerCase();
|
|
|
|
|
|
- // 如果該標籤還未在分組中則初始化
|
|
|
if (!groupings[tag]) {
|
|
|
groupings[tag] = [];
|
|
|
}
|
|
|
|
|
|
- // 將提醒添加到相應標籤的分組中
|
|
|
groupings[tag].push(reminder);
|
|
|
}
|
|
|
|
|
|
@@ -180,8 +162,6 @@ export default class RemindersHandler {
|
|
|
* @param keyword - Text to search for in description and tag
|
|
|
*/
|
|
|
private searchTags(keyword: string): Reminder[] {
|
|
|
-
|
|
|
- // 篩選出標籤匹配的提醒
|
|
|
return this._reminders.filter((reminder) => reminder.tag.toLowerCase() === keyword);
|
|
|
}
|
|
|
|
|
|
@@ -190,7 +170,6 @@ export default class RemindersHandler {
|
|
|
* @param keyword - Text to search for in description and tag
|
|
|
*/
|
|
|
private searchDescriptions(keyword: string): Reminder[] {
|
|
|
- // 篩選出描述中包含關鍵字的提醒
|
|
|
return this._reminders.filter((reminder) => reminder.description.toLowerCase().includes(keyword));
|
|
|
}
|
|
|
}
|