diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 4dc3e187714bb4bcdc2b61ab69bbc3c9880a3556..f131619d29ca114b29baa43e366c5946b33f4a45 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -98,6 +98,9 @@ export default class EntryAbility extends UIAbility { return; } Logger.info(TAG, 'Succeeded in loading the content. Data: ' + JSON.stringify(data) ?? ''); + + let uiContext: UIContext | undefined = windowStage.getMainWindowSync().getUIContext() + AppStorage.setOrCreate('uiContext', uiContext); }); } diff --git a/entry/src/main/ets/pages/EditFile.ets b/entry/src/main/ets/pages/EditFile.ets index 9f87fe4e365b89ac279f9c9e2c8ec0763a960bcb..d0d2a535ee0571f93091ab15e9e70ada9ff7dcb3 100644 --- a/entry/src/main/ets/pages/EditFile.ets +++ b/entry/src/main/ets/pages/EditFile.ets @@ -21,9 +21,9 @@ import MediaFileUri from '../media/MediaFileUri'; import Logger from '../common/Logger'; import { terminateSelf } from '../utils/utils'; import { Constants } from '../common/Constants'; - +const uiContext: UIContext | undefined = AppStorage.get('uiContext'); const TAG = 'EditFile: '; -let storage = LocalStorage.getShared(); +let storage = uiContext!.getSharedLocalStorage(); const OPACITY_VALUE = 0.6; interface myParams extends Object { @@ -43,7 +43,7 @@ struct EditFile { @StorageLink('editable') editable: Boolean = false; @StorageLink('myFileSize') myFileSize: number = 0; @StorageLink('myFileContent') myFileContent: string = ''; - @State myContext: Context = getContext(this) as common.UIAbilityContext; + @State myContext: Context = this.getUIContext().getHostContext() as common.UIAbilityContext; @State myUri: string = ''; @State opacityValue: number = OPACITY_VALUE; @State uriSave: string = ''; @@ -62,8 +62,8 @@ struct EditFile { this.myUri = this.loadUri; Logger.info(TAG, 'The count of getFileInfo is myFileContent ' + this.myFileContent); } else { - this.myUri = (router.getParams() as myParams).myUri; - this.myFileName = (router.getParams() as myParams).fileName; + this.myUri = (this.getUIContext().getRouter().getParams() as myParams).myUri; + this.myFileName = (this.getUIContext().getRouter().getParams() as myParams).fileName; this.myFileContent = this.mediaFileUri.readFileContent(this.myUri); this.myFileSize = this.mediaFileUri.myGetFileSize(this.myUri, fileIo.OpenMode.READ_ONLY); Logger.info(TAG, 'The count of getFileInfo is myFileName is: ' + this.myFileName); @@ -125,10 +125,10 @@ struct EditFile { .onClick(() => { if (this.loadFlag) { Logger.info(TAG, 'end page'); - let context = getContext(this); + let context = this.getUIContext().getHostContext(); terminateSelf(context); } else { - router.back(); + this.getUIContext().getRouter().back(); } }) } @@ -206,7 +206,7 @@ struct EditFile { AppStorage.setOrCreate('editable', this.editable); Logger.info(TAG, 'EditFile caretPosition length = ' + this.myFileContent.length); this.controller.caretPosition(this.myFileContent.length); - promptAction.showToast({ message: $r('app.string.editable') }); + this.getUIContext().getPromptAction().showToast({ message: $r('app.string.editable') }); }) Image($r('app.media.ic_save')) @@ -248,7 +248,7 @@ struct EditFile { flage = false; Logger.info(`save data to file failed with error: ${JSON.stringify(err)}: ${JSON.stringify(err?.message)}`); - promptAction.showToast({ + this.getUIContext().getPromptAction().showToast({ message: $r('app.string.show_toast_message'), duration: 6500 }) @@ -257,7 +257,7 @@ struct EditFile { if (flage) { this.editable = false; AppStorage.setOrCreate('editable', this.editable); - promptAction.showToast({ message: $r('app.string.saved') }); + this.getUIContext().getPromptAction().showToast({ message: $r('app.string.saved') }); } } }) diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 7663e787e4eaaf50b5250179e275654e0abe49f2..e1135d4ce778dde6c6b3f31acadefda149ac6b3b 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -90,7 +90,7 @@ struct Index { } async getFilenameByUriForMedia(myUris: string[]) { - router.pushUrl({ + this.getUIContext().getRouter().pushUrl({ url: 'pages/ViewMedia', params: { uris: myUris @@ -100,7 +100,7 @@ struct Index { async getFilenameByUri(myUri: string): Promise { this.filename = (myUri.split('/').pop()) as string; - router.pushUrl({ + this.getUIContext().getRouter().pushUrl({ url: 'pages/EditFile', params: { fileName: this.filename, @@ -288,7 +288,7 @@ struct Index { .onClick(() => { Logger.info(TAG, 'fileAsset.displayName fileName item: ' + item); if (index !== undefined) { - router.pushUrl({ + this.getUIContext().getRouter().pushUrl({ url: 'pages/EditFile', params: { fileName: item, diff --git a/entry/src/main/ets/pages/ViewMedia.ets b/entry/src/main/ets/pages/ViewMedia.ets index d176d5ed9feae1a1f5028210f0d8adbe731a88a3..d1c75ccc13fcaec79938f3da5f06c29aa33c0ed2 100644 --- a/entry/src/main/ets/pages/ViewMedia.ets +++ b/entry/src/main/ets/pages/ViewMedia.ets @@ -31,13 +31,13 @@ interface myParams extends Object { @Entry @Component struct ViewMedia { - @State myContext: Context = getContext(this) as common.UIAbilityContext; + @State myContext: Context = this.getUIContext().getHostContext() as common.UIAbilityContext; @State myFileSizes: number[] = []; @State myFileNames: string[] = []; @State myFileTypes: number[] = []; @StorageLink('myFileName') myFileName: string = ''; @StorageLink('myFileSize') myFileSize: number = 0; - @State myUris: string[] = (router.getParams() as myParams).uris; + @State myUris: string[] = (this.getUIContext().getRouter().getParams() as myParams).uris; @State uri: string = 'Hello World'; @StorageLink('showPauses') showPauses: Array = []; mediaFileUri: MediaFileUri = new MediaFileUri(); @@ -86,7 +86,7 @@ struct ViewMedia { .align(Alignment.Start) .id('back2Index') .onClick(() => { - router.back(); + this.getUIContext().getRouter().back(); }) } .width(Constants.BACK_WIDTH)