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 Log from '../../../../../../../../../common/src/main/ets/default/Log'; 17import ViewModel from '../../vm/accountsViewModel'; 18import Constants from '../../common/constants'; 19import {UserData} from '../../data/userData'; 20import { ACCOUNTS_REFRESH_EVENT } from '../../model/accountsModel' 21import EventManager, { unsubscribe } from '../../../../../../../../../common/src/main/ets/default/event/EventManager'; 22 23const TAG = 'ScreenLock-Accounts' 24 25@Component 26export default struct Accounts { 27 @State accountList: UserData[] = []; 28 private mViewModel: ViewModel = new ViewModel(); 29 @StorageLink('screenlockdirection') screenlockdirection: number = 1 30 unSubscriber?: unsubscribe; 31 32 aboutToAppear() { 33 Log.showInfo(TAG, `aboutToAppear`); 34 this.unSubscriber = EventManager.subscribe(ACCOUNTS_REFRESH_EVENT, (data) => { 35 this.accountList = data; 36 }); 37 } 38 39 aboutToDisappear() { 40 Log.showInfo(TAG, `aboutToDisappear`); 41 this.unSubscriber && this.unSubscriber(); 42 this.unSubscriber = undefined; 43 } 44 45 build() { 46 Row() { 47 if (this.accountList.length > 1) { 48 List({ space: this.screenlockdirection == 1 ? Constants.ACCOUNT_SPACE : Constants.ACCOUNT_SPACE_PORTRAIT }) { 49 ForEach(this.accountList, (item: any) => { 50 ListItem() { 51 UserItem({ userItem: item, viewModel: this.mViewModel }) 52 } 53 }) 54 } 55 .listDirection(Axis.Horizontal) 56 } 57 } 58 } 59} 60 61@Component 62struct UserItem { 63 @State userItem: UserData = null; 64 private viewModel: ViewModel; 65 66 build() { 67 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) { 68 if (this.userItem.userIconPath.length == 0) { 69 Image($r('app.media.ic_user_portrait')) 70 .width(Constants.SHORTCUT_CIRCLE_WIDTH) 71 .height(Constants.SHORTCUT_CIRCLE_WIDTH) 72 } else { 73 Image(this.userItem.userIconPath) 74 .width(Constants.SHORTCUT_CIRCLE_WIDTH) 75 .height(Constants.SHORTCUT_CIRCLE_WIDTH) 76 } 77 Text(this.userItem.userName) 78 .fontSize(Constants.SHORTCUT_TEXT_SIZE) 79 .fontColor($r('app.color.shortcut_text_color')) 80 .margin({ top: $r("app.float.accounts_block") }) 81 } 82 .onClick(event => { 83 this.viewModel.onUserClick(this.userItem.userId); 84 }) 85 } 86}