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