效果演示
代码结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
├──entry/src/main/ets // 代码区 │ ├──common │ │ ├──constants │ │ │ └──CommonConstants.ets // 常量类 │ │ └──utils │ │ ├──GlobalContext.ets // 全局上下文工具类 │ │ └──Logger.ets // 日志打印工具类 │ ├──entryability │ │ └──EntryAbility.ets // 程序入口类 │ ├──pages │ │ ├──AdvertisingPage.ets // 广告页 │ │ ├──AppHomePage.ets // 应用首页 │ │ ├──LauncherPage.ets // 应用启动页 │ │ └──PrivacyPage.ets // 隐私协议页 │ └──view │ └──CustomDialogComponent.ets // 自定义弹窗组件 └──entry/src/main/resources // 资源文件目录 |
代码
attributemodifier
AdvertisingModifier.ets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
import CommonConstants from '../common/constants/CommonConstants' export enum AdvertisingAttrType { STACK, BACKGROUND, TITLE, LOGO, LIFE_TEXT, LIFE_INTRODUCE, ROW, COLUMN } export interface AdvertisingModifier { stack: AdvertisingAttrModifier background: AdvertisingAttrModifier title: AdvertisingAttrModifier logo: AdvertisingAttrModifier lifeText: AdvertisingAttrModifier lifeIntroduce: AdvertisingAttrModifier row: AdvertisingAttrModifier<RowAttribute> column: AdvertisingAttrModifier<ColumnAttribute> } export const advertisingModifier = (): AdvertisingModifier => { return { stack: new AdvertisingAttrModifier(AdvertisingAttrType.STACK), background: new AdvertisingAttrModifier(AdvertisingAttrType.BACKGROUND), title: new AdvertisingAttrModifier(AdvertisingAttrType.TITLE), logo: new AdvertisingAttrModifier(AdvertisingAttrType.LOGO), lifeText: new AdvertisingAttrModifier(AdvertisingAttrType.LIFE_TEXT), lifeIntroduce: new AdvertisingAttrModifier(AdvertisingAttrType.LIFE_INTRODUCE), row: new AdvertisingAttrModifier(AdvertisingAttrType.ROW), column: new AdvertisingAttrModifier(AdvertisingAttrType.COLUMN) } } export class AdvertisingAttrModifier<Attribute extends CommonAttribute | TextAttribute = CommonAttribute> implements AttributeModifier<Attribute> { attrType: AdvertisingAttrType constructor(attrType: AdvertisingAttrType) { this.attrType = attrType } applyNormalAttribute(instance: Attribute): void { this.setStack(instance) this.setBackground(instance) this.setTitle(instance as TextAttribute) this.setLogo(instance) this.setRow(instance as RowAttribute) this.setColumn(instance as ColumnAttribute) this.setLifeText(instance as TextAttribute) this.setLifeIntroduce(instance as TextAttribute) } setStack(instance: Attribute) { this.attrType === AdvertisingAttrType.STACK && instance .width(CommonConstants.FULL_WIDTH) .height(CommonConstants.FULL_WIDTH) } setBackground(instance: Attribute) { this.attrType == AdvertisingAttrType.BACKGROUND && instance .width(CommonConstants.FULL_WIDTH) .height(CommonConstants.FULL_HEIGHT) } setTitle(instance: TextAttribute) { this.attrType == AdvertisingAttrType.TITLE && instance .fontColor(Color.White) .fontSize($r('app.float.advertising_text_font_size')) .letterSpacing(CommonConstants.ADVERTISING_TITLE_TEXT_LETTER_SPACING) .backgroundColor($r('app.color.advertising_text_background_color')) .border({ radius: $r('app.float.advertising_text_radius'), width: $r('app.float.advertising_text_border_width'), color: Color.White }) .margin({ top: $r('app.float.advertising_title_text_margin_top'), left: $r('app.float.advertising_title_text_margin_left') }) .padding({ left: $r('app.float.advertising_text_padding_left'), top: $r('app.float.advertising_text_padding_top'), right: $r('app.float.advertising_text_padding_right'), bottom: $r('app.float.advertising_text_padding_bottom') }) } setLogo(instance: Attribute) { this.attrType == AdvertisingAttrType.LOGO && instance .width($r('app.float.advertising_image_width')) .height($r('app.float.advertising_image_height')) .margin({ bottom: CommonConstants.ADVERTISING_ROW_MARGIN_BOTTOM }) } setRow(instance: RowAttribute) { this.attrType == AdvertisingAttrType.ROW && instance .alignItems(VerticalAlign.Bottom) .height(CommonConstants.FULL_HEIGHT) } setColumn(instance: ColumnAttribute) { this.attrType == AdvertisingAttrType.COLUMN && instance .alignItems(HorizontalAlign.Start) .margin({ left: CommonConstants.ADVERTISING_COLUMN_MARGIN_LEFT, bottom: CommonConstants.ADVERTISING_ROW_MARGIN_BOTTOM }) } setLifeText(instance: TextAttribute) { this.attrType == AdvertisingAttrType.LIFE_TEXT && instance .fontWeight(FontWeight.Bold) .letterSpacing(CommonConstants.ADVERTISING_HEALTHY_LIFE_TEXT_SPACING) .fontSize($r('app.float.advertising_text_title_size')) .fontColor($r('app.color.advertising_text_title_color')) } setLifeIntroduce(instance: TextAttribute) { this.attrType == AdvertisingAttrType.LIFE_INTRODUCE && instance .fontWeight(FontWeight.Normal) .letterSpacing(CommonConstants.ADVERTISING_HEALTHY_LIFE_TEXT_SPACING) .fontSize($r('app.float.advertising_text_introduce_size')) .fontColor($r('app.color.launcher_text_introduce_color')) .opacity($r('app.float.advertising_text_opacity')) .margin({ top: CommonConstants.ADVERTISING_TEXT_INTRODUCE_MARGIN_TOP }) } } |
DialogModifier.ets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
import CommonConstants from '../common/constants/CommonConstants'; export enum DialogAttrType { TITLE, CONTENT, STATEMENT, PROMPT, DISAGREE, AGREE, ROW, COLUMN, BLANK } export interface DialogModifier { title: DialogAttrModifier<TextAttribute>; content: DialogAttrModifier<TextAttribute>; statement: DialogAttrModifier<TextAttribute>; prompt: DialogAttrModifier<TextAttribute>; disagree: DialogAttrModifier<TextAttribute>; agree: DialogAttrModifier<TextAttribute>; row: DialogAttrModifier<RowAttribute>; column: DialogAttrModifier<ColumnAttribute>; blank: DialogAttrModifier<BlankAttribute> } export const dialogModifier = (): DialogModifier => { return { title: new DialogAttrModifier(DialogAttrType.TITLE), content: new DialogAttrModifier(DialogAttrType.CONTENT), statement: new DialogAttrModifier(DialogAttrType.STATEMENT), prompt: new DialogAttrModifier(DialogAttrType.PROMPT), disagree: new DialogAttrModifier(DialogAttrType.DISAGREE), agree: new DialogAttrModifier(DialogAttrType.AGREE), row: new DialogAttrModifier(DialogAttrType.ROW), column: new DialogAttrModifier(DialogAttrType.COLUMN), blank: new DialogAttrModifier(DialogAttrType.BLANK) } } export class DialogAttrModifier<Attribute extends TextAttribute | BlankAttribute | CommonAttribute = TextAttribute> implements AttributeModifier<Attribute> { attrType: DialogAttrType constructor(type: DialogAttrType) { this.attrType = type } applyNormalAttribute(instance: Attribute): void { this.setTitleStyle(instance as TextAttribute) this.setContentStyle(instance as TextAttribute) this.setStatementStyle(instance as TextAttribute) this.setPromptStyle(instance as TextAttribute) this.setDisagreeStyle(instance as TextAttribute) this.setAgreeStyle(instance as TextAttribute) this.setColumnStyle(instance as ColumnAttribute) this.setRowStyle(instance as RowAttribute) this.setBlankStyle(instance as BlankAttribute) } setTitleStyle(instance: TextAttribute) { this.attrType === DialogAttrType.TITLE && instance .width(CommonConstants.DIALOG_COMPONENT_WIDTH_PERCENT) .fontColor($r('app.color.dialog_text_color')) .fontSize($r('app.float.dialog_text_privacy_size')) .textAlign(TextAlign.Center) .fontWeight(CommonConstants.DIALOG_TITLE_FONT_WEIGHT) .margin({ top: $r('app.float.dialog_text_privacy_top'), bottom: $r('app.float.dialog_text_privacy_bottom') }) } setContentStyle(instance: TextAttribute) { this.attrType === DialogAttrType.CONTENT && instance .fontSize($r('app.float.dialog_common_text_size')) .width(CommonConstants.DIALOG_COMPONENT_WIDTH_PERCENT) } setStatementStyle(instance: TextAttribute) { this.attrType === DialogAttrType.STATEMENT && instance .width(CommonConstants.DIALOG_COMPONENT_WIDTH_PERCENT) .fontColor($r('app.color.dialog_text_statement_color')) .fontSize($r('app.float.dialog_common_text_size')) } setPromptStyle(instance: TextAttribute) { this.attrType === DialogAttrType.PROMPT && instance .width(CommonConstants.DIALOG_COMPONENT_WIDTH_PERCENT) .fontColor($r('app.color.dialog_text_color')) .fontSize($r('app.float.dialog_common_text_size')) .opacity($r('app.float.dialog_text_opacity')) .margin({ bottom: $r('app.float.dialog_text_declaration_bottom') }) } setDisagreeStyle(instance: TextAttribute) { this.attrType === DialogAttrType.DISAGREE && instance .fontColor($r('app.color.dialog_fancy_text_color')) .fontSize($r('app.float.dialog_fancy_text_size')) .textAlign(TextAlign.Center) .fontWeight(FontWeight.Medium) .layoutWeight(CommonConstants.COMMON_LAYOUT_WEIGHT) } setAgreeStyle(instance: TextAttribute) { this.attrType === DialogAttrType.AGREE && instance .fontColor($r('app.color.dialog_fancy_text_color')) .fontSize($r('app.float.dialog_fancy_text_size')) .textAlign(TextAlign.Center) .fontWeight(FontWeight.Medium) .layoutWeight(CommonConstants.COMMON_LAYOUT_WEIGHT) } setRowStyle(instance: RowAttribute) { this.attrType === DialogAttrType.ROW && instance .margin({ bottom: CommonConstants.DIALOG_ROW_MARGIN_BOTTOM }) } setColumnStyle(instance: ColumnAttribute) { this.attrType === DialogAttrType.COLUMN && instance .width(CommonConstants.DIALOG_COMPONENT_WIDTH_PERCENT) .borderRadius(CommonConstants.DIALOG_BORDER_RADIUS) .backgroundColor(Color.White) } setBlankStyle(instance: BlankAttribute) { this.attrType === DialogAttrType.BLANK && instance .backgroundColor($r('app.color.dialog_blank_background_color')) .width($r('app.float.dialog_blank_width')) .height($r('app.float.dialog_blank_height')) } } |
LauncherModifier.ets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
import CommonConstants from '../common/constants/CommonConstants' export enum LauncherAttrType { BACKGROUND, LOGO, COLUMN, TEXT, INTRODUCE } export interface LauncherModifier { background: LauncherAttrModifier<ImageAttribute> logo: LauncherAttrModifier<ImageAttribute> column: LauncherAttrModifier<ColumnAttribute> text: LauncherAttrModifier<TextAttribute> introduce: LauncherAttrModifier<TextAttribute> } export const launcherModifier = (): LauncherModifier => { return { background: new LauncherAttrModifier(LauncherAttrType.BACKGROUND), logo: new LauncherAttrModifier(LauncherAttrType.LOGO), column: new LauncherAttrModifier(LauncherAttrType.COLUMN), text: new LauncherAttrModifier(LauncherAttrType.TEXT), introduce: new LauncherAttrModifier(LauncherAttrType.INTRODUCE) } } export class LauncherAttrModifier<Attribute extends CommonAttribute | TextAttribute> implements AttributeModifier<Attribute> { attrType: LauncherAttrType constructor(attrType: LauncherAttrType) { this.attrType = attrType } applyNormalAttribute(instance: Attribute): void { this.setBgStyle(instance) this.setLogoStyle(instance) this.setColumnStyle(instance) this.setTextStyle(instance as TextAttribute) this.setIntroduceStyle(instance as TextAttribute) } setBgStyle(instance: Attribute) { (this.attrType === LauncherAttrType.BACKGROUND) && instance .width(CommonConstants.FULL_WIDTH) .height(CommonConstants.FULL_HEIGHT) } setLogoStyle(instance: Attribute) { (this.attrType === LauncherAttrType.LOGO) && instance .width($r('app.float.launcher_logo_size')) .height($r('app.float.launcher_logo_size')) .margin({ top: CommonConstants.LAUNCHER_IMAGE_MARGIN_TOP }) } setColumnStyle(instance: Attribute) { (this.attrType === LauncherAttrType.COLUMN) && instance .width(CommonConstants.FULL_WIDTH) .height(CommonConstants.FULL_HEIGHT) } setTextStyle(instance: TextAttribute) { (this.attrType === LauncherAttrType.TEXT) && instance .width($r('app.float.launcher_life_text_width')) .height($r('app.float.launcher_life_text_height')) .fontWeight(FontWeight.Bold) .letterSpacing(CommonConstants.LAUNCHER_LIFE_TEXT_SPACING) .fontSize($r('app.float.launcher_text_title_size')) .fontColor($r('app.color.launcher_text_title_color')) .margin({ top: CommonConstants.LAUNCHER_TEXT_TITLE_MARGIN_TOP }) } setIntroduceStyle(instance: TextAttribute) { (this.attrType === LauncherAttrType.INTRODUCE) && instance .height(CommonConstants.LAUNCHER_TEXT_INTRODUCE_HEIGHT) .fontWeight(FontWeight.Normal) .letterSpacing(CommonConstants.LAUNCHER_TEXT_INTRODUCE_SPACING) .fontSize($r('app.float.launcher_text_introduce_size')) .fontColor($r('app.color.launcher_text_introduce_color')) .opacity($r('app.float.launcher_text_opacity')) .margin({ top: CommonConstants.LAUNCHER_TEXT_INTRODUCE_MARGIN_TOP }) } } |
common
constants/CommonConstants.ets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
/** * Common constants for all features. */ export default class CommonConstants { /** * The main ability tag. */ static readonly ENTRY_ABILITY_TAG: string = 'EntryAbility'; /** * The launcher page tag. */ static readonly LAUNCHER_PAGE_TAG: string = 'LauncherPage'; /** * The advertsing page tag. */ static readonly ADVERTISING_PAGE_TAG: string = 'AdvertisingPage'; /** * The custom dialog component tag. */ static readonly CUSTOM_DIALOG_TAG: string = 'CustomDialogComponent'; /** * Preference saved key. */ static readonly PREFERENCES_KEY_PRIVACY: string = 'isPrivacy'; /** * Preference saved file name. */ static readonly PREFERENCES_FILE_NAME: string = 'myStore'; /** * Launcher page count down. */ static readonly LAUNCHER_DELAY_TIME: number = 3000; /** * Image logo top margin. */ static readonly LAUNCHER_IMAGE_MARGIN_TOP: string = '16.2%'; /** * Healthy living text spacing. */ static readonly LAUNCHER_LIFE_TEXT_SPACING: number = 0.1; /** * Healthy living title text top margin. */ static readonly LAUNCHER_TEXT_TITLE_MARGIN_TOP: string = '0.5%'; /** * Content control height. */ static readonly LAUNCHER_TEXT_INTRODUCE_HEIGHT: string = '2.7%'; /** * Healthy living instructions. */ static readonly LAUNCHER_TEXT_INTRODUCE_SPACING: number = 3.4; /** * Healthy living content top margin. */ static readonly LAUNCHER_TEXT_INTRODUCE_MARGIN_TOP: string = '1.3%'; /** * Interval execution time. */ static readonly ADVERTISING_INTERVAL_TIME: number = 1000; /** * Advertising page url. */ static readonly ADVERTISING_PAGE_URL: string = 'pages/AdvertisingPage'; /** * Display countdown seconds. */ static readonly ADVERTISING_COUNT_DOWN_SECONDS: number = 2; /** * Count down text spacing. */ static readonly ADVERTISING_TITLE_TEXT_LETTER_SPACING: number = 0.05; /** * Advertising page healthy text spacing. */ static readonly ADVERTISING_HEALTHY_LIFE_TEXT_SPACING: number = 0.1; /** * Advertising page health description text spacing. */ static readonly ADVERTISING_TEXT_INTRODUCE_LETTER_SPACING: number = 3.4; /** * Advertising page health description text top margin. */ static readonly ADVERTISING_TEXT_INTRODUCE_MARGIN_TOP: string = '0.4%'; /** * Column container left margin. */ static readonly ADVERTISING_COLUMN_MARGIN_LEFT: string = '3.1%'; /** * Row container bottom margin. */ static readonly ADVERTISING_ROW_MARGIN_BOTTOM: string = '3.1%'; /** * Dialog component width the percentage of the 90. */ static readonly DIALOG_COMPONENT_WIDTH_PERCENT: string = '90%'; /** * Dialog title text weight. */ static readonly DIALOG_TITLE_FONT_WEIGHT: number = 600; /** * Dialog width the percentage of the 93.3. */ static readonly DIALOG_WIDTH_PERCENT: string = '93.3%'; /** * Dialog border radius. */ static readonly DIALOG_BORDER_RADIUS: number = 24; /** * Dialog component bottom margin, */ static readonly DIALOG_ROW_MARGIN_BOTTOM: string = '3.1%'; /** * Dialog y-axis offset distance. */ static readonly DIALOG_CONTROLLER_DY_OFFSET: number = -24; /** * Width the percentage of the 100. */ static readonly FULL_WIDTH: string = '100%'; /** * Height the percentage of the 100. */ static readonly FULL_HEIGHT: string = '100%'; /** * Privacy page url. */ static readonly PRIVACY_PAGE_URL: string = 'pages/PrivacyPage'; /** * App home page url. */ static readonly APP_HOME_PAGE_URL: string = 'pages/AppHomePage'; /** * Common layout weight. */ static readonly COMMON_LAYOUT_WEIGHT: number = 1; } |
utils/GlobalContext.ets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
export class GlobalContext { private constructor() { } private static instance: GlobalContext; private _objects = new Map<string, Object>(); public static getContext(): GlobalContext { if (!GlobalContext.instance) { GlobalContext.instance = new GlobalContext(); } return GlobalContext.instance; } getObject(value: string): Object | undefined { return this._objects.get(value); } setObject(key: string, objectClass: Object): void { this._objects.set(key, objectClass); } } |
utils/Logger.ets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import { hilog } from '@kit.PerformanceAnalysisKit'; class Logger { private domain: number; private prefix: string; private format: string = '%{public}s, %{public}s'; /** * constructor. * * @param Prefix Identifies the log tag. * @param domain Domain Indicates the service domain, which is a hexadecimal integer ranging from 0x0 to 0xFFFFF. */ constructor(prefix: string = 'MyApp', domain: number = 0xFF00) { this.prefix = prefix; this.domain = domain; } debug(...args: string[]): void { hilog.debug(this.domain, this.prefix, this.format, args); } info(...args: string[]): void { hilog.info(this.domain, this.prefix, this.format, args); } warn(...args: string[]): void { hilog.warn(this.domain, this.prefix, this.format, args); } error(...args: string[]): void { hilog.error(this.domain, this.prefix, this.format, args); } } export default new Logger('FirstStartDemo', 0xFF00) |
entryability
EntryAbility.ets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { window } from '@kit.ArkUI'; export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); } onDestroy(): void { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); } onWindowStageCreate(windowStage: window.WindowStage): void { // Main window is created, set main page for this ability hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); windowStage.loadContent('pages/LauncherPage', (err) => { if (err.code) { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; } hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); }); } onWindowStageDestroy(): void { // Main window is destroyed, release UI related resources hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); } onForeground(): void { // Ability has brought to foreground hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); } onBackground(): void { // Ability has back to background hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); } } |
pages
AdvertisingPage.ets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
import { advertisingModifier, AdvertisingModifier } from '../attributemodifier/AdvertisingModifier' import CommonConstants from '../common/constants/CommonConstants' import { router } from '@kit.ArkUI' import Logger from '../common/utils/Logger' @Entry @Component struct AdvertisingPage { @State countDownSeconds: number = CommonConstants.ADVERTISING_COUNT_DOWN_SECONDS private timeId: number = 0 private modifier: AdvertisingModifier = advertisingModifier() onPageShow() { this.timeId = setInterval(() => { if (this.countDownSeconds == 0) { this.jumpToAppHomePage() } else { this.countDownSeconds-- } }, CommonConstants.ADVERTISING_INTERVAL_TIME) } onPageHide(): void { router.clear() clearInterval(this.timeId) } jumpToAppHomePage() { router.pushUrl({ url: CommonConstants.APP_HOME_PAGE_URL }).catch((error: Error) => { Logger.error(CommonConstants.ADVERTISING_PAGE_TAG, 'AdvertisingPage pushUrl error ' + JSON.stringify(error)) }) } build() { Stack({ alignContent: Alignment.Top }) { Image($r('app.media.ic_advertising_background')) .attributeModifier(this.modifier.background) Text($r('app.string.advertising_text_title', this.countDownSeconds)) .attributeModifier(this.modifier.title) .onClick(() => { this.jumpToAppHomePage() }) Row() { Image($r('app.media.ic_logo')) .attributeModifier(this.modifier.logo) Column() { Text($r('app.string.healthy_life_text')) .attributeModifier(this.modifier.lifeText) Text($r('app.string.healthy_life_introduce')) .attributeModifier(this.modifier.lifeIntroduce) } .attributeModifier(this.modifier.column) } .attributeModifier(this.modifier.row) } .attributeModifier(this.modifier.stack) } } |
AppHomePage.ets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import CommonConstants from '../common/constants/CommonConstants' @Entry @Component struct AppHomePage { build() { Stack() { Image($r('app.media.ic_home_page_background')) .width(CommonConstants.FULL_WIDTH) .height(CommonConstants.FULL_HEIGHT) Text($r('app.string.home_page_text')) .fontSize($r('app.float.home_page_text_size')) .fontColor($r('app.color.home_page_text_color')) .fontWeight(FontWeight.Bold) .textAlign(TextAlign.Center) .height(CommonConstants.FULL_HEIGHT) .width(CommonConstants.FULL_WIDTH) } } } |
LauncherPage.ets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
import { launcherModifier, LauncherModifier } from '../attributemodifier/LauncherModifier' import { common } from '@kit.AbilityKit' import Logger from '../common/utils/Logger' import CommonConstants from '../common/constants/CommonConstants' import { preferences } from '@kit.ArkData' import { GlobalContext } from '../common/utils/GlobalContext' import CustomDialogComponent from '../view/CustomDialogComponent' import { router } from '@kit.ArkUI' @Entry @Component struct LauncherPage { private modifier: LauncherModifier = launcherModifier() private context?: common.UIAbilityContext private timerId: number = 0 private isJumpToAdvertising: boolean = false dialogController: CustomDialogController = new CustomDialogController({ builder: CustomDialogComponent({ cancel: () => { this.onCancel() }, confirm: () => { this.onConfirm() } }), alignment: DialogAlignment.Bottom, offset: { dx: 0, dy: CommonConstants.DIALOG_CONTROLLER_DY_OFFSET }, customStyle: true, autoCancel: false }) onPageShow() { this.context = getContext(this) as common.UIAbilityContext this.getDataPreferences(this).then((preferences: preferences.Preferences) => { // preferences.delete(CommonConstants.PREFERENCES_KEY_PRIVACY) preferences.get(CommonConstants.PREFERENCES_KEY_PRIVACY, true).then((value: preferences.ValueType) => { Logger.info(CommonConstants.LAUNCHER_PAGE_TAG, 'onPageShow value: ' + value) if (value) { let isJumpPrivacy: boolean = (GlobalContext.getContext().getObject('isJumpPrivacy') as boolean) ?? false if (!isJumpPrivacy) { this.dialogController.open() } } else { this.jumpToAdvertisingPage() } }) }) } onPageHide(): void { if (this.isJumpToAdvertising) { router.clear() } GlobalContext.getContext().setObject('isJumpPrivacy', true) clearTimeout(this.timerId) } onCancel() { // Exit the application. this.context?.terminateSelf() } onConfirm() { // Save privacy agreement status. this.saveIsPrivacy() this.jumpToAdvertisingPage() } saveIsPrivacy() { let preferences: Promise<preferences.Preferences> = this.getDataPreferences(this) preferences.then((result: preferences.Preferences) => { let privacyPut = result.put(CommonConstants.PREFERENCES_KEY_PRIVACY, false) result.flush() privacyPut.then(() => { Logger.info(CommonConstants.LAUNCHER_PAGE_TAG, 'Put the value of startup SuccessFully.') }).catch((err: Error) => { Logger.error(CommonConstants.LAUNCHER_PAGE_TAG, 'Put the value of startup Failed, err: ' + err) }) }).catch((err: Error) => { Logger.error(CommonConstants.LAUNCHER_PAGE_TAG, 'Get the preferences Failed, err: ' + err) }) } /** * Jump to advertising page. */ jumpToAdvertisingPage() { this.timerId = setTimeout(() => { this.isJumpToAdvertising = true router.pushUrl({ url: CommonConstants.ADVERTISING_PAGE_URL }).catch((error: Error) => { Logger.error(CommonConstants.LAUNCHER_PAGE_TAG, 'LauncherPage pushUrl error ' + JSON.stringify(error)) }) }, CommonConstants.LAUNCHER_DELAY_TIME) } /** * Get data preferences action. * @param common * @returns */ getDataPreferences(common: Object) { return preferences.getPreferences(getContext(common), CommonConstants.PREFERENCES_FILE_NAME) } build() { Stack() { Image($r('app.media.ic_launcher_background')) .attributeModifier(this.modifier.background) Column() { Image($r('app.media.ic_logo')) .attributeModifier(this.modifier.logo) Text($r('app.string.healthy_life_text')) .attributeModifier(this.modifier.text) Text($r('app.string.healthy_life_introduce')) .attributeModifier(this.modifier.introduce) } .attributeModifier(this.modifier.column) } } } |
PrivacyPage.ets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import CommonConstants from '../common/constants/CommonConstants' import { GlobalContext } from '../common/utils/GlobalContext' import { router } from '@kit.ArkUI' @Entry @Component struct PrivacyPage { build() { Column() { Text($r('app.string.privacy_text_title')) .titleStyle() Text($r('app.string.privacy_text_content')) .contentStyle() Text($r('app.string.privacy_back')) .backStyle() .onClick(() => { GlobalContext.getContext().setObject('isJumpPrivacy', false); router.back(); }) } .justifyContent(FlexAlign.Center) .height(CommonConstants.FULL_HEIGHT) } onBackPress(): boolean | void { GlobalContext.getContext().setObject('isJumpPrivacy', false) } } @Extend(Text) function contentStyle() { .fontSize($r('app.float.dialog_common_text_size')) .margin({ left: $r('app.float.privacy_text_content_left'), right: $r('app.float.privacy_text_content_right') }) } @Extend(Text) function backStyle() { .fontColor($r('app.color.privacy_back_text')) .fontSize($r('app.float.privacy_back_text_size')) .textAlign(TextAlign.Center) .fontWeight(FontWeight.Medium) .margin({ top: $r('app.float.privacy_bottom_text_margin') }) } @Extend(Text) function titleStyle() { .fontSize($r('app.float.privacy_text_title_size')) .textAlign(TextAlign.Center) .margin({ top: $r('app.float.privacy_text_margin_top'), bottom: $r('app.float.privacy_text_margin_bottom') }) } |
view
CustomDialogComponent.ets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
import { dialogModifier, DialogModifier } from '../attributemodifier/DialogModifier' import { GlobalContext } from '../common/utils/GlobalContext' import { router } from '@kit.ArkUI' import CommonConstants from '../common/constants/CommonConstants' import Logger from '../common/utils/Logger' @CustomDialog export default struct CustomDialogComponent { private modifier: DialogModifier = dialogModifier() controller: CustomDialogController = new CustomDialogController({ builder: '' }) cancel: Function = () => { } confirm: Function = () => { } build() { Column() { Text($r('app.string.dialog_text_title')) .attributeModifier(this.modifier.title) Text($r('app.string.dialog_text_privacy_content')) .attributeModifier(this.modifier.content) Text($r('app.string.dialog_text_privacy_statement')) .attributeModifier(this.modifier.statement) .onClick(() => { this.controller.close() GlobalContext.getContext().setObject('isJumpPrivacy', true) router.pushUrl({ url: CommonConstants.PRIVACY_PAGE_URL }).catch((error: Error) => { Logger.error(CommonConstants.CUSTOM_DIALOG_TAG, 'CustomDialog pushUrl error ' + JSON.stringify(error)) }) }) Text($r('app.string.dialog_text_declaration_prompt')) .attributeModifier(this.modifier.prompt) Row() { Text($r('app.string.dialog_button_disagree')) .attributeModifier(this.modifier.disagree) .onClick(() => { this.controller.close() this.cancel() }) Blank() .attributeModifier(this.modifier.blank) Text($r('app.string.dialog_button_agree')) .attributeModifier(this.modifier.agree) .onClick(() => { this.controller.close() this.confirm() }) } .attributeModifier(this.modifier.row) } .attributeModifier(this.modifier.column) } } |