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