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