ini myapp and review rest api
This commit is contained in:
parent
58ee459459
commit
ff8c5714d9
23
app/src/main/ets/MyApp.ets
Normal file
23
app/src/main/ets/MyApp.ets
Normal file
@ -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();
|
||||
}
|
||||
}
|
@ -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) ?? '');
|
||||
});
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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();
|
||||
});
|
||||
}
|
||||
}
|
@ -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({
|
||||
|
@ -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 {
|
||||
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user