diff --git a/IntentsKitNewsUpdate/Application/build-profile.json5 b/IntentsKitNewsUpdate/Application/build-profile.json5 index e7ad1daa252110cb69dbd01d17d482fb004da49f..23b3a838425384810742349851b76e8e63ac8a65 100644 --- a/IntentsKitNewsUpdate/Application/build-profile.json5 +++ b/IntentsKitNewsUpdate/Application/build-profile.json5 @@ -6,6 +6,7 @@ "name": "default", "signingConfig": "default", "compatibleSdkVersion": "5.0.5(17)", + "targetSdkVersion": "5.0.5(17)", "runtimeOS": "HarmonyOS", "buildOption": { "strictMode": { diff --git a/IntentsKitNewsUpdate/Application/entry/src/main/ets/common/utils/Utils.ets b/IntentsKitNewsUpdate/Application/entry/src/main/ets/common/utils/Utils.ets index ec3b1de82f3f04a8ed2783a101bfdaada9cf811a..242004cf1610b95afb6026c264d80d4b7e088ff4 100644 --- a/IntentsKitNewsUpdate/Application/entry/src/main/ets/common/utils/Utils.ets +++ b/IntentsKitNewsUpdate/Application/entry/src/main/ets/common/utils/Utils.ets @@ -14,6 +14,7 @@ */ import { i18n } from '@kit.LocalizationKit'; +import Logger from './Logger'; export function formatRelativeTime(timestamp: number): string { const now = Date.now(); @@ -103,7 +104,8 @@ function padZero(num: number): string { */ export function formatDateTime(date: Date): string { if (!(date instanceof Date) || isNaN(date.getTime())) { - throw new Error('Invalid Date object'); + Logger.showError('formatDateTime', 'Invalid Date object'); + date = new Date(); } const year = date.getFullYear(); diff --git a/IntentsKitNewsUpdate/Application/entry/src/main/ets/entryability/EntryAbility.ets b/IntentsKitNewsUpdate/Application/entry/src/main/ets/entryability/EntryAbility.ets index a44ec0d677b039cf5a0b3f045f2df3340f5ea305..5e66d028760a01741ad61bdf89419654b3b7d0c6 100644 --- a/IntentsKitNewsUpdate/Application/entry/src/main/ets/entryability/EntryAbility.ets +++ b/IntentsKitNewsUpdate/Application/entry/src/main/ets/entryability/EntryAbility.ets @@ -18,9 +18,10 @@ import { window } from '@kit.ArkUI'; import CommonConstants from '../common/constants/CommonConstants'; import Logger from '../common/utils/Logger'; import { IIntentParam } from '../model/IntentParam'; +import { BusinessError } from '@kit.BasicServicesKit'; const TAG = '[EntryAbility]'; -let IPara: IIntentParam= { +let IPara: IIntentParam = { entityId: '', blogCategory: '', items: [] @@ -30,11 +31,16 @@ export default class EntryAbility extends UIAbility { storage: LocalStorage = new LocalStorage(IPara); onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { - this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); - if (launchParam.launchReason === AbilityConstant.LaunchReason.INSIGHT_INTENT && want && want.parameters) { - const param = (want.parameters?.['ohos.insightIntent.executeParam.param'] as IIntentParam); - Logger.showInfo(TAG, `cold launch, param is => ${JSON.stringify(param)}`); - this.storage.setOrCreate(CommonConstants.STORAGE_KEY, param); + try { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + if (launchParam.launchReason === AbilityConstant.LaunchReason.INSIGHT_INTENT && want && want.parameters) { + const param = (want.parameters?.['ohos.insightIntent.executeParam.param'] as IIntentParam); + Logger.showInfo(TAG, `cold launch, param is => ${JSON.stringify(param)}`); + this.storage.setOrCreate(CommonConstants.STORAGE_KEY, param); + } + } catch (err) { + let error = err as BusinessError; + Logger.showError(TAG, `onCreate error.code is ${error.code}, error.message is ${error.message}`); } Logger.showInfo(TAG, 'Ability onCreate'); } diff --git a/IntentsKitNewsUpdate/Application/entry/src/main/ets/pages/Index.ets b/IntentsKitNewsUpdate/Application/entry/src/main/ets/pages/Index.ets index 158a14a63ff570fdbc6e6aefbf560ce135266899..f5a3bd09ca59ba1d9f0361ff58f5e5e05779217b 100644 --- a/IntentsKitNewsUpdate/Application/entry/src/main/ets/pages/Index.ets +++ b/IntentsKitNewsUpdate/Application/entry/src/main/ets/pages/Index.ets @@ -15,6 +15,7 @@ import { common } from '@kit.AbilityKit'; import { i18n } from '@kit.LocalizationKit'; +import { BusinessError } from '@kit.BasicServicesKit'; import { News } from '../clouddb/news/News'; import newsDb from '../clouddb/news/NewsDb'; import Logger from '../common/utils/Logger'; @@ -69,26 +70,31 @@ struct Home { } aboutToAppear(): void { - const data = this.storage?.get(CommonConstants.STORAGE_KEY) as IIntentParam; - Logger.showInfo(TAG, `aboutToAppear: ${JSON.stringify(data)}`); - this.handleIntentParam(data); - - // listen intent execute event - this.abilityContext?.eventHub.on(CommonConstants.VIEW_NEWS_EVENT, (data: IIntentParam) => { - Logger.showInfo(TAG, `aboutToAppear.eventHub: ${JSON.stringify(data)}`); + try { + const data = this.storage?.get(CommonConstants.STORAGE_KEY) as IIntentParam; + Logger.showInfo(TAG, `aboutToAppear: ${JSON.stringify(data)}`); this.handleIntentParam(data); - }); - - if (this.currentTabIndex > 0) { - const newsType = this.getUIContext() - .getHostContext()?.resourceManager.getStringSync(this.newsTab[this.currentTabIndex].id) as string; - const isEn = i18n.System.getSystemLanguage() === CommonConstants.LANGUAGE_ENG; - let type = isEn ? enDict[newsType] : newsType; - newsDb.getNewsByNewsType(type).then((ret) => { - this.newsList = ret; + + // listen intent execute event + this.abilityContext?.eventHub.on(CommonConstants.VIEW_NEWS_EVENT, (data: IIntentParam) => { + Logger.showInfo(TAG, `aboutToAppear.eventHub: ${JSON.stringify(data)}`); + this.handleIntentParam(data); }); - } else { - this.newsList = this.recommendList; + + if (this.currentTabIndex > 0) { + const newsType = this.getUIContext() + .getHostContext()?.resourceManager.getStringSync(this.newsTab[this.currentTabIndex].id) as string; + const isEn = i18n.System.getSystemLanguage() === CommonConstants.LANGUAGE_ENG; + let type = isEn ? enDict[newsType] : newsType; + newsDb.getNewsByNewsType(type).then((ret) => { + this.newsList = ret; + }); + } else { + this.newsList = this.recommendList; + } + } catch (err) { + let error = err as BusinessError; + Logger.showError(TAG, `aboutToAppear, error.code is ${error.code}, error.message is ${error.message}`); } } @@ -212,7 +218,10 @@ struct Home { } @Builder - tabBuilder(text: string, index: number) { + tabBuilder(text: + string, index: + number + ) { Column() { Text(text) .fontSize(14) @@ -241,7 +250,9 @@ struct Home { }.height('100%').justifyContent(FlexAlign.Center) } - gotoViewNews(entityId: string) { + gotoViewNews(entityId: + string + ) { if (entityId) { newsDb.getNewsByNewsId(entityId).then((ret: null | News[]) => { if (!ret) { diff --git a/IntentsKitNewsUpdate/Application/entry/src/main/ets/pages/NewsDetail.ets b/IntentsKitNewsUpdate/Application/entry/src/main/ets/pages/NewsDetail.ets index 82a8cf7ea5c7d3e75a5b8695ecdfb51b2c7cde60..ede471a901d34a2d2b321a271cfad629cfd85228 100644 --- a/IntentsKitNewsUpdate/Application/entry/src/main/ets/pages/NewsDetail.ets +++ b/IntentsKitNewsUpdate/Application/entry/src/main/ets/pages/NewsDetail.ets @@ -32,6 +32,7 @@ const TAG = 'NewsDetail'; export struct NewsDetail { pathStack: NavPathStack = new NavPathStack(); @State currentNews: News | undefined = undefined; + @State pubDate: string = ''; build() { NavDestination() { @@ -70,7 +71,7 @@ export struct NewsDetail { .letterSpacing(0) .textAlign(TextAlign.Start) .align(Alignment.Start) - Text(`${formatDateTime(this.currentNews?.pubDate)} - ${this.currentNews?.pubUserName}官方帐号`) + Text(`${this.pubDate} - ${this.currentNews?.pubUserName ?? `新闻`}官方帐号`) .fontColor('rgba(0, 0, 0, 0.4)') .fontSize(10) .lineHeight(14) @@ -127,6 +128,7 @@ export struct NewsDetail { if (!this.currentNews.content) { this.currentNews = (await newsDb.getNewsByNewsId(this.currentNews.newsId.toString()) as News[])[0]; } + this.pubDate = formatDateTime(this.currentNews?.pubDate); await this.shareIntent(this.currentNews); }) } diff --git a/screenshots/client/auto_signature.jpg b/screenshots/client/auto_signature.jpg index 0282b8c5eb74d4248bc13abaa03e19f7217793ad..e16943cc25f358dcb80ddd91607bbd4722522e02 100644 Binary files a/screenshots/client/auto_signature.jpg and b/screenshots/client/auto_signature.jpg differ