1/*
2 * Copyright (c) 2021-2023 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 window from '@ohos.window';
17import prompt from '@ohos.prompt';
18import { getResourceString } from './Tools';
19import { MILLISECOND, FILENAME_REGEXP } from '../constants/Constant';
20import Logger from '../log/Logger';
21
22const TAG = 'commonUtil';
23
24export const toast = (text: string | Resource, time = MILLISECOND.ONE_SECOND) => {
25  let resourceString: string | undefined = undefined;
26  if (typeof text !== 'string') {
27    resourceString = getResourceString(text);
28  } else {
29    resourceString = text;
30  }
31  prompt.showToast({ message: resourceString, duration: time });
32}
33
34/**
35 * 设置导航栏、状态栏背景、文字颜色
36 *
37 * @param navigationBarColor 导航栏背景颜色
38 * @param statusBarColor 状态栏背景颜色
39 * @param navigationBarContentColor 导航栏文字颜色
40 * @param statusBarContentColor 状态栏文字颜色
41 */
42export const setSystemBar = async (navigationBarColor?: string, statusBarColor?: string,
43                                   navigationBarContentColor?: string, statusBarContentColor?: string) => {
44  let w = await window.getTopWindow(getContext(this));
45  await w.setSystemBarProperties({
46    navigationBarColor,
47    statusBarColor,
48    navigationBarContentColor,
49    statusBarContentColor
50  });
51}
52
53/**
54 * 设置沉浸式
55 *
56 * @param isImmersion 是否沉浸式
57 */
58export const setImmersion = (isImmersion: boolean) => {
59  let TAG = 'setImmersion';
60  if (!globalThis.windowClass) {
61    return;
62  }
63  globalThis.windowClass.setFullScreen(isImmersion, (err, data) => {
64    if (err.code) {
65      Logger.e(TAG, 'Failed to enable the full-screen mode. Cause:' + JSON.stringify(err));
66      return;
67    }
68    Logger.i(TAG, 'Succeeded in enabling the full-screen mode. Data: ' + JSON.stringify(data));
69  })
70}
71
72/**
73 * 校验文件名合法性
74 *
75 * @param fileName 文件名
76 * @return 是否合法
77 */
78export function isValidFileName(fileName): boolean {
79  return FILENAME_REGEXP.test(fileName);
80}
81
82