diff --git a/app/src/main/ets/MyApp.ets b/app/src/main/ets/MyApp.ets new file mode 100644 index 0000000..2478c58 --- /dev/null +++ b/app/src/main/ets/MyApp.ets @@ -0,0 +1,23 @@ +import { Context } from '@ohos.arkui.UIContext'; +import window from '@ohos.window'; + +export class MyApp { + + static appContext: Context; + static uiContext: UIContext; + static mainWindow: window.Window; + static uiAbilityContext: Context; + + private constructor() { + } + + static initAbility(uiAbilityContext: Context) { + MyApp.uiAbilityContext = uiAbilityContext; + MyApp.appContext = uiAbilityContext.getApplicationContext(); + } + + static initWindow(window: window.Window) { + MyApp.mainWindow = window; + MyApp.uiContext = window.getUIContext(); + } +} \ No newline at end of file diff --git a/app/src/main/ets/appability/AppAbility.ets b/app/src/main/ets/appability/AppAbility.ets index 3222813..46ea429 100644 --- a/app/src/main/ets/appability/AppAbility.ets +++ b/app/src/main/ets/appability/AppAbility.ets @@ -1,6 +1,7 @@ import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { window } from '@kit.ArkUI'; +import { MyApp } from '../MyApp'; const TAG = '[AppAbility]' @@ -21,12 +22,13 @@ export default class AppAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage): void { // Main window is created, set main page for this ability hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); - + MyApp.initAbility(this.context); windowStage.loadContent('pages/Index', (err, data) => { if (err.code) { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; } + MyApp.initWindow(windowStage.getMainWindowSync()); hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); }); } diff --git a/app/src/main/ets/pages/Index.ets b/app/src/main/ets/pages/Index.ets index 2ce7e30..ed1c6b8 100644 --- a/app/src/main/ets/pages/Index.ets +++ b/app/src/main/ets/pages/Index.ets @@ -8,6 +8,7 @@ import { BaseLocalStorage, ScreenUtil } from '@devwiki/base'; import { HomeItem, HomeItemGroup } from '../model/Home'; import { Context } from '@kit.AbilityKit'; import { Calculator } from '@devwiki/hmcalculate'; +import { MyApp } from '../MyApp'; import('./animation/LoadingPage') @@ -68,8 +69,8 @@ struct Index { ]; aboutToAppear(): void { - ScreenUtil.getInstance().initScreenSize(); - BaseLocalStorage.getInstance().init(getContext(this)); + ScreenUtil.getInstance().initScreenSize(MyApp.appContext); + BaseLocalStorage.getInstance().init(MyApp.appContext); } onPageShow(): void { diff --git a/app/src/main/ets/pages/net/RestAPIPage.ets b/app/src/main/ets/pages/net/RestAPIPage.ets index be4294b..4928257 100644 --- a/app/src/main/ets/pages/net/RestAPIPage.ets +++ b/app/src/main/ets/pages/net/RestAPIPage.ets @@ -1,5 +1,8 @@ import { Log } from '@devwiki/base/Index'; import axios, { AxiosError, AxiosResponse } from '@ohos/axios' +import http from '@ohos.net.http'; + +const TAG = '[RestAPIPage]' @Entry({routeName: "RestAPIPage"}) @Component @@ -8,29 +11,37 @@ export struct RestAPIPage { @State viewModel: RestAPIViewModel = new RestAPIViewModel(); build() { - Row() { + Column() { Flex({ - justifyContent: FlexAlign.SpaceBetween + justifyContent: FlexAlign.Start, + direction: FlexDirection.Column, + alignItems: ItemAlign.Center }) { - Button("Get").onClick(() =>{ - this.viewModel.getServerVersion(); - }) + Button("GetByAxios").onClick(() =>{ + this.viewModel.getServerVersionByAxios(); + }).margin({top: 20}) - Text(this.viewModel.serverVersion).backgroundColor(Color.Blue).flexGrow(1); - } - }.justifyContent(FlexAlign.Start).alignItems(VerticalAlign.Center).width('80%').backgroundColor(Color.Green) + Button("GetByHttp").onClick(() =>{ + this.viewModel.getServerVersionByHttp(); + }).margin({top: 20}) + + Text(this.viewModel.serverVersion).margin({top: 20}) + }.width('80%') + }.width('100%') } } class RestAPIViewModel { + readonly viewUrl: string = 'https://music.devwiki.net/rest/ping.view?v=1.16.1&c=myapp&f=json'; serverVersion: string = '123' - async getServerVersion() { + async getServerVersionByAxios() { + this.serverVersion = '' await axios({ method: "post", - url: 'https://music.devwiki.net/rest/ping.view?v=1.16.1&c=myapp&f=json' + url: this.viewUrl }).then((response: AxiosResponse) => { if (response.status == 200) { let version:string = response.data['subsonic-response']['serverVersion']; @@ -41,4 +52,22 @@ class RestAPIViewModel { Log.e(error.message); }) } + + getServerVersionByHttp() { + this.serverVersion = '' + let httpRequest = http.createHttp(); + let option:http.HttpRequestOptions = { + method: http.RequestMethod.GET + }; + httpRequest.request(this.viewUrl, option, (error, resp) =>{ + if (!error) { + Log.i(TAG, 'request http code:' + resp.responseCode) + let version:string = resp.result['subsonic-response']['serverVersion']; + this.serverVersion = version; + } else { + Log.e(TAG, `request failed, code:${error.code}, message:${error.message}`) + } + httpRequest.destroy(); + }); + } } \ No newline at end of file diff --git a/app/src/main/ets/pages/system/SetLanguagePage.ets b/app/src/main/ets/pages/system/SetLanguagePage.ets index c1ba7c9..2fa03c5 100644 --- a/app/src/main/ets/pages/system/SetLanguagePage.ets +++ b/app/src/main/ets/pages/system/SetLanguagePage.ets @@ -1,5 +1,8 @@ import { TitleBar } from '@devwiki/common_ui'; import { i18n } from '@kit.LocalizationKit'; +import { Log } from '@devwiki/base'; + +const TAG = '[SetLanguagePage]' @Entry @Component @@ -7,6 +10,13 @@ struct SetLanguagePage { private readonly languageGroup = "languageGroup" @State selectedLanguage: string = 'zh-Hans'; + aboutToAppear(): void { + let languages:string[] = i18n.System.getSystemLanguages(); + languages.forEach((value, index) => { + Log.i(TAG, `${index.toString()}:${value}`) + }) + } + build() { Column() { TitleBar({ diff --git a/base/src/main/ets/utils/Log.ets b/base/src/main/ets/utils/Log.ets index 9e11f2f..521501e 100644 --- a/base/src/main/ets/utils/Log.ets +++ b/base/src/main/ets/utils/Log.ets @@ -2,8 +2,8 @@ import hilog from '@ohos.hilog'; let domain: number = 0xFF00; -let prefix: string = 'HM4Demo'; -let format: string = `%{public}s`; +let prefix: string = 'HMDemo'; +let format: string = `%{public}s, %{public}s`; export class Log { diff --git a/base/src/main/ets/utils/ScreenUtil.ets b/base/src/main/ets/utils/ScreenUtil.ets index 8b67282..c18e16f 100644 --- a/base/src/main/ets/utils/ScreenUtil.ets +++ b/base/src/main/ets/utils/ScreenUtil.ets @@ -25,11 +25,11 @@ export class ScreenUtil { return ScreenUtil.instance; } - initScreenSize(): void { + initScreenSize(context: Context): void { this.portraitListener.on('change', (result)=> { AppStorage.setOrCreate(ScreenUtil.isPortraitKey, result.matches) }) - window.getLastWindow(getContext(this)) + window.getLastWindow(context) .then((windowClass: window.Window) => { let type = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR; // 以导航条避让为例 let avoidArea = windowClass.getWindowAvoidArea(type);