• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import {WidthWeightMessage, unSelectedNoteBgColorMap, selectedNoteBgColorMap, folderTextMap, widthWeightMessageMap
17} from '../model/NoteBaseData'
18import FolderData from '../model/databaseModel/FolderData'
19import NoteData from '../model/databaseModel/NoteData'
20import {SysDefFolderUuid, FolderType, Favorite, Delete, TableName, NoteTableColumn,
21} from '../model/databaseModel/EnumData'
22import GlobalResourceManager from './GlobalResourceManager'
23import StyleConstants from '../constants/StyleConstants'
24import {LogUtil} from './LogUtil'
25import FolderUtil from './FolderUtil'
26import RdbStoreUtil from './RdbStoreUtil'
27import prompt from '@system.prompt'
28
29const TAG = "NoteUtil"
30
31export class NoteUtil {
32  /**
33  * Set Note Checked status
34  *
35  * @param checkedNoteArray - Checked status Note list
36  * @param noteItem - note
37  */
38  setNoteChecked(checkedNoteArray: NoteData[], noteItem: NoteData) {
39    LogUtil.info(TAG, "setNoteChecked")
40    checkedNoteArray.push(noteItem)
41    AppStorage.Set('CheckedNoteArray',checkedNoteArray)
42  }
43
44  /**
45  * unset Note with Checked status
46  *
47  * @param checkedNoteArray - Checked status Note list
48  * @param noteItem - note
49  */
50  unsetNoteChecked(checkedNoteArray: NoteData[], noteItem: NoteData) {
51    let index = checkedNoteArray.findIndex((note) => {
52      return note == noteItem
53    })
54    LogUtil.info(TAG, "index " + index.toString() + noteItem.uuid)
55    index == -1 ? null : checkedNoteArray.splice(index, 1)
56    AppStorage.Set('CheckedNoteArray',checkedNoteArray)
57  }
58
59  /**
60  * Is Note Checked status
61  *
62  * @param checkedNoteArray - Checked status Note list
63  * @param noteItem - note
64  * @return boolean - Checked: true, unchecked: false
65  */
66  isNoteChecked(checkedNoteArray: NoteData[], noteItem: NoteData): boolean {
67    for (let i = 0; i < checkedNoteArray.length; i++) {
68      LogUtil.info(TAG, "index " + checkedNoteArray[i].uuid + " noteItem uuid: " + noteItem.uuid)
69    }
70    let index = checkedNoteArray.findIndex((note) => {
71      return note.uuid == noteItem.uuid
72    })
73    LogUtil.info(TAG, "index " + index.toString() + " noteItem uuid: " + noteItem.uuid)
74    return index != -1
75  }
76
77  /**
78  * Set all notes Checked status
79  *
80  * @param checkedNoteArray - Checked status Note list
81  * @param selectedNoteArray - note array
82  */
83  setAllNotesChecked(checkedNoteArray: NoteData[], noteArray: NoteData[]) {
84    LogUtil.info(TAG, "setAllNotesChecked")
85    this.unsetAllNotesChecked(checkedNoteArray)
86    checkedNoteArray.push(...noteArray)
87    AppStorage.Set('CheckedNoteArray', checkedNoteArray)
88  }
89
90  /**
91  * unset all Notes with Checked status
92  *
93  * @param CheckedNoteArray - Checked status Note list
94  */
95  unsetAllNotesChecked(checkedNoteArray: NoteData[]) {
96    LogUtil.info(TAG, "unsetAllNotesChecked")
97    checkedNoteArray.splice(0, checkedNoteArray.length)
98    AppStorage.Set('CheckedNoteArray', checkedNoteArray)
99  }
100
101  /**
102  * Get Note Count in folder
103  *
104  * @param noteDataArray
105  * @param folderData
106  * @return number - Note Count
107  */
108  getNoteCount(noteDataArray: NoteData[], folderData: FolderData): number {
109    let count = 0;
110    const folderTextMap = {
111      "sys_def_allNotes_uuid": (noteData) => {
112          noteData.is_deleted == Delete.No ? ++count : count
113      },
114      "sys_def_recentDeletes_uuid": (noteData) => {
115          noteData.is_deleted == Delete.Yes ? ++count : count
116      },
117      "sys_def_myFavorites_uuid": (noteData) => {
118          noteData.is_favorite == Favorite.Yes && noteData.is_deleted == Delete.No ? ++count : count
119      },
120      "sys_def_other_uuid": (noteData) => {
121          noteData.folder_uuid == folderData.uuid && noteData.is_deleted == Delete.No ? ++count : count
122      }
123    }
124    if (folderData.uuid == SysDefFolderUuid.AllNotes || folderData.folder_type == FolderType.FeatureDef) {
125      noteDataArray.forEach(folderTextMap[folderData.uuid])
126    } else {
127      noteDataArray.forEach(folderTextMap["sys_def_other_uuid"])
128    }
129    LogUtil.info(TAG, "folderUuid " + folderData.uuid + " count " + count.toString())
130    return count;
131  }
132
133  /**
134  * Get note background color
135  *
136  * @param folderDataArray - Folder data list
137  * @param uuid - note's folder_uuid
138  * @param selectFolderUuid - selected folder's uuid
139  * @param isSelected - is selected or not
140  * @return Resource | string - color value
141  */
142  getNoteBgColor(folderDataArray: FolderData[], uuid: string, selectFolderUuid: string, isSelected: boolean): Resource | string {
143    let folderData: FolderData = FolderUtil.getFolderData(folderDataArray, uuid)
144    if (folderData == undefined || (selectFolderUuid != SysDefFolderUuid.AllNotes && !isSelected)) {
145      LogUtil.info(TAG, "NoteBgColor is ffffff")
146      return $r("app.color.color_ffffff")
147    }
148    if (folderData.color == "#ffffff") {
149      return "#FFEBE1"
150    }
151    if (folderData.color == "#e84026") {
152      return "#ffe0b2ac"
153    }
154    LogUtil.info(TAG, "isSelected " + isSelected.toString() + ", folderData.color: " + folderData.color.toString())
155    return isSelected ? selectedNoteBgColorMap[folderData.color] : unSelectedNoteBgColorMap[folderData.color]
156  }
157
158  /**
159  * Get verticalBar background color
160  *
161  * @param folderDataArray - Folder data list
162  * @param uuid - note's folder_uuid
163  * @return Resource | string - color value
164  */
165  getVerticalBarBgColor(folderDataArray: FolderData[], uuid: string): Resource | string {
166    let folderData: FolderData = FolderUtil.getFolderData(folderDataArray, uuid)
167    if (folderData == undefined) {
168      LogUtil.info(TAG, "NoteBgColor is ffffff")
169      return $r("app.color.color_ffffff")
170    }
171    LogUtil.info(TAG, "isSelected " + folderData.color.toString() + ", folderData.color: " + folderData.color.toString())
172    if (folderData.color == "#ffffff") {
173      return "#ff9516"
174    }
175    return folderData.color
176  }
177
178  /**
179  * Automatically delete notes in the deleted folder more than 30 days
180  *
181  * @param allNoteDataArray
182  */
183  autoDeleteNote(allNoteDataArray: NoteData[]) {
184    for (let index = 0; index < allNoteDataArray.length; ) {
185      if (allNoteDataArray[index].is_deleted == Delete.Yes && new Date().getTime() - allNoteDataArray[index].deleted_time > StyleConstants.DELETE_DEADLINE) {
186        LogUtil.info(TAG, "Delete Note, uuid " + allNoteDataArray[index].uuid + " delete time " + allNoteDataArray[index].deleted_time)
187        // delete note from db
188        let predicates_note = RdbStoreUtil.getRdbPredicates(TableName.NoteTable)
189        predicates_note.equalTo(NoteTableColumn.Uuid, allNoteDataArray[index].uuid)
190        RdbStoreUtil.delete(predicates_note, null)
191        allNoteDataArray.splice(index, 1);
192      }
193      index++
194    }
195  }
196
197  /**
198  * Get Note Count
199  *
200  * @param allNoteDataArray
201  * @param folderUuid
202  * @return NoteData[] - return Note in folder
203  */
204  getNoteDataArray(allNoteDataArray: NoteData[], folderUuid: string): NoteData[] {
205    LogUtil.info(TAG, "folderUuid " + folderUuid)
206    this.autoDeleteNote(allNoteDataArray)
207    let noteDataArray: NoteData[] = [];
208    let func = (noteData) => noteDataArray.push(noteData)
209    const folderTextMap = {
210      "sys_def_allNotes_uuid": (noteData) => {
211          noteData.is_deleted == Delete.No ? func(noteData) : null
212      },
213      "sys_def_recentDeletes_uuid": (noteData) => {
214          noteData.is_deleted == Delete.Yes ? func(noteData) : null
215      },
216      "sys_def_myFavorites_uuid": (noteData) => {
217          noteData.is_favorite == Favorite.Yes && noteData.is_deleted == Delete.No ? func(noteData) : null
218      },
219      "sys_def_other_uuid": (noteData) => {
220          noteData.folder_uuid == folderUuid && noteData.is_deleted == Delete.No ? func(noteData) : null
221      },
222    }
223    if (folderUuid == SysDefFolderUuid.AllNotes || folderUuid == SysDefFolderUuid.RecentDeletes || folderUuid == SysDefFolderUuid.MyFavorites) {
224      allNoteDataArray.forEach(folderTextMap[folderUuid])
225    } else {
226      allNoteDataArray.forEach(folderTextMap["sys_def_other_uuid"])
227    }
228    // Sort priority: 1、Top or not 2、created time
229    noteDataArray.sort((noteItemLeft: NoteData, noteItemRight: NoteData) => {
230      if (noteItemRight.is_top != noteItemLeft.is_top) {
231        return noteItemRight.is_top - noteItemLeft.is_top
232      }
233      return noteItemRight.modified_time - noteItemLeft.modified_time
234    })
235    LogUtil.info(TAG, "noteDataArray size " + noteDataArray.length.toString())
236    return noteDataArray
237  }
238
239  /**
240  * Get First Note in note array
241  *
242  * @param allNoteDataArray
243  * @param folderUuid
244  * @return NoteData
245  */
246  getFirstNoteData(allNoteDataArray: NoteData[], folderUuid: string): NoteData {
247    let noteDataArray: NoteData[] = this.getNoteDataArray(allNoteDataArray, folderUuid);
248    LogUtil.info(TAG, "get noteDataArray size " + noteDataArray.length.toString())
249    if (noteDataArray.length == 0) {
250      LogUtil.info(TAG, "get noteDataArray empty")
251      return undefined;
252    }
253    return noteDataArray == undefined ? null : noteDataArray[0]
254  }
255
256  /**
257  * Remove NoteData
258  *
259  * @param allNoteDataArray
260  * @param noteUuid
261  */
262  removeNoteData(allNoteDataArray: NoteData[], noteUuid: string) {
263    let index = allNoteDataArray.findIndex((note) => {
264      return note.uuid == noteUuid
265    })
266    LogUtil.info(TAG, "index " + index.toString() + " uuid " + noteUuid)
267    index == -1 ? null : allNoteDataArray.splice(index, 1)
268  }
269
270  deleteEmptyNote(selectedNoteData: NoteData, AllNoteArray: NoteData[], controllerShow: WebController): boolean {
271    if (selectedNoteData.title == "标题" && selectedNoteData.content_text == "") {
272      // delete note from db
273      noteUtil.removeNoteData(AllNoteArray, selectedNoteData.uuid)
274      LogUtil.info(TAG, "delete note:" + selectedNoteData.uuid)
275      let predicates_note = RdbStoreUtil.getRdbPredicates(TableName.NoteTable)
276      predicates_note.equalTo(NoteTableColumn.Uuid, selectedNoteData.uuid)
277      RdbStoreUtil.delete(predicates_note, null)
278      LogUtil.info(TAG, "delete note success:" + selectedNoteData.uuid)
279      return true
280    }
281    return false
282  }
283
284  refreshAll() {
285    let isRefresh = AppStorage.Get('isUpdate')
286    AppStorage.Set('isUpdate',!isRefresh)
287  }
288}
289
290let noteUtil = new NoteUtil()
291
292export default noteUtil as NoteUtil