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