From 1243bc5a63d3c28a705877b8acd8162b8e69b756 Mon Sep 17 00:00:00 2001 From: sunlian Date: Tue, 24 Sep 2024 11:27:04 +0800 Subject: [PATCH 1/4] update doc Signed-off-by: sunlian --- README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 5cd20093..1ec3b5e9 100644 --- a/README.md +++ b/README.md @@ -102,19 +102,19 @@ * 性能测试 -| 编号 | 子系统 | 测试项 | -|-----|--------|------------------| -| 1 | 应用程序框架 | 应用启动、切换 | -| 2 | 文件 | 文件IO性能 | -| 3 | 通信 | 网络传输性能(Wi-Fi) | -| 4 | 图形 | 图形显示性能 | -| 5 | 多媒体 | 音频性能 | -| 6 | 多媒体 | 视频性能 | -| 7 | 多媒体 | 相机性能 | -| 8 | 电源管理 | 功耗 | +| 编号 | 子系统 | 测试项 | +|-----|--------|----------------| +| 1 | 应用程序框架 | 应用启动、切换 | +| 2 | 文件 | 文件IO性能 | +| 3 | 通信 | 网络传输性能(Wi-Fi) | +| 4 | 图形 | 图形显示性能 | +| 5 | 多媒体 | 音频性能 | +| 6 | 多媒体 | 视频性能 | +| 7 | 多媒体 | 相机性能 | +| 8 | 电源管理 | 功耗 | | 9 | ArkUI | ArkUI组件benchmark | | 10 | ArkTS | ArkTS语言benchmark | -| 11 | <综合> | 游戏性能(CPU,GPU,IO) | +| 11 | <综合> | 性能(CPU,GPU,IO) | 详细测试项参考各模块说明。 -- Gitee From b647918790da596afc89d5f6c32ac89411ea9eed Mon Sep 17 00:00:00 2001 From: sunlian Date: Wed, 25 Sep 2024 15:00:19 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E9=A6=96=E9=A1=B5=E6=8E=A8=E8=8D=90?= =?UTF-8?q?=E7=83=AD=E9=97=A8=E6=AD=8C=E6=89=8B=EF=BC=8Chttp=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: sunlian --- scenario/MusicPlayerOnline/README_zh.md | 3 +- .../main/ets/constants/ServerConstants.ets | 44 +- .../src/main/ets/manager/HttpManager.ets | 704 ++++++++++++++++++ .../src/main/ets/manager/PlayerManager.ets | 194 +---- .../entry/src/main/ets/pages/Index.ets | 651 +++++----------- .../entry/src/main/ets/view/PlayList.ets | 4 +- .../entry/src/main/ets/view/PlayListItem.ets | 2 +- .../entry/src/main/ets/view/PlayerBar.ets | 7 +- .../entry/src/main/ets/view/PlayerDetail.ets | 21 +- .../src/main/ets/view/addToListDialog.ets | 2 +- .../main/resources/base/element/string.json | 4 + .../main/resources/en_US/element/string.json | 4 + .../main/resources/zh_CN/element/string.json | 4 + 13 files changed, 983 insertions(+), 661 deletions(-) create mode 100644 scenario/MusicPlayerOnline/entry/src/main/ets/manager/HttpManager.ets diff --git a/scenario/MusicPlayerOnline/README_zh.md b/scenario/MusicPlayerOnline/README_zh.md index dbea8b08..33f7ff40 100644 --- a/scenario/MusicPlayerOnline/README_zh.md +++ b/scenario/MusicPlayerOnline/README_zh.md @@ -90,7 +90,7 @@ 10. 边听边存、离线播放 11. 歌词 12. 桌面播放卡片 - 13. 歌曲分类及搜索 + 13. 歌曲分类: 热门歌手 (已完成) 14. 其他平台歌曲推荐、播放 15. 音频焦点处理 16. 其他界面交互效果(切换动画、点击效果) @@ -101,6 +101,7 @@ 19. 性能优化:加载大量歌单和歌曲的列表(1000以上容易appfreeze),应用优化+服务器控制数据量 20. 一多适配:平板、pc 21. 异常处理:检测网络状态、播放器异常等 + 22. 播放状态保存恢复:服务器记录,上次播放的歌曲和歌单,登录或重启后恢复 #### 未计划功能: 1. 社交(用户信息,好友,分享,评论) diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/constants/ServerConstants.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/constants/ServerConstants.ets index a60181d1..5e158049 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/constants/ServerConstants.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/constants/ServerConstants.ets @@ -33,6 +33,10 @@ export default class ServerConstants { * search song */ static readonly SEARCH_SONG_URL = this.SERVER_HOST + 'search_song/'; + /** + * singer list + */ + static readonly SINGER_LIST_URL = this.SERVER_HOST + 'singer_list/'; /** * recommend list URL */ @@ -93,23 +97,41 @@ export default class ServerConstants { static readonly PLAYER_STATE_UNKNOWN = 'unknown'; /** - * PLAYER UPDATE + * 首页播放状态更新消息 + */ + static readonly MAIN_UPDATE_TIME = '1'; + static readonly MAIN_UPDATE_STATE = '2'; + /** + * 详情页播放状态更新消息 */ - static readonly UPDATE_TIME_EVENT_ID = '1'; - static readonly UPDATE_STATE_EVENT_ID = '2'; - static readonly UPDATE_TIME_EVENT_ID_DETAIL = '4'; - static readonly UPDATE_STATE_EVENT_ID_DETAIL = '5'; - static readonly UPDATE_FAVOR_EVENT_ID_DETAIL = '6'; - static readonly UPDATE_USER_LIST = '3'; - static readonly UPDATE_USER_LIST_CUSTOM = '31'; - static readonly UPDATE_USER_LIST_RECENT = '32'; - static readonly UPDATE_USER_LIST_FAVOR = '33'; + static readonly DETAIL_UPDATE_TIME = '3'; + static readonly DETAIL_UPDATE_STATE = '4'; + static readonly DETAIL_UPDATE_FAVOR = '5'; + /** + * 播放列表播放状态更新消息 + */ + static readonly PLAYLIST_UPDATE_STATE = '6'; + + //首页列表更新消息 + static readonly UPDATE_USER_LIST = '8'; + static readonly UPDATE_USER_LIST_RECOMMEND = '80'; + static readonly UPDATE_USER_LIST_CUSTOM = '81'; + static readonly UPDATE_USER_LIST_RECENT = '82'; + static readonly UPDATE_USER_LIST_FAVOR = '83'; + static readonly UPDATE_USER_LIST_SINGER = '84'; + static readonly UPDATE_USER_LIST_SEARCH = '85'; + + //首页登录更新消息 + static readonly LOGIN_RESULT = '9' + static readonly LOGIN_RESULT_SUCCESS = '90' + static readonly LOGIN_RESULT_FAILED = '91' /** * PLAYER LIST */ static readonly LIST_SONG_COUNT = '首'; static readonly LIST_SONG_NO_PLAYING = '没有播放'; static readonly LIST_SONG_FAVOURITE = '我的收藏'; + static readonly LIST_SONG_RECENT = '最近播放'; /** * PLAYER LOGIN */ @@ -126,7 +148,7 @@ export default class ServerConstants { static readonly CANCEL_FAVOURITE_SUCCESS = "取消收藏成功"; static readonly CANCEL_FAVOURITE_ALREADY = "重复取消收藏"; static readonly SEARCH_DEFAULT = '未搜索'; - static readonly SEARCHING = '搜索中'; + static readonly SEARCHING = '搜索:'; static readonly EMPTY_SEARCH_WORD = '不能搜索空字符'; static readonly LOGIN_FAILED = 'login failed!'; static readonly REGISTER_FAILED = 'signup failed!'; diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/manager/HttpManager.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/manager/HttpManager.ets new file mode 100644 index 00000000..48da19fb --- /dev/null +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/manager/HttpManager.ets @@ -0,0 +1,704 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import http from '@ohos.net.http' +import ServerConstants from '../constants/ServerConstants'; +import emitter from '@ohos.events.emitter'; +import PlayerManager from './PlayerManager'; +import prompt from '@ohos.promptAction'; +import AudioItem from '../model/AudioItem'; +import PlayListData from '../model/PlayListData'; + +// 单例对象 HttpManager +export default class HttpManager { + private static instance: HttpManager; + private PlayerManager: PlayerManager = AppStorage.get('PlayerManager') as PlayerManager; + private httpRequest = http.createHttp(); + private csrfToken: string = ''; + private emitterOptions: emitter.Options = { + priority: emitter.EventPriority.HIGH + }; + + private constructor() { + } + + public static getInstance(): HttpManager { + if (!HttpManager.instance) { + HttpManager.instance = new HttpManager(); + } + return HttpManager.instance; + } + + //注册 + register(userName: string, pwd: string, pwd2: string) { + if (userName.trim() === '' || pwd.trim() === '') { + prompt.showToast({ + message: '用户名和密码不能为空!' + }) + return; + } + if (pwd !== pwd2) { + prompt.showToast({ + message: '两次密码输入不同!' + }) + return; + } + try { + this.httpRequest.request(ServerConstants.CSRF_URL, + (err: Error, data: http.HttpResponse) => { + if (!err) { + console.info('HttpResponse Result:' + data.result); + try { + const jsonObject: object = JSON.parse(data.result as string); + this.csrfToken = jsonObject['csrf_token']; + this.httpRequest.request(ServerConstants.REGISTER_URL, + { + method: http.RequestMethod.POST, + extraData: 'username=' + userName.trim() + '&password=' + pwd.trim(), + header: { + 'content-Type': 'application/x-www-form-urlencoded', + 'X-CSRFToken': this.csrfToken, + 'Cookie': 'csrftoken=' + this.csrfToken + ';' + }, + }, + (err: Error, data: http.HttpResponse) => { + if (!err) { + console.info('HttpResponse Result:' + data.result); + if ((data.result as string).indexOf(ServerConstants.REGISTER_FAILED) !== -1) { + prompt.showToast({ + message: ServerConstants.REGISTER_INFO_FAILED + data.result + }) + return; + } + prompt.showToast({ + message: ServerConstants.REGISTER_INFO_SUCCESS + }) + this.login(userName.trim(), pwd.trim()); + } else { + console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: ServerConstants.REGISTER_INFO_FAILED + }) + } + }); + } catch (err) { + console.info('HttpRequest error:' + JSON.stringify(err)); + prompt.showToast({ + message: ServerConstants.REGISTER_INFO_FAILED + }) + } + } else { + console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: ServerConstants.REGISTER_INFO_FAILED + }) + } + }); + } catch (err) { + console.info('HttpRequest error:' + JSON.stringify(err)); + prompt.showToast({ + message: ServerConstants.REGISTER_INFO_FAILED + }) + } + } + + //登录 + login(userName: string, pwd: string) { + try { + this.httpRequest.request(ServerConstants.CSRF_URL, + (err: Error, data: http.HttpResponse) => { + if (!err) { + console.info('HttpResponse Result:' + data.result); + try { + const jsonObject: object = JSON.parse(data.result as string); + this.csrfToken = jsonObject['csrf_token']; + this.httpRequest.request(ServerConstants.LOGIN_URL, + { + method: http.RequestMethod.POST, + extraData: 'username=' + userName + '&password=' + pwd, + header: { + 'content-Type': 'application/x-www-form-urlencoded', + 'X-CSRFToken': this.csrfToken, + 'Cookie': 'csrftoken=' + this.csrfToken + ';' + }, + }, + (err: Error, data: http.HttpResponse) => { + if (!err) { + console.info('HttpResponse Result:' + data.result); + console.info('HttpResponse code:' + data.responseCode); + console.info('HttpResponse type:' + JSON.stringify(data.resultType)); + console.info('HttpResponse header:' + JSON.stringify(data.header)); + if (200 === data.responseCode) { + console.info('HttpResponse code:' + (data.result as string).indexOf(ServerConstants.LOGIN_FAILED)); + if ((data.result as string).indexOf(ServerConstants.LOGIN_FAILED) !== -1) { + this.loginFailed(); + return; + } + + AppStorage.setOrCreate('loginName', userName); + let loginCookie: string[] = data.header['set-cookie']; + let cookie: string = loginCookie.map(cookie => { + const parts = cookie.split('='); + return `${parts[0]}=${parts[1]}`; + }).join('; ') + AppStorage.setOrCreate('loginCookie', cookie); + + let eventData: emitter.EventData = { + data: { + "message": ServerConstants.LOGIN_RESULT_SUCCESS, + } + }; + emitter.emit(ServerConstants.LOGIN_RESULT, this.emitterOptions, eventData); + //this.getRecommendListFromServer(); + this.getCustomListFromServer(); + } + } else { + console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) + this.loginFailed(); + } + }); + } + catch (error) { + console.error("JSON :", error.message); + this.loginFailed(); + } + } else { + console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) + this.loginFailed(); + } + }); + } catch (err) { + console.info('HttpRequest error:' + JSON.stringify(err)); + this.loginFailed(); + } + } + + loginFailed() { + let eventData: emitter.EventData = { + data: { + "message": ServerConstants.LOGIN_RESULT_FAILED, + } + }; + emitter.emit(ServerConstants.LOGIN_RESULT, this.emitterOptions, eventData); + prompt.showToast({ + message: ServerConstants.LOGIN_INFO_FAILED + }) + } + + //记录最近播放信息,并获取歌曲格式 + record_recent() { + let loginCookie: string | undefined = AppStorage.get('loginCookie'); + if (loginCookie === '' || loginCookie === undefined) { + return; + } + try { + this.httpRequest.request(ServerConstants.RECORD_RECENT + this.PlayerManager.getItem().id, + { + method: http.RequestMethod.GET, + header: { + 'Cookie': loginCookie + } + }, + (err: Error, data: http.HttpResponse) => { + if (!err) { + console.info('HttpResponse record_recent Result:' + data.result); + console.info('HttpResponse code:' + data.responseCode); + console.info('HttpResponse type:' + JSON.stringify(data.resultType)); + console.info('HttpResponse header:' + JSON.stringify(data.header)); + console.info('HttpResponse cookies:' + data.cookies); + try { + const jsonObject: object = JSON.parse(data.result as string); + this.PlayerManager.setItemQuality(jsonObject['message']); + emitter.emit(ServerConstants.DETAIL_UPDATE_FAVOR, this.emitterOptions); + } catch (err) { + console.info('HttpResponse error:' + JSON.stringify(err)); + } + this.getRecentListFromServer(); + } else { + console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) + } + }); + } catch (err) { + console.info('HttpRequest error:' + JSON.stringify(err)); + } + } + + //设置收藏 + set_favourite() { + let loginCookie: string | undefined = AppStorage.get('loginCookie'); + let msg: string = '' + try { + let item = this.PlayerManager.getItem(); + let is_favor: string = item.isFavor ? '0' : '1'; + console.info('HttpResponse start:' + ServerConstants.SET_FAVOURITE + item.id + '/' + is_favor); + this.httpRequest.request(ServerConstants.SET_FAVOURITE + item.id + '/' + is_favor, + { + method: http.RequestMethod.GET, + header: { + 'Cookie': loginCookie + } + }, + (err: Error, data: http.HttpResponse) => { + if (!err) { + console.info('HttpResponse Result:' + data.result); + console.info('HttpResponse code:' + data.responseCode); + console.info('HttpResponse type:' + JSON.stringify(data.resultType)); + console.info('HttpResponse header:' + JSON.stringify(data.header)); + console.info('HttpResponse cookies:' + data.cookies); + try { + const jsonObject: object = JSON.parse(data.result as string); + switch (jsonObject["message"]) { + case ServerConstants.SET_FAVOURITE_ALREADY: + case ServerConstants.SET_FAVOURITE_SUCCESS: + item.isFavor = true; + break; + case ServerConstants.CANCEL_FAVOURITE_SUCCESS: + case ServerConstants.CANCEL_FAVOURITE_ALREADY: + item.isFavor = false; + break; + default: + item.isFavor = false; + break; + } + this.PlayerManager.setItemFavor(item.isFavor); + emitter.emit(ServerConstants.DETAIL_UPDATE_FAVOR, this.emitterOptions); + prompt.showToast({ + message: jsonObject["message"] + }) + this.getFavorListFromServer(); + } + catch (error) { + console.error("JSON :", error.message); + msg = JSON.stringify(error); + } + } else { + console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) + } + }); + } catch (err) { + console.info('HttpRequest error:' + JSON.stringify(err)); + msg = JSON.stringify(err); + } + if (msg !== '') { + prompt.showToast({ + message: msg + }) + } + } + + + add_to_playlist(song_id: string, playlist_name: string) { + let loginCookie: string | undefined = AppStorage.get('loginCookie'); + let msg: string = '' + try { + console.info('HttpResponse start:' + ServerConstants.ADD_TO_PLAYLIST + song_id + '/' + playlist_name); + this.httpRequest.request(ServerConstants.ADD_TO_PLAYLIST + song_id + '/' + playlist_name, + { + method: http.RequestMethod.GET, + header: { + 'Cookie': loginCookie + } + }, + (err: Error, data: http.HttpResponse) => { + if (!err) { + console.info('HttpResponse Result:' + data.result); + console.info('HttpResponse code:' + data.responseCode); + console.info('HttpResponse type:' + JSON.stringify(data.resultType)); + console.info('HttpResponse header:' + JSON.stringify(data.header)); + console.info('HttpResponse cookies:' + data.cookies); + try { + const jsonObject: object = JSON.parse(data.result as string); + console.info('HttpResponse msg:' + jsonObject["message"]); + let eventData: emitter.EventData = { + data: { + "message": ServerConstants.UPDATE_USER_LIST_CUSTOM, + } + }; + emitter.emit(ServerConstants.UPDATE_USER_LIST, this.emitterOptions, eventData); + prompt.showToast({ + message: jsonObject["message"] + }) + } catch (error) { + console.error("JSON :", error.message); + msg = JSON.stringify(error); + } + } else { + console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) + } + }); + } catch (err) { + console.info('HttpRequest error:' + JSON.stringify(err)); + msg = JSON.stringify(err); + } + console.info('HttpRequest msg:' + msg); + if (msg !== '') { + prompt.showToast({ + message: msg + }) + } + } + + getRecommendListFromServer() { + try { + this.httpRequest.request(ServerConstants.RECOMMEND_LIST_URL, + { + method: http.RequestMethod.GET, + header: { + 'Cookie': AppStorage.get('loginCookie') + } + }, + (err: Error, data: http.HttpResponse) => { + if (!err) { + console.info('HttpResponse getRecommendListFromServer Result:' + data.result); + try { + let isStart = true; + const jsonObject: object = JSON.parse(data.result as string); + Object.keys(jsonObject).forEach((key) => { + let aPlayingList: AudioItem[] = Array(); + Object.keys(jsonObject[key]).forEach((key2) => { + aPlayingList.push(new AudioItem(jsonObject[key][key2].name, jsonObject[key][key2].singer, jsonObject[key][key2].id)); + }) + let item = new PlayListData(key, $r('app.media.icon'), aPlayingList.length + ServerConstants.LIST_SONG_COUNT, + aPlayingList, '') + let eventData: emitter.EventData = { + data: { + "message": ServerConstants.UPDATE_USER_LIST_RECOMMEND, + "item": item, + "isStart": isStart, + } + }; + emitter.emit(ServerConstants.UPDATE_USER_LIST, this.emitterOptions, eventData); + isStart = false; + }); + } + catch (error) { + console.error("JSON :", error.message); + } + this.getSingerListFromServer(); + console.info('HttpResponse code:' + data.responseCode); + console.info('HttpResponse type:' + JSON.stringify(data.resultType)); + console.info('HttpResponse header:' + JSON.stringify(data.header)); + console.info('HttpResponse cookies:' + data.cookies); + } else { + console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) + } + }); + } catch (err) { + console.info('HttpRequest error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) + } + } + + getSingerListFromServer() { + try { + this.httpRequest.request(ServerConstants.SINGER_LIST_URL, + { + method: http.RequestMethod.GET, + header: { + 'Cookie': AppStorage.get('loginCookie') + } + }, + (err: Error, data: http.HttpResponse) => { + if (!err) { + console.info('HttpResponse getSingerListFromServer Result:' + data.result); + try { + const jsonObject: object = JSON.parse(data.result as string); + let isStart = true; + Object.keys(jsonObject).forEach((key) => { + let aPlayingList: AudioItem[] = Array(); + Object.keys(jsonObject[key]).forEach((key2) => { + aPlayingList.push(new AudioItem(jsonObject[key][key2].name, jsonObject[key][key2].singer, jsonObject[key][key2].id)); + }); + let item = new PlayListData(key, $r('app.media.icon'), aPlayingList.length + ServerConstants.LIST_SONG_COUNT, + aPlayingList, '') + let eventData: emitter.EventData = { + data: { + "message": ServerConstants.UPDATE_USER_LIST_SINGER, + "item": item, + "isStart": isStart, + } + }; + emitter.emit(ServerConstants.UPDATE_USER_LIST, this.emitterOptions, eventData); + isStart = false; + }); + } + catch (error) { + console.error("JSON :", error.message); + } + console.info('HttpResponse code:' + data.responseCode); + console.info('HttpResponse type:' + JSON.stringify(data.resultType)); + console.info('HttpResponse header:' + JSON.stringify(data.header)); + console.info('HttpResponse cookies:' + data.cookies); + } else { + console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) + } + }); + } catch (err) { + console.info('HttpRequest error:' + JSON.stringify(err)); + } + } + + getFavorListFromServer() { + try { + this.httpRequest.request(ServerConstants.FAVOR_LIST_URL, + { + method: http.RequestMethod.GET, + header: { + 'Cookie': AppStorage.get('loginCookie') + } + }, + (err: Error, data: http.HttpResponse) => { + if (!err) { + console.info('HttpResponse getFavorListFromServer Result:' + data.result); + try { + let aPlayingList: AudioItem[] = Array(); + const jsonObject: object = JSON.parse(data.result as string); + let item: PlayListData | undefined = undefined; + if (jsonObject[ServerConstants.LIST_SONG_FAVOURITE]) { + Object.keys(jsonObject[ServerConstants.LIST_SONG_FAVOURITE]).forEach((key) => { + aPlayingList.push(new AudioItem(jsonObject[ServerConstants.LIST_SONG_FAVOURITE][key].name, jsonObject[ServerConstants.LIST_SONG_FAVOURITE][key].singer, jsonObject[ServerConstants.LIST_SONG_FAVOURITE][key].id)); + }); + item = new PlayListData(ServerConstants.LIST_SONG_FAVOURITE, $r('app.media.icon'), aPlayingList.length + ServerConstants.LIST_SONG_COUNT, + aPlayingList, '') + } + this.PlayerManager.setFavourList(aPlayingList); + let eventData: emitter.EventData = { + data: { + "message": ServerConstants.UPDATE_USER_LIST_FAVOR, + "item": item, + } + }; + emitter.emit(ServerConstants.UPDATE_USER_LIST, this.emitterOptions, eventData); + } + catch (error) { + console.error("JSON :", error.message); + } + console.info('HttpResponse code:' + data.responseCode); + console.info('HttpResponse type:' + JSON.stringify(data.resultType)); + console.info('HttpResponse header:' + JSON.stringify(data.header)); + console.info('HttpResponse cookies:' + data.cookies); + } else { + console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) + } + }); + } catch (err) { + console.info('HttpRequest error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) + } + } + + getRecentListFromServer() { + try { + this.httpRequest.request(ServerConstants.RECENT_LIST_URL, + { + method: http.RequestMethod.GET, + header: { + 'Cookie': AppStorage.get('loginCookie') + } + }, + (err: Error, data: http.HttpResponse) => { + if (!err) { + console.info('HttpResponse getRecentListFromServer Result:' + data.result); + try { + const jsonObject: object = JSON.parse(data.result as string); + let isStart = true; + Object.keys(jsonObject).forEach((key) => { + let aPlayingList: AudioItem[] = Array(); + Object.keys(jsonObject[key]).forEach((key2) => { + aPlayingList.push(new AudioItem(jsonObject[key][key2].name, jsonObject[key][key2].singer, jsonObject[key][key2].id)); + }); + + let item = new PlayListData(key, $r('app.media.icon'), aPlayingList.length + ServerConstants.LIST_SONG_COUNT, + aPlayingList, ''); + let eventData: emitter.EventData = { + data: { + "message": ServerConstants.UPDATE_USER_LIST_RECENT, + "item": item, + "isStart": isStart, + } + }; + emitter.emit(ServerConstants.UPDATE_USER_LIST, this.emitterOptions, eventData); + isStart = false; + }); + } + catch (error) { + console.error("JSON :", error.message); + } + console.info('HttpResponse code:' + data.responseCode); + console.info('HttpResponse type:' + JSON.stringify(data.resultType)); + console.info('HttpResponse header:' + JSON.stringify(data.header)); + console.info('HttpResponse cookies:' + data.cookies); + } else { + console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) + } + }); + } catch (err) { + console.info('HttpRequest error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) + } + } + + getCustomListFromServer() { + try { + this.httpRequest.request(ServerConstants.CUSTOM_LIST_URL, + { + method: http.RequestMethod.GET, + header: { + 'Cookie': AppStorage.get('loginCookie') + } + }, + (err: Error, data: http.HttpResponse) => { + if (!err) { + console.info('HttpResponse getCustomListFromServer Result:' + data.result); + try { + interface PlaylistItem { + playlist_name: string; + song: number; + id: number; + name: string; + singer: string; + language: string; + } + + let jsonObject: PlaylistItem[] = JSON.parse(data.result as string); + let groupedByPlaylistName = jsonObject.reduce((accumulator, item) => { + if (!accumulator[item.playlist_name]) { + accumulator[item.playlist_name] = []; + } + accumulator[item.playlist_name].push(item); + return accumulator; + }, {} as Record); + const keys = Object.keys(groupedByPlaylistName); + let isStart = true; + keys.forEach((playlistName) => { + console.log(`Playlist: ${playlistName}`); + let aPlayingList: AudioItem[] = Array(); + groupedByPlaylistName[playlistName].forEach((song, index) => { + aPlayingList.push(new AudioItem(song.name, song.singer, song.id.toString())); + }); + + let item = new PlayListData(playlistName, $r('app.media.icon'), aPlayingList.length + ServerConstants.LIST_SONG_COUNT, + aPlayingList, '') + let eventData: emitter.EventData = { + data: { + "message": ServerConstants.UPDATE_USER_LIST_CUSTOM, + "item": item, + "isStart": isStart, + } + }; + emitter.emit(ServerConstants.UPDATE_USER_LIST, this.emitterOptions, eventData); + isStart = false; + }); + this.getRecentListFromServer(); + this.getFavorListFromServer(); + } + catch (error) { + console.error("JSON :", error.message); + } + } else { + console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) + } + }); + } catch (err) { + console.info('HttpRequest error:' + JSON.stringify(err)); + } + } + + searchSongsFromServer(search_word: string) { + try { + this.httpRequest.request(ServerConstants.SEARCH_SONG_URL + search_word, + { + method: http.RequestMethod.GET, + header: { + 'Cookie': AppStorage.get('loginCookie') + } + }, + (err: Error, data: http.HttpResponse) => { + if (!err) { + console.info('HttpResponse searchSongsFromServer Result:' + data.result); + try { + const jsonObject: object = JSON.parse(data.result as string); + let isStart = true; + Object.keys(jsonObject).forEach((key) => { + Object.keys(jsonObject[key]).forEach((key2) => { + let aPlayingList: AudioItem[] = Array(); + let aAudioItem = new AudioItem(jsonObject[key][key2].name, jsonObject[key][key2].singer, jsonObject[key][key2].id) + aPlayingList.push(aAudioItem); + let item = new PlayListData(jsonObject[key][key2].name, $r('app.media.icon'), '', aPlayingList, '') + let eventData: emitter.EventData = { + data: { + "message": ServerConstants.UPDATE_USER_LIST_SEARCH, + "item": item, + "isStart": isStart, + } + }; + emitter.emit(ServerConstants.UPDATE_USER_LIST, this.emitterOptions, eventData); + isStart = false; + }); + }); + } catch (error) { + console.error("JSON :", error.message); + } + console.info('HttpResponse code:' + data.responseCode); + console.info('HttpResponse type:' + JSON.stringify(data.resultType)); + console.info('HttpResponse header:' + JSON.stringify(data.header)); + console.info('HttpResponse cookies:' + data.cookies); + } else { + console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) + } + }); + } catch (err) { + console.info('HttpRequest error:' + JSON.stringify(err)); + } + } +} diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/manager/PlayerManager.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/manager/PlayerManager.ets index 79f3c75c..7e65f9b7 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/manager/PlayerManager.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/manager/PlayerManager.ets @@ -23,8 +23,7 @@ import { PLAY_MODE } from '../constants/PlayMode'; import backgroundTaskManager from '@ohos.resourceschedule.backgroundTaskManager'; import wantAgent, { WantAgent } from '@ohos.app.ability.wantAgent'; import common from '@ohos.app.ability.common'; -import http from '@ohos.net.http' -import prompt from '@ohos.promptAction'; +import HttpManager from './HttpManager'; export default class PlayerManager { @@ -39,7 +38,6 @@ export default class PlayerManager { private listPosition: number = 0; private state: string = ServerConstants.PLAYER_STATE_UNKNOWN; private listTitle: string = ServerConstants.LIST_SONG_NO_PLAYING; - private httpRequest = http.createHttp(); private playMode: number | undefined = PLAY_MODE.REPEAT; private shuffleIndex: number[] = []; private emitterOptions: emitter.Options = { @@ -71,8 +69,8 @@ export default class PlayerManager { "currentDuration": this.currentDuration } }; - emitter.emit(ServerConstants.UPDATE_TIME_EVENT_ID, this.emitterOptions, eventData); - emitter.emit(ServerConstants.UPDATE_TIME_EVENT_ID_DETAIL, this.emitterOptions, eventData); + emitter.emit(ServerConstants.MAIN_UPDATE_TIME, this.emitterOptions, eventData); + emitter.emit(ServerConstants.DETAIL_UPDATE_TIME, this.emitterOptions, eventData); }) avPlayer.on('durationUpdate', (time: number) => { console.info('PlayerManager state durationUpdate:' + time); @@ -88,8 +86,9 @@ export default class PlayerManager { "state": state, } }; - emitter.emit(ServerConstants.UPDATE_STATE_EVENT_ID, this.emitterOptions, eventData); - emitter.emit(ServerConstants.UPDATE_STATE_EVENT_ID_DETAIL, this.emitterOptions, eventData); + emitter.emit(ServerConstants.MAIN_UPDATE_STATE, this.emitterOptions, eventData); + emitter.emit(ServerConstants.DETAIL_UPDATE_STATE, this.emitterOptions, eventData); + emitter.emit(ServerConstants.PLAYLIST_UPDATE_STATE, this.emitterOptions, eventData); switch (state) { case ServerConstants.PLAYER_STATE_IDLE: // 成功调用reset接口后触发该状态机上报 console.info('PlayerManager state idle called.'); @@ -106,7 +105,7 @@ export default class PlayerManager { case ServerConstants.PLAYER_STATE_PLAYING: // play成功调用后触发该状态机上报 console.info('PlayerManager state playing called.'); this.list[this.listPosition].isPlaying = true; - this.record_recent(); + HttpManager.getInstance().record_recent(); break; case ServerConstants.PLAYER_STATE_PAUSED: // pause成功调用后触发该状态机上报 console.info('PlayerManager state paused called.'); @@ -320,6 +319,14 @@ export default class PlayerManager { return this.item; } + setItemQuality(quality: string) { + this.item.quality = quality; + } + + setItemFavor(isFavor: boolean) { + this.item.isFavor = isFavor; + } + getListTitle(): string { return this.listTitle; } @@ -358,173 +365,7 @@ export default class PlayerManager { this.favourList = list; } - record_recent() { - let loginCookie: string | undefined = AppStorage.get('loginCookie'); - if (loginCookie === '' || loginCookie === undefined) { - return; - } - try { - this.httpRequest.request(ServerConstants.RECORD_RECENT + this.item.id, - { - method: http.RequestMethod.GET, - header: { - 'Cookie': loginCookie - } - }, - (err: Error, data: http.HttpResponse) => { - if (!err) { - console.info('HttpResponse Result:' + data.result); - console.info('HttpResponse code:' + data.responseCode); - console.info('HttpResponse type:' + JSON.stringify(data.resultType)); - console.info('HttpResponse header:' + JSON.stringify(data.header)); - console.info('HttpResponse cookies:' + data.cookies); - try { - const jsonObject: object = JSON.parse(data.result as string); - this.item.quality = jsonObject['message']; - emitter.emit(ServerConstants.UPDATE_FAVOR_EVENT_ID_DETAIL, this.emitterOptions); - } catch (err) { - console.info('HttpResponse error:' + JSON.stringify(err)); - } - let eventData: emitter.EventData = { - data: { - "message": ServerConstants.UPDATE_USER_LIST_RECENT, - } - }; - emitter.emit(ServerConstants.UPDATE_USER_LIST, this.emitterOptions, eventData); - } else { - console.info('HttpResponse error:' + JSON.stringify(err)); - prompt.showToast({ - message: JSON.stringify(err) - }) - } - }); - } catch (err) { - console.info('HttpRequest error:' + JSON.stringify(err)); - } - } - - set_favourite() { - let loginCookie: string | undefined = AppStorage.get('loginCookie'); - let msg: string = '' - try { - let is_favor: string = this.item.isFavor ? '0' : '1'; - console.info('HttpResponse start:' + ServerConstants.SET_FAVOURITE + this.item.id + '/' + is_favor); - this.httpRequest.request(ServerConstants.SET_FAVOURITE + this.item.id + '/' + is_favor, - { - method: http.RequestMethod.GET, - header: { - 'Cookie': loginCookie - } - }, - (err: Error, data: http.HttpResponse) => { - if (!err) { - console.info('HttpResponse Result:' + data.result); - console.info('HttpResponse code:' + data.responseCode); - console.info('HttpResponse type:' + JSON.stringify(data.resultType)); - console.info('HttpResponse header:' + JSON.stringify(data.header)); - console.info('HttpResponse cookies:' + data.cookies); - try { - const jsonObject: object = JSON.parse(data.result as string); - switch (jsonObject["message"]) { - case ServerConstants.SET_FAVOURITE_ALREADY: - case ServerConstants.SET_FAVOURITE_SUCCESS: - this.item.isFavor = true; - break; - case ServerConstants.CANCEL_FAVOURITE_SUCCESS: - case ServerConstants.CANCEL_FAVOURITE_ALREADY: - this.item.isFavor = false; - break; - default: - this.item.isFavor = false; - break; - } - emitter.emit(ServerConstants.UPDATE_FAVOR_EVENT_ID_DETAIL, this.emitterOptions); - prompt.showToast({ - message: jsonObject["message"] - }) - let eventData: emitter.EventData = { - data: { - "message": ServerConstants.UPDATE_USER_LIST_FAVOR, - } - }; - emitter.emit(ServerConstants.UPDATE_USER_LIST, this.emitterOptions, eventData); - } - catch (error) { - console.error("JSON :", error.message); - msg = JSON.stringify(error); - } - } else { - console.info('HttpResponse error:' + JSON.stringify(err)); - prompt.showToast({ - message: JSON.stringify(err) - }) - } - }); - } catch (err) { - console.info('HttpRequest error:' + JSON.stringify(err)); - msg = JSON.stringify(err); - } - if (msg !== '') { - prompt.showToast({ - message: msg - }) - } - } - - add_to_playlist(song_id: string, playlist_name: string) { - let loginCookie: string | undefined = AppStorage.get('loginCookie'); - let msg: string = '' - try { - console.info('HttpResponse start:' + ServerConstants.ADD_TO_PLAYLIST + song_id + '/' + playlist_name); - this.httpRequest.request(ServerConstants.ADD_TO_PLAYLIST + song_id + '/' + playlist_name, - { - method: http.RequestMethod.GET, - header: { - 'Cookie': loginCookie - } - }, - (err: Error, data: http.HttpResponse) => { - if (!err) { - console.info('HttpResponse Result:' + data.result); - console.info('HttpResponse code:' + data.responseCode); - console.info('HttpResponse type:' + JSON.stringify(data.resultType)); - console.info('HttpResponse header:' + JSON.stringify(data.header)); - console.info('HttpResponse cookies:' + data.cookies); - try { - const jsonObject: object = JSON.parse(data.result as string); - console.info('HttpResponse msg:' + jsonObject["message"]); - let eventData: emitter.EventData = { - data: { - "message": ServerConstants.UPDATE_USER_LIST_CUSTOM, - } - }; - emitter.emit(ServerConstants.UPDATE_USER_LIST, this.emitterOptions, eventData); - prompt.showToast({ - message: jsonObject["message"] - }) - } catch (error) { - console.error("JSON :", error.message); - msg = JSON.stringify(error); - } - } else { - console.info('HttpResponse error:' + JSON.stringify(err)); - prompt.showToast({ - message: JSON.stringify(err) - }) - } - }); - } catch (err) { - console.info('HttpRequest error:' + JSON.stringify(err)); - msg = JSON.stringify(err); - } - console.info('HttpRequest msg:' + msg); - if (msg !== '') { - prompt.showToast({ - message: msg - }) - } - } - + //设置后台任务 startContinuousTask() { let wantAgentInfo: wantAgent.WantAgentInfo = { wants: [ @@ -556,7 +397,7 @@ export default class PlayerManager { } } - // cancel continuous task + //取消后台任务 stopContinuousTask(): void { try { backgroundTaskManager.stopBackgroundRunning(AppStorage.get('APPContext') as common.UIAbilityContext).then(() => { @@ -569,6 +410,7 @@ export default class PlayerManager { } } + //退出app async exitAPP() { try { if (this.avPlayer !== undefined) { diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/pages/Index.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/pages/Index.ets index fa80dcd1..415c316c 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/pages/Index.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/pages/Index.ets @@ -16,10 +16,8 @@ import PlayerBar from '../view/PlayerBar'; import PlayList from '../view/PlayList'; import PlayListItem from '../view/PlayListItem'; -import http from '@ohos.net.http' import ServerConstants from '../constants/ServerConstants'; import UIConstants from '../constants/UIConstants'; -import AudioItem from '../model/AudioItem'; import PlayListData from '../model/PlayListData'; import PlayerDetail from '../view/PlayerDetail'; import emitter from '@ohos.events.emitter'; @@ -27,6 +25,7 @@ import prompt from '@ohos.promptAction'; import PlayerManager from '../manager/PlayerManager'; import Setting from '../view/Setting'; import { LoginCustomDialog } from '../view/loginDialog'; +import HttpManager from '../manager/HttpManager'; PersistentStorage.persistProp('loginCookie', ''); PersistentStorage.persistProp('loginName', ServerConstants.LOGIN_INFO_NO); @@ -38,10 +37,12 @@ struct Index { @State recommendPlayLists: PlayListData[] = []; @State customPlayLists: PlayListData[] = []; @State searchResultPlayLists: PlayListData[] = []; + @State singerList: PlayListData[] = []; @State searchSongTitle: string = ServerConstants.SEARCH_DEFAULT; @State isShowPlayerDetail: boolean = false; @State isShowPlayList: boolean = false; @State isRefreshing: boolean = false; + @State isShowSingerList: boolean = false; @State mOffset: number = 0; @State mFriction: number = 48; @State maskOpacity: number = 0.5; @@ -49,12 +50,12 @@ struct Index { @State loginButton: string = ServerConstants.LOGIN_BUTTON_IN; @State rowAngle: number = 0; @State colAngle: number = 0; + @State SingerAngle: number = 0; @State isShowSidebar: boolean = false; - private httpRequest = http.createHttp(); - private csrfToken: string = ''; private rowScroller: Scroller = new Scroller(); private colScroller: Scroller = new Scroller() private rootScroller: Scroller = new Scroller() + private singerListClickable: boolean = true; @State inputUserName: string = '' @State inputPwd: string = '' @State inputPwd2: string = '' @@ -63,6 +64,7 @@ struct Index { cancel: () => { if (this.dialogController !== null) { this.dialogController.close() + this.loginButton = ServerConstants.LOGIN_BUTTON_IN; } }, confirm: (isRegister: boolean) => { @@ -98,25 +100,132 @@ struct Index { aboutToAppear(): void { let loginName: string | undefined = AppStorage.get('loginName'); - this.getRecommendListFromServer(); + HttpManager.getInstance().getRecommendListFromServer(); if (loginName != ServerConstants.LOGIN_INFO_NO) { this.loginInfo = loginName + ServerConstants.LOGIN_INFO_DONE; this.loginButton = ServerConstants.LOGIN_BUTTON_OUT; - this.getCustomListFromServer(); + HttpManager.getInstance().getCustomListFromServer(); } else { this.customPlayLists = []; } + + emitter.on(ServerConstants.LOGIN_RESULT, (eventData: emitter.EventData) => { + if (eventData !== undefined && eventData.data !== undefined) { + switch (eventData.data.message) { + case ServerConstants.LOGIN_RESULT_SUCCESS: + this.loginInfo = this.inputUserName + ServerConstants.LOGIN_INFO_DONE; + this.loginButton = ServerConstants.LOGIN_BUTTON_OUT; + if (this.dialogController !== null) { + this.dialogController.close(); + } + break; + case ServerConstants.LOGIN_RESULT_FAILED: + this.loginButton = ServerConstants.LOGIN_BUTTON_IN; + break; + default: + break; + } + } + }); + emitter.on(ServerConstants.UPDATE_USER_LIST, (eventData: emitter.EventData) => { if (eventData !== undefined && eventData.data !== undefined) { switch (eventData.data.message) { + case ServerConstants.UPDATE_USER_LIST_RECOMMEND: + if (eventData.data.isStart) { + animateTo({ duration: 2000 }, () => { + this.recommendPlayLists = []; + }) + } + animateTo( + { duration: 1000 }, + () => { + if (eventData.data !== undefined && eventData.data.item !== undefined) { + console.info('UPDATE_USER_LIST_RECOMMEND:' + (eventData.data.item as PlayListData).title); + this.recommendPlayLists.push(eventData.data.item as PlayListData); + } + }) + break; case ServerConstants.UPDATE_USER_LIST_CUSTOM: - this.getCustomListFromServer(); + if (eventData.data.isStart) { + animateTo({ duration: 2000 }, () => { + this.customPlayLists = []; + }) + } + animateTo({ duration: 1000 }, () => { + if (eventData.data !== undefined && eventData.data.item !== undefined) { + console.info('UPDATE_USER_LIST_CUSTOM:' + (eventData.data.item as PlayListData).title); + this.customPlayLists.push(eventData.data.item as PlayListData); + } + }) break; case ServerConstants.UPDATE_USER_LIST_RECENT: - this.getRecentListFromServer(); + animateTo( + { duration: 1000 }, + () => { + if (eventData.data !== undefined && eventData.data.item !== undefined) { + console.info('UPDATE_USER_LIST_RECENT:' + (eventData.data.item as PlayListData).title); + let recentItem = eventData.data.item as PlayListData; + let existingIndex = this.customPlayLists.findIndex(item => item.title === recentItem.title); + if (existingIndex !== -1) { + this.customPlayLists[existingIndex] = recentItem; + } else { + this.customPlayLists.unshift(recentItem); + } + } + }) + break; + case ServerConstants.UPDATE_USER_LIST_SEARCH: + if (eventData.data.isStart) { + animateTo({ duration: 2000 }, () => { + this.searchResultPlayLists = [] + }) + } + animateTo( + { duration: 1000 }, + () => { + if (eventData.data !== undefined && eventData.data.item !== undefined) { + console.info('UPDATE_USER_LIST_SEARCH:' + (eventData.data.item as PlayListData).title); + this.searchResultPlayLists.push(eventData.data.item as PlayListData) + } + }) break; case ServerConstants.UPDATE_USER_LIST_FAVOR: - this.getFavorListFromServer(); + let existingIndex = this.customPlayLists.findIndex(item => item.title === ServerConstants.LIST_SONG_FAVOURITE); + animateTo( + { duration: 1000 }, + () => { + if (eventData.data !== undefined) { + if (eventData.data.item !== undefined) { + console.info('UPDATE_USER_LIST_FAVOR:' + (eventData.data.item as PlayListData).title); + if (existingIndex !== -1) { + this.customPlayLists[existingIndex] = eventData.data.item as PlayListData; + } else { + this.customPlayLists.unshift(eventData.data.item as PlayListData); + } + } else { + if (existingIndex !== -1) { + this.customPlayLists.splice(existingIndex, 1); + } + } + } + }) + break; + case ServerConstants.UPDATE_USER_LIST_SINGER: + if (eventData.data.isStart) { + animateTo({ duration: 2000 }, () => { + this.singerList = [] + }) + } + animateTo( + { duration: 1000 }, + () => { + if (eventData.data !== undefined && eventData.data.item !== undefined) { + console.info('UPDATE_USER_LIST_SINGER:' + (eventData.data.item as PlayListData).title); + this.singerList.push(eventData.data.item as PlayListData); + this.SingerAngle = 180; + } + }) break; default: break; @@ -126,9 +235,10 @@ struct Index { } aboutToDisappear(): void { - emitter.off(ServerConstants.UPDATE_TIME_EVENT_ID); - emitter.off(ServerConstants.UPDATE_STATE_EVENT_ID); + emitter.off(ServerConstants.MAIN_UPDATE_TIME); + emitter.off(ServerConstants.MAIN_UPDATE_STATE); emitter.off(ServerConstants.UPDATE_USER_LIST); + emitter.off(ServerConstants.LOGIN_RESULT); if (this.dialogController !== null) { this.dialogController.close(); this.dialogController = null; @@ -232,8 +342,9 @@ struct Index { .fontWeight(FontWeight.Medium) Image($r('app.media.chevron_right_2')) .objectFit(ImageFit.Contain) - .width(32) - .height(32) + .width(48) + .height(48) + .padding(8) .rotate({ angle: this.rowAngle }) .id('scroll_row') .clickEffect(UIConstants.CLICK_EFFECT) @@ -286,8 +397,9 @@ struct Index { .transition(TransitionEffect.OPACITY.animation({ duration: 2000, curve: Curve.Ease })) Image($r('app.media.xmark_circle')) .objectFit(ImageFit.Contain) - .width(32) - .height(32) + .width(48) + .height(48) + .padding(8) .transition(TransitionEffect.OPACITY.animation({ duration: 2000, curve: Curve.Ease }).combine( TransitionEffect.rotate({ z: 1, angle: 180 }) )) @@ -319,6 +431,62 @@ struct Index { .rowsGap(12) .width('90%') } + Row() { + Text($r('app.string.singer_list')) + .fontSize('20fp') + .fontWeight(FontWeight.Medium) + .transition(TransitionEffect.OPACITY.animation({ duration: 2000, curve: Curve.Ease })) + if (this.singerListClickable) { + Image($r('app.media.chevron_down_2')) + .objectFit(ImageFit.Contain) + .width(48) + .height(48) + .padding(8) + .rotate({ angle: this.SingerAngle }) + .clickEffect(UIConstants.CLICK_EFFECT) + .onClick(() => { + if (!this.singerListClickable) { + return; + } + this.singerListClickable = false; + setTimeout(() => { + this.singerListClickable = true; + }, 2000) + animateTo({ duration: 1000 }, () => { + if (this.singerList.length > 0) { + animateTo( + { duration: 1000 }, + () => { + this.SingerAngle = 0; + this.singerList = []; + }) + } else { + HttpManager.getInstance().getSingerListFromServer(); + } + }) + }) + } + } + .height(36) + .justifyContent(FlexAlign.SpaceBetween) + .width('90%') + .margin(12) + + Grid() { + ForEach(this.singerList, (item: PlayListData) => { + GridItem() { + PlayListItem({ item }) + } + .id('search_list_item') + .transition({ type: TransitionType.All, opacity: 0 }) + }) + } + .margin(12) + .columnsTemplate('1fr 1fr 1fr') + .columnsGap(8) + .rowsGap(12) + .width('90%') + Row() { Text($r('app.string.my_list')) @@ -326,8 +494,9 @@ struct Index { .fontWeight(FontWeight.Medium) Image($r('app.media.chevron_down_2')) .objectFit(ImageFit.Contain) - .width(32) - .height(32) + .width(48) + .height(48) + .padding(8) .id('scroll_col') .rotate({ angle: this.colAngle }) .clickEffect(UIConstants.CLICK_EFFECT) @@ -394,14 +563,14 @@ struct Index { .margin({ top: '12%' }) .onRefreshing(() => { setTimeout(() => { - this.getRecommendListFromServer(); + HttpManager.getInstance().getRecommendListFromServer(); if (this.loginInfo != ServerConstants.LOGIN_INFO_NO) { - this.getCustomListFromServer(); + HttpManager.getInstance().getCustomListFromServer(); } else { this.customPlayLists = []; } this.isRefreshing = false - }, 500) + }, 1000) }) Column() { @@ -496,399 +665,18 @@ struct Index { this.customPlayLists = []; this.loginInfo = ServerConstants.LOGIN_INFO_NO; }) - this.getRecommendListFromServer(); + HttpManager.getInstance().getRecommendListFromServer(); //TODO request logout from server } login(userName: string, pwd: string) { - try { - this.loginButton = ServerConstants.LOGIN_INFO_ING; - this.httpRequest.request(ServerConstants.CSRF_URL, - (err: Error, data: http.HttpResponse) => { - if (!err) { - console.info('HttpResponse Result:' + data.result); - try { - const jsonObject: object = JSON.parse(data.result as string); - this.csrfToken = jsonObject['csrf_token']; - this.httpRequest.request(ServerConstants.LOGIN_URL, - { - method: http.RequestMethod.POST, - extraData: 'username=' + userName + '&password=' + pwd, - header: { - 'content-Type': 'application/x-www-form-urlencoded', - 'X-CSRFToken': this.csrfToken, - 'Cookie': 'csrftoken=' + this.csrfToken + ';' - }, - }, - (err: Error, data: http.HttpResponse) => { - if (!err) { - console.info('HttpResponse Result:' + data.result); - console.info('HttpResponse code:' + data.responseCode); - console.info('HttpResponse type:' + JSON.stringify(data.resultType)); - console.info('HttpResponse header:' + JSON.stringify(data.header)); - if (200 === data.responseCode) { - console.info('HttpResponse code:' + (data.result as string).indexOf(ServerConstants.LOGIN_FAILED)); - if ((data.result as string).indexOf(ServerConstants.LOGIN_FAILED) !== -1) { - this.loginFailed(); - return; - } - this.loginInfo = this.inputUserName + ServerConstants.LOGIN_INFO_DONE; - AppStorage.setOrCreate('loginName', this.inputUserName); - let loginCookie: string[] = data.header['set-cookie']; - let cookie: string = loginCookie.map(cookie => { - const parts = cookie.split('='); - return `${parts[0]}=${parts[1]}`; - }).join('; ') - AppStorage.setOrCreate('loginCookie', cookie); - } - this.loginButton = ServerConstants.LOGIN_BUTTON_OUT; - if (this.dialogController !== null) { - this.dialogController.close(); - } - this.getRecommendListFromServer(); - this.getCustomListFromServer(); - } else { - console.info('HttpResponse error:' + JSON.stringify(err)); - prompt.showToast({ - message: JSON.stringify(err) - }) - this.loginFailed(); - } - }); - } - catch (error) { - console.error("JSON :", error.message); - this.loginFailed(); - } - } else { - console.info('HttpResponse error:' + JSON.stringify(err)); - prompt.showToast({ - message: JSON.stringify(err) - }) - this.loginFailed(); - } - }); - } catch (err) { - console.info('HttpRequest error:' + JSON.stringify(err)); - this.loginFailed(); - } + this.loginButton = ServerConstants.LOGIN_INFO_ING; + HttpManager.getInstance().login(userName, pwd) } register(userName: string, pwd: string, pwd2: string) { - if (userName.trim() === '' || pwd.trim() === '') { - prompt.showToast({ - message: '用户名和密码不能为空!' - }) - return; - } - if (pwd !== pwd2) { - prompt.showToast({ - message: '两次密码输入不同!' - }) - return; - } - try { - this.loginButton = ServerConstants.LOGIN_INFO_ING; - this.httpRequest.request(ServerConstants.CSRF_URL, - (err: Error, data: http.HttpResponse) => { - if (!err) { - console.info('HttpResponse Result:' + data.result); - try { - const jsonObject: object = JSON.parse(data.result as string); - this.csrfToken = jsonObject['csrf_token']; - this.httpRequest.request(ServerConstants.REGISTER_URL, - { - method: http.RequestMethod.POST, - extraData: 'username=' + userName.trim() + '&password=' + pwd.trim(), - header: { - 'content-Type': 'application/x-www-form-urlencoded', - 'X-CSRFToken': this.csrfToken, - 'Cookie': 'csrftoken=' + this.csrfToken + ';' - }, - }, - (err: Error, data: http.HttpResponse) => { - if (!err) { - console.info('HttpResponse Result:' + data.result); - if ((data.result as string).indexOf(ServerConstants.REGISTER_FAILED) !== -1) { - prompt.showToast({ - message: ServerConstants.REGISTER_INFO_FAILED + data.result - }) - return; - } - prompt.showToast({ - message: ServerConstants.REGISTER_INFO_SUCCESS - }) - this.login(userName.trim(), pwd.trim()); - if (this.dialogController) { - this.dialogController.close(); - } - } else { - console.info('HttpResponse error:' + JSON.stringify(err)); - prompt.showToast({ - message: ServerConstants.REGISTER_INFO_FAILED - }) - } - }); - } catch (err) { - console.info('HttpRequest error:' + JSON.stringify(err)); - prompt.showToast({ - message: ServerConstants.REGISTER_INFO_FAILED - }) - } - } else { - console.info('HttpResponse error:' + JSON.stringify(err)); - prompt.showToast({ - message: ServerConstants.REGISTER_INFO_FAILED - }) - } - }); - } catch (err) { - console.info('HttpRequest error:' + JSON.stringify(err)); - prompt.showToast({ - message: ServerConstants.REGISTER_INFO_FAILED - }) - } - } - - loginFailed() { - this.loginButton = ServerConstants.LOGIN_BUTTON_IN; - prompt.showToast({ - message: ServerConstants.LOGIN_INFO_FAILED - }) - } - - getRecommendListFromServer() { - animateTo({ duration: 2000 }, () => { - this.recommendPlayLists = []; - }) - try { - this.httpRequest.request(ServerConstants.RECOMMEND_LIST_URL, - { - method: http.RequestMethod.GET, - header: { - 'Cookie': AppStorage.get('loginCookie') - } - }, - (err: Error, data: http.HttpResponse) => { - if (!err) { - console.info('HttpResponse getRecommendListFromServer Result:' + data.result); - try { - const jsonObject: object = JSON.parse(data.result as string); - Object.keys(jsonObject).forEach((key) => { - let aPlayingList: AudioItem[] = Array(); - Object.keys(jsonObject[key]).forEach((key2) => { - aPlayingList.push(new AudioItem(jsonObject[key][key2].name, jsonObject[key][key2].singer, jsonObject[key][key2].id)); - }); - animateTo( - { duration: 1000 }, - () => { - this.recommendPlayLists.push(new PlayListData(key, $r('app.media.icon'), aPlayingList.length + ServerConstants.LIST_SONG_COUNT, - aPlayingList, '')); - }) - }); - } - catch (error) { - console.error("JSON :", error.message); - } - console.info('HttpResponse code:' + data.responseCode); - console.info('HttpResponse type:' + JSON.stringify(data.resultType)); - console.info('HttpResponse header:' + JSON.stringify(data.header)); - console.info('HttpResponse cookies:' + data.cookies); - } else { - console.info('HttpResponse error:' + JSON.stringify(err)); - prompt.showToast({ - message: JSON.stringify(err) - }) - } - }); - } catch (err) { - console.info('HttpRequest error:' + JSON.stringify(err)); - prompt.showToast({ - message: JSON.stringify(err) - }) - } - } - - getFavorListFromServer() { - try { - this.httpRequest.request(ServerConstants.FAVOR_LIST_URL, - { - method: http.RequestMethod.GET, - header: { - 'Cookie': AppStorage.get('loginCookie') - } - }, - (err: Error, data: http.HttpResponse) => { - if (!err) { - console.info('HttpResponse getRecentListFromServer Result:' + data.result); - try { - let existingIndex = this.customPlayLists.findIndex(item => item.title === ServerConstants.LIST_SONG_FAVOURITE); - let aPlayingList: AudioItem[] = Array(); - const jsonObject: object = JSON.parse(data.result as string); - if (jsonObject[ServerConstants.LIST_SONG_FAVOURITE]) { - Object.keys(jsonObject[ServerConstants.LIST_SONG_FAVOURITE]).forEach((key) => { - aPlayingList.push(new AudioItem(jsonObject[ServerConstants.LIST_SONG_FAVOURITE][key].name, jsonObject[ServerConstants.LIST_SONG_FAVOURITE][key].singer, jsonObject[ServerConstants.LIST_SONG_FAVOURITE][key].id)); - }); - animateTo( - { duration: 1000 }, - () => { - if (aPlayingList.length !== 0) { - let favorItem = new PlayListData(ServerConstants.LIST_SONG_FAVOURITE, $r('app.media.icon'), aPlayingList.length + ServerConstants.LIST_SONG_COUNT, - aPlayingList, ''); - if (existingIndex !== -1) { - this.customPlayLists[existingIndex] = favorItem; - } else { - this.customPlayLists.unshift(favorItem); - } - } else { - if (existingIndex !== -1) { - this.customPlayLists.splice(existingIndex, 1); - } - } - }) - } else { - this.customPlayLists.splice(existingIndex, 1); - } - this.PlayerManager.setFavourList(aPlayingList); - } - catch (error) { - console.error("JSON :", error.message); - } - console.info('HttpResponse code:' + data.responseCode); - console.info('HttpResponse type:' + JSON.stringify(data.resultType)); - console.info('HttpResponse header:' + JSON.stringify(data.header)); - console.info('HttpResponse cookies:' + data.cookies); - } else { - console.info('HttpResponse error:' + JSON.stringify(err)); - prompt.showToast({ - message: JSON.stringify(err) - }) - } - }); - } catch (err) { - console.info('HttpRequest error:' + JSON.stringify(err)); - prompt.showToast({ - message: JSON.stringify(err) - }) - } - } - - getRecentListFromServer() { - try { - this.httpRequest.request(ServerConstants.RECENT_LIST_URL, - { - method: http.RequestMethod.GET, - header: { - 'Cookie': AppStorage.get('loginCookie') - } - }, - (err: Error, data: http.HttpResponse) => { - if (!err) { - console.info('HttpResponse getRecentListFromServer Result:' + data.result); - try { - const jsonObject: object = JSON.parse(data.result as string); - Object.keys(jsonObject).forEach((key) => { - let aPlayingList: AudioItem[] = Array(); - Object.keys(jsonObject[key]).forEach((key2) => { - aPlayingList.push(new AudioItem(jsonObject[key][key2].name, jsonObject[key][key2].singer, jsonObject[key][key2].id)); - }); - animateTo( - { duration: 1000 }, - () => { - let recentItem = new PlayListData(key, $r('app.media.icon'), aPlayingList.length + ServerConstants.LIST_SONG_COUNT, - aPlayingList, ''); - let existingIndex = this.customPlayLists.findIndex(item => item.title === recentItem.title); - if (existingIndex !== -1) { - this.customPlayLists[existingIndex] = recentItem; - } else { - this.customPlayLists.unshift(recentItem); - } - }) - }); - } - catch (error) { - console.error("JSON :", error.message); - } - console.info('HttpResponse code:' + data.responseCode); - console.info('HttpResponse type:' + JSON.stringify(data.resultType)); - console.info('HttpResponse header:' + JSON.stringify(data.header)); - console.info('HttpResponse cookies:' + data.cookies); - } else { - console.info('HttpResponse error:' + JSON.stringify(err)); - prompt.showToast({ - message: JSON.stringify(err) - }) - } - }); - } catch (err) { - console.info('HttpRequest error:' + JSON.stringify(err)); - prompt.showToast({ - message: JSON.stringify(err) - }) - } - } - - getCustomListFromServer() { - animateTo({ duration: 2000 }, () => { - this.customPlayLists = []; - }) - try { - this.httpRequest.request(ServerConstants.CUSTOM_LIST_URL, - { - method: http.RequestMethod.GET, - header: { - 'Cookie': AppStorage.get('loginCookie') - } - }, - (err: Error, data: http.HttpResponse) => { - if (!err) { - console.info('HttpResponse getCustomListFromServer Result:' + data.result); - try { - interface PlaylistItem { - playlist_name: string; - song: number; - id: number; - name: string; - singer: string; - language: string; - } - - let jsonObject: PlaylistItem[] = JSON.parse(data.result as string); - let groupedByPlaylistName = jsonObject.reduce((accumulator, item) => { - if (!accumulator[item.playlist_name]) { - accumulator[item.playlist_name] = []; - } - accumulator[item.playlist_name].push(item); - return accumulator; - }, {} as Record); - const keys = Object.keys(groupedByPlaylistName); - keys.forEach((playlistName) => { - console.log(`Playlist: ${playlistName}`); - let aPlayingList: AudioItem[] = Array(); - groupedByPlaylistName[playlistName].forEach((song, index) => { - aPlayingList.push(new AudioItem(song.name, song.singer, song.id.toString())); - }); - animateTo({ duration: 1000 }, () => { - this.customPlayLists.push(new PlayListData(playlistName, $r('app.media.icon'), aPlayingList.length + ServerConstants.LIST_SONG_COUNT, - aPlayingList, '')); - }) - }); - this.getRecentListFromServer(); - this.getFavorListFromServer(); - } - catch (error) { - console.error("JSON :", error.message); - } - } else { - console.info('HttpResponse error:' + JSON.stringify(err)); - prompt.showToast({ - message: JSON.stringify(err) - }) - } - }); - } catch (err) { - console.info('HttpRequest error:' + JSON.stringify(err)); - } + this.loginButton = ServerConstants.LOGIN_INFO_ING; + HttpManager.getInstance().register(userName, pwd, pwd2); } searchSongsFromServer(search_word: string) { @@ -903,55 +691,6 @@ struct Index { this.searchSongTitle = search_word + ServerConstants.SEARCHING; this.searchResultPlayLists = []; }) - try { - this.httpRequest.request(ServerConstants.SEARCH_SONG_URL + search_word, - { - method: http.RequestMethod.GET, - header: { - 'Cookie': AppStorage.get('loginCookie') - } - }, - (err: Error, data: http.HttpResponse) => { - if (!err) { - console.info('HttpResponse searchSongsFromServer Result:' + data.result); - try { - const jsonObject: object = JSON.parse(data.result as string); - Object.keys(jsonObject).forEach((key) => { - animateTo( - { duration: 1000 }, - () => { - this.searchSongTitle = key; - }) - Object.keys(jsonObject[key]).forEach((key2) => { - let aPlayingList: AudioItem[] = Array(); - let aAudioItem = new AudioItem(jsonObject[key][key2].name, jsonObject[key][key2].singer, jsonObject[key][key2].id) - aPlayingList.push(aAudioItem); - animateTo( - { duration: 1000 }, - () => { - this.searchResultPlayLists.push(new PlayListData(jsonObject[key][key2].name, $r('app.media.icon'), '', aPlayingList, '')) - }) - }); - }); - } catch (error) { - console.error("JSON :", error.message); - this.searchSongTitle = ServerConstants.SEARCH_DEFAULT; - } - console.info('HttpResponse code:' + data.responseCode); - console.info('HttpResponse type:' + JSON.stringify(data.resultType)); - console.info('HttpResponse header:' + JSON.stringify(data.header)); - console.info('HttpResponse cookies:' + data.cookies); - } else { - console.info('HttpResponse error:' + JSON.stringify(err)); - prompt.showToast({ - message: JSON.stringify(err) - }) - this.searchSongTitle = ServerConstants.SEARCH_DEFAULT; - } - }); - } catch (err) { - console.info('HttpRequest error:' + JSON.stringify(err)); - this.searchSongTitle = ServerConstants.SEARCH_DEFAULT; - } + HttpManager.getInstance().searchSongsFromServer(search_word); } } \ No newline at end of file diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayList.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayList.ets index ad1c9635..cc6d35ff 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayList.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayList.ets @@ -35,7 +35,8 @@ export default struct PlayList { setTimeout(() => { this.scrollerForList.scrollToIndex(this.playingList.indexOf(this.playerManager.getItem()), true, ScrollAlign.CENTER); }, 2000) - emitter.on(ServerConstants.UPDATE_STATE_EVENT_ID, (eventData: emitter.EventData) => { + + emitter.on(ServerConstants.PLAYLIST_UPDATE_STATE, (eventData: emitter.EventData) => { if (eventData !== undefined && eventData.data !== undefined && eventData.data.state === ServerConstants.PLAYER_STATE_PLAYING) { animateTo({ duration: 2000 }, () => { this.playingList = this.playerManager.getCurrentPlayList(); @@ -48,6 +49,7 @@ export default struct PlayList { } aboutToDisappear(): void { + emitter.off(ServerConstants.PLAYLIST_UPDATE_STATE); } build() { diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayListItem.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayListItem.ets index 42b72907..4d84ee4c 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayListItem.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayListItem.ets @@ -56,7 +56,7 @@ export default struct PlayListItem { Text(this.item.title) .fontSize('20fp') - .maxLines(2) + .maxLines(1) .margin({ top: 4 }) } .alignItems(HorizontalAlign.Center) diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerBar.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerBar.ets index 2b9944ba..ef4e1de9 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerBar.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerBar.ets @@ -38,14 +38,13 @@ export default struct PlayerBar { aboutToAppear() { Logger.info(this.tag, 'aboutToAppear'); - emitter.on(ServerConstants.UPDATE_TIME_EVENT_ID, (eventData: emitter.EventData) => { + emitter.on(ServerConstants.MAIN_UPDATE_TIME, (eventData: emitter.EventData) => { if (eventData !== undefined && eventData.data !== undefined) { - //Logger.info(this.tag, 'currentTime:' + eventData.data.currentTime); this.currentTime = eventData.data.currentTime; this.durationTime = eventData.data.currentDuration; } }); - emitter.on(ServerConstants.UPDATE_STATE_EVENT_ID, (eventData: emitter.EventData) => { + emitter.on(ServerConstants.MAIN_UPDATE_STATE, (eventData: emitter.EventData) => { if (eventData !== undefined && eventData.data !== undefined) { Logger.info(this.tag, 'state:' + eventData.data.state); if (this.state !== ServerConstants.PLAYER_STATE_PLAYING && eventData.data.state === ServerConstants.PLAYER_STATE_PLAYING) { @@ -66,8 +65,6 @@ export default struct PlayerBar { aboutToDisappear() { Logger.info(this.tag, 'aboutToDisappear'); - emitter.off(ServerConstants.UPDATE_TIME_EVENT_ID); - emitter.off(ServerConstants.UPDATE_STATE_EVENT_ID); } build() { diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerDetail.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerDetail.ets index 7fde4661..d5a84ac8 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerDetail.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerDetail.ets @@ -25,6 +25,7 @@ import { PLAY_MODE } from '../constants/PlayMode'; import { addToListDialog } from './addToListDialog'; import PlayListData from '../model/PlayListData'; import prompt from '@ohos.promptAction'; +import HttpManager from '../manager/HttpManager'; /** * Setting tab content @@ -66,7 +67,7 @@ export default struct PlayerDetail { confirm: () => { if (this.inputListName.trim()) { if (this.songIdToAdd !== '') { - this.PlayerManager.add_to_playlist(this.songIdToAdd, this.inputListName); + HttpManager.getInstance().add_to_playlist(this.songIdToAdd, this.inputListName); this.songIdToAdd = ''; if (this.dialogController !== null) { this.dialogController.close(); @@ -116,13 +117,13 @@ export default struct PlayerDetail { } aboutToAppear() { - emitter.on(ServerConstants.UPDATE_FAVOR_EVENT_ID_DETAIL, () => { + emitter.on(ServerConstants.DETAIL_UPDATE_FAVOR, () => { animateTo({ duration: 2000 }, () => { this.item = this.PlayerManager.getItem(); }) this.noItem = false; }); - emitter.on(ServerConstants.UPDATE_TIME_EVENT_ID_DETAIL, (eventData: emitter.EventData) => { + emitter.on(ServerConstants.DETAIL_UPDATE_TIME, (eventData: emitter.EventData) => { if (eventData !== undefined && eventData.data !== undefined) { //Logger.info(this.tag, 'currentTime:' + eventData.data.currentTime); this.currentTime = eventData.data.currentTime; @@ -134,7 +135,7 @@ export default struct PlayerDetail { } } }); - emitter.on(ServerConstants.UPDATE_STATE_EVENT_ID_DETAIL, (eventData: emitter.EventData) => { + emitter.on(ServerConstants.DETAIL_UPDATE_STATE, (eventData: emitter.EventData) => { if (eventData !== undefined && eventData.data !== undefined) { Logger.info(this.tag, 'state:' + eventData.data.state); if (eventData.data.state === ServerConstants.PLAYER_STATE_IDLE) { @@ -172,9 +173,9 @@ export default struct PlayerDetail { aboutToDisappear() { Logger.info(this.tag, 'aboutToDisappear'); - emitter.off(ServerConstants.UPDATE_TIME_EVENT_ID_DETAIL); - emitter.off(ServerConstants.UPDATE_STATE_EVENT_ID_DETAIL); - emitter.off(ServerConstants.UPDATE_FAVOR_EVENT_ID_DETAIL); + emitter.off(ServerConstants.DETAIL_UPDATE_TIME); + emitter.off(ServerConstants.DETAIL_UPDATE_STATE); + emitter.off(ServerConstants.DETAIL_UPDATE_FAVOR); if (this.dialogController !== null) { this.dialogController.close(); this.isDialogOpened = false; @@ -252,7 +253,9 @@ export default struct PlayerDetail { if (this.dialogController != null) { this.listOptions = []; for (let item of this.customPlayLists) { - this.listOptions.push({ value: item.title }); + if(item.title !== ServerConstants.LIST_SONG_FAVOURITE && item.title !== ServerConstants.LIST_SONG_RECENT){ + this.listOptions.push({ value: item.title }); + } } this.dialogController.open(); this.songIdToAdd = this.item.id; @@ -313,7 +316,7 @@ export default struct PlayerDetail { .margin({ right: '8vp' }) .clickEffect(UIConstants.CLICK_EFFECT) .onClick(() => { - this.PlayerManager.set_favourite(); + HttpManager.getInstance().set_favourite(); }) } .width('80%') diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/view/addToListDialog.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/view/addToListDialog.ets index 28648afc..cbab5270 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/view/addToListDialog.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/view/addToListDialog.ets @@ -85,7 +85,7 @@ export struct addToListDialog { .fontWeight(FontWeight.Lighter) .fontSize(18) .fontColor(Color.Red) - .id('login_button') + .id('login_confirm') .onClick(() => { this.confirm() }) diff --git a/scenario/MusicPlayerOnline/entry/src/main/resources/base/element/string.json b/scenario/MusicPlayerOnline/entry/src/main/resources/base/element/string.json index 7484cf4f..b901aa71 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/resources/base/element/string.json +++ b/scenario/MusicPlayerOnline/entry/src/main/resources/base/element/string.json @@ -24,6 +24,10 @@ "name": "recommend_list", "value": "推荐歌单" }, + { + "name": "singer_list", + "value": "热门歌手" + }, { "name": "my_list", "value": "我的歌单" diff --git a/scenario/MusicPlayerOnline/entry/src/main/resources/en_US/element/string.json b/scenario/MusicPlayerOnline/entry/src/main/resources/en_US/element/string.json index 683bb2c4..ac57a47f 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/resources/en_US/element/string.json +++ b/scenario/MusicPlayerOnline/entry/src/main/resources/en_US/element/string.json @@ -20,6 +20,10 @@ "name": "recommend_list", "value": "Recommend Lists" }, + { + "name": "singer_list", + "value": "Singer" + }, { "name": "my_list", "value": "My Lists" diff --git a/scenario/MusicPlayerOnline/entry/src/main/resources/zh_CN/element/string.json b/scenario/MusicPlayerOnline/entry/src/main/resources/zh_CN/element/string.json index 99e4d74f..37be2ffa 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/resources/zh_CN/element/string.json +++ b/scenario/MusicPlayerOnline/entry/src/main/resources/zh_CN/element/string.json @@ -20,6 +20,10 @@ "name": "recommend_list", "value": "推荐歌单" }, + { + "name": "singer_list", + "value": "热门歌手" + }, { "name": "my_list", "value": "我的歌单" -- Gitee From 1c86e0943e06fcfa69be44396a722661440f6a5c Mon Sep 17 00:00:00 2001 From: sunlian Date: Wed, 25 Sep 2024 15:32:04 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E9=A6=96=E9=A1=B5=E6=8E=A8=E8=8D=90?= =?UTF-8?q?=E7=83=AD=E9=97=A8=E6=AD=8C=E6=89=8B=EF=BC=8Chttp=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: sunlian --- .../MusicPlayerOnline/build-profile.json5 | 15 +- .../main/ets/constants/ServerConstants.ets | 2 +- .../src/main/ets/manager/HttpManager.ets | 20 +- .../entry/src/main/ets/pages/Index.ets | 250 +++++++++--------- .../entry/src/main/ets/view/PlayList.ets | 20 +- .../entry/src/main/ets/view/PlayerBar.ets | 47 ++-- .../entry/src/main/ets/view/PlayerDetail.ets | 106 ++++---- 7 files changed, 251 insertions(+), 209 deletions(-) diff --git a/scenario/MusicPlayerOnline/build-profile.json5 b/scenario/MusicPlayerOnline/build-profile.json5 index 08c1020e..4fac6ae1 100644 --- a/scenario/MusicPlayerOnline/build-profile.json5 +++ b/scenario/MusicPlayerOnline/build-profile.json5 @@ -1,6 +1,19 @@ { "app": { - "signingConfigs": [], + "signingConfigs": [ + { + "name": "default", + "material": { + "certpath": "C:/Users/Administrator/.ohos/config/openharmony/default_MusicPlayerOnline_hk2UHQE6-ErE070Ws-glQcy1fqzzWJa-YFhy3hlWKdg=.cer", + "storePassword": "0000001AB22FBDBD4816BDDE71C489BBC1F9A76F7472B473A58321B20952F07AA2A077937F0FC72BF3F2", + "keyAlias": "debugKey", + "keyPassword": "0000001A240E59627ABDDF94AD4BE975DCAA2B17D1D2D6E89305F7794E5506071F20FA9E0AE0AF6600E1", + "profile": "C:/Users/Administrator/.ohos/config/openharmony/default_MusicPlayerOnline_hk2UHQE6-ErE070Ws-glQcy1fqzzWJa-YFhy3hlWKdg=.p7b", + "signAlg": "SHA256withECDSA", + "storeFile": "C:/Users/Administrator/.ohos/config/openharmony/default_MusicPlayerOnline_hk2UHQE6-ErE070Ws-glQcy1fqzzWJa-YFhy3hlWKdg=.p12" + } + } + ], "products": [ { "name": "default", diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/constants/ServerConstants.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/constants/ServerConstants.ets index 5e158049..87f26ac2 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/constants/ServerConstants.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/constants/ServerConstants.ets @@ -106,7 +106,7 @@ export default class ServerConstants { */ static readonly DETAIL_UPDATE_TIME = '3'; static readonly DETAIL_UPDATE_STATE = '4'; - static readonly DETAIL_UPDATE_FAVOR = '5'; + static readonly DETAIL_UPDATE_FAVOR_AND_QUALITY = '5'; /** * 播放列表播放状态更新消息 */ diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/manager/HttpManager.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/manager/HttpManager.ets index 48da19fb..59411276 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/manager/HttpManager.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/manager/HttpManager.ets @@ -160,7 +160,6 @@ export default class HttpManager { } }; emitter.emit(ServerConstants.LOGIN_RESULT, this.emitterOptions, eventData); - //this.getRecommendListFromServer(); this.getCustomListFromServer(); } } else { @@ -226,7 +225,7 @@ export default class HttpManager { try { const jsonObject: object = JSON.parse(data.result as string); this.PlayerManager.setItemQuality(jsonObject['message']); - emitter.emit(ServerConstants.DETAIL_UPDATE_FAVOR, this.emitterOptions); + emitter.emit(ServerConstants.DETAIL_UPDATE_FAVOR_AND_QUALITY, this.emitterOptions); } catch (err) { console.info('HttpResponse error:' + JSON.stringify(err)); } @@ -281,7 +280,7 @@ export default class HttpManager { break; } this.PlayerManager.setItemFavor(item.isFavor); - emitter.emit(ServerConstants.DETAIL_UPDATE_FAVOR, this.emitterOptions); + emitter.emit(ServerConstants.DETAIL_UPDATE_FAVOR_AND_QUALITY, this.emitterOptions); prompt.showToast({ message: jsonObject["message"] }) @@ -309,7 +308,7 @@ export default class HttpManager { } } - + //歌曲加入歌单 add_to_playlist(song_id: string, playlist_name: string) { let loginCookie: string | undefined = AppStorage.get('loginCookie'); let msg: string = '' @@ -332,15 +331,10 @@ export default class HttpManager { try { const jsonObject: object = JSON.parse(data.result as string); console.info('HttpResponse msg:' + jsonObject["message"]); - let eventData: emitter.EventData = { - data: { - "message": ServerConstants.UPDATE_USER_LIST_CUSTOM, - } - }; - emitter.emit(ServerConstants.UPDATE_USER_LIST, this.emitterOptions, eventData); prompt.showToast({ message: jsonObject["message"] }) + this.getCustomListFromServer(); } catch (error) { console.error("JSON :", error.message); msg = JSON.stringify(error); @@ -364,6 +358,7 @@ export default class HttpManager { } } + //获取推荐歌单 getRecommendListFromServer() { try { this.httpRequest.request(ServerConstants.RECOMMEND_LIST_URL, @@ -420,6 +415,7 @@ export default class HttpManager { } } + //获取推荐歌手 getSingerListFromServer() { try { this.httpRequest.request(ServerConstants.SINGER_LIST_URL, @@ -472,6 +468,7 @@ export default class HttpManager { } } + //获取我的收藏 getFavorListFromServer() { try { this.httpRequest.request(ServerConstants.FAVOR_LIST_URL, @@ -526,6 +523,7 @@ export default class HttpManager { } } + //获取最近播放 getRecentListFromServer() { try { this.httpRequest.request(ServerConstants.RECENT_LIST_URL, @@ -582,6 +580,7 @@ export default class HttpManager { } } + //获取自定义歌单 getCustomListFromServer() { try { this.httpRequest.request(ServerConstants.CUSTOM_LIST_URL, @@ -651,6 +650,7 @@ export default class HttpManager { } } + //搜素歌曲 searchSongsFromServer(search_word: string) { try { this.httpRequest.request(ServerConstants.SEARCH_SONG_URL + search_word, diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/pages/Index.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/pages/Index.ets index 415c316c..7e58bb29 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/pages/Index.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/pages/Index.ets @@ -22,7 +22,6 @@ import PlayListData from '../model/PlayListData'; import PlayerDetail from '../view/PlayerDetail'; import emitter from '@ohos.events.emitter'; import prompt from '@ohos.promptAction'; -import PlayerManager from '../manager/PlayerManager'; import Setting from '../view/Setting'; import { LoginCustomDialog } from '../view/loginDialog'; import HttpManager from '../manager/HttpManager'; @@ -33,7 +32,6 @@ PersistentStorage.persistProp('loginName', ServerConstants.LOGIN_INFO_NO); @Entry @Component struct Index { - private PlayerManager: PlayerManager = AppStorage.get('PlayerManager') as PlayerManager; @State recommendPlayLists: PlayListData[] = []; @State customPlayLists: PlayListData[] = []; @State searchResultPlayLists: PlayListData[] = []; @@ -59,7 +57,7 @@ struct Index { @State inputUserName: string = '' @State inputPwd: string = '' @State inputPwd2: string = '' - dialogController: CustomDialogController | null = new CustomDialogController({ + private dialogController: CustomDialogController | null = new CustomDialogController({ builder: LoginCustomDialog({ cancel: () => { if (this.dialogController !== null) { @@ -110,133 +108,15 @@ struct Index { } emitter.on(ServerConstants.LOGIN_RESULT, (eventData: emitter.EventData) => { - if (eventData !== undefined && eventData.data !== undefined) { - switch (eventData.data.message) { - case ServerConstants.LOGIN_RESULT_SUCCESS: - this.loginInfo = this.inputUserName + ServerConstants.LOGIN_INFO_DONE; - this.loginButton = ServerConstants.LOGIN_BUTTON_OUT; - if (this.dialogController !== null) { - this.dialogController.close(); - } - break; - case ServerConstants.LOGIN_RESULT_FAILED: - this.loginButton = ServerConstants.LOGIN_BUTTON_IN; - break; - default: - break; - } - } + this.updateLogin(eventData); }); emitter.on(ServerConstants.UPDATE_USER_LIST, (eventData: emitter.EventData) => { - if (eventData !== undefined && eventData.data !== undefined) { - switch (eventData.data.message) { - case ServerConstants.UPDATE_USER_LIST_RECOMMEND: - if (eventData.data.isStart) { - animateTo({ duration: 2000 }, () => { - this.recommendPlayLists = []; - }) - } - animateTo( - { duration: 1000 }, - () => { - if (eventData.data !== undefined && eventData.data.item !== undefined) { - console.info('UPDATE_USER_LIST_RECOMMEND:' + (eventData.data.item as PlayListData).title); - this.recommendPlayLists.push(eventData.data.item as PlayListData); - } - }) - break; - case ServerConstants.UPDATE_USER_LIST_CUSTOM: - if (eventData.data.isStart) { - animateTo({ duration: 2000 }, () => { - this.customPlayLists = []; - }) - } - animateTo({ duration: 1000 }, () => { - if (eventData.data !== undefined && eventData.data.item !== undefined) { - console.info('UPDATE_USER_LIST_CUSTOM:' + (eventData.data.item as PlayListData).title); - this.customPlayLists.push(eventData.data.item as PlayListData); - } - }) - break; - case ServerConstants.UPDATE_USER_LIST_RECENT: - animateTo( - { duration: 1000 }, - () => { - if (eventData.data !== undefined && eventData.data.item !== undefined) { - console.info('UPDATE_USER_LIST_RECENT:' + (eventData.data.item as PlayListData).title); - let recentItem = eventData.data.item as PlayListData; - let existingIndex = this.customPlayLists.findIndex(item => item.title === recentItem.title); - if (existingIndex !== -1) { - this.customPlayLists[existingIndex] = recentItem; - } else { - this.customPlayLists.unshift(recentItem); - } - } - }) - break; - case ServerConstants.UPDATE_USER_LIST_SEARCH: - if (eventData.data.isStart) { - animateTo({ duration: 2000 }, () => { - this.searchResultPlayLists = [] - }) - } - animateTo( - { duration: 1000 }, - () => { - if (eventData.data !== undefined && eventData.data.item !== undefined) { - console.info('UPDATE_USER_LIST_SEARCH:' + (eventData.data.item as PlayListData).title); - this.searchResultPlayLists.push(eventData.data.item as PlayListData) - } - }) - break; - case ServerConstants.UPDATE_USER_LIST_FAVOR: - let existingIndex = this.customPlayLists.findIndex(item => item.title === ServerConstants.LIST_SONG_FAVOURITE); - animateTo( - { duration: 1000 }, - () => { - if (eventData.data !== undefined) { - if (eventData.data.item !== undefined) { - console.info('UPDATE_USER_LIST_FAVOR:' + (eventData.data.item as PlayListData).title); - if (existingIndex !== -1) { - this.customPlayLists[existingIndex] = eventData.data.item as PlayListData; - } else { - this.customPlayLists.unshift(eventData.data.item as PlayListData); - } - } else { - if (existingIndex !== -1) { - this.customPlayLists.splice(existingIndex, 1); - } - } - } - }) - break; - case ServerConstants.UPDATE_USER_LIST_SINGER: - if (eventData.data.isStart) { - animateTo({ duration: 2000 }, () => { - this.singerList = [] - }) - } - animateTo( - { duration: 1000 }, - () => { - if (eventData.data !== undefined && eventData.data.item !== undefined) { - console.info('UPDATE_USER_LIST_SINGER:' + (eventData.data.item as PlayListData).title); - this.singerList.push(eventData.data.item as PlayListData); - this.SingerAngle = 180; - } - }) - break; - default: - break; - } - } + this.updateLists(eventData); }); } aboutToDisappear(): void { - emitter.off(ServerConstants.MAIN_UPDATE_TIME); - emitter.off(ServerConstants.MAIN_UPDATE_STATE); emitter.off(ServerConstants.UPDATE_USER_LIST); emitter.off(ServerConstants.LOGIN_RESULT); if (this.dialogController !== null) { @@ -693,4 +573,128 @@ struct Index { }) HttpManager.getInstance().searchSongsFromServer(search_word); } + + updateLogin(eventData: emitter.EventData) { + if (eventData !== undefined && eventData.data !== undefined) { + switch (eventData.data.message) { + case ServerConstants.LOGIN_RESULT_SUCCESS: + this.loginInfo = this.inputUserName + ServerConstants.LOGIN_INFO_DONE; + this.loginButton = ServerConstants.LOGIN_BUTTON_OUT; + if (this.dialogController !== null) { + this.dialogController.close(); + } + break; + case ServerConstants.LOGIN_RESULT_FAILED: + this.loginButton = ServerConstants.LOGIN_BUTTON_IN; + break; + default: + break; + } + } + } + + updateLists(eventData: emitter.EventData) { + if (eventData !== undefined && eventData.data !== undefined) { + switch (eventData.data.message) { + case ServerConstants.UPDATE_USER_LIST_RECOMMEND: + if (eventData.data.isStart) { + animateTo({ duration: 2000 }, () => { + this.recommendPlayLists = []; + }) + } + animateTo( + { duration: 1000 }, + () => { + if (eventData.data !== undefined && eventData.data.item !== undefined) { + console.info('UPDATE_USER_LIST_RECOMMEND:' + (eventData.data.item as PlayListData).title); + this.recommendPlayLists.push(eventData.data.item as PlayListData); + } + }) + break; + case ServerConstants.UPDATE_USER_LIST_CUSTOM: + if (eventData.data.isStart) { + animateTo({ duration: 2000 }, () => { + this.customPlayLists = []; + }) + } + animateTo({ duration: 1000 }, () => { + if (eventData.data !== undefined && eventData.data.item !== undefined) { + console.info('UPDATE_USER_LIST_CUSTOM:' + (eventData.data.item as PlayListData).title); + this.customPlayLists.push(eventData.data.item as PlayListData); + } + }) + break; + case ServerConstants.UPDATE_USER_LIST_RECENT: + animateTo( + { duration: 1000 }, + () => { + if (eventData.data !== undefined && eventData.data.item !== undefined) { + console.info('UPDATE_USER_LIST_RECENT:' + (eventData.data.item as PlayListData).title); + let recentItem = eventData.data.item as PlayListData; + let existingIndex = this.customPlayLists.findIndex(item => item.title === recentItem.title); + if (existingIndex !== -1) { + this.customPlayLists[existingIndex] = recentItem; + } else { + this.customPlayLists.unshift(recentItem); + } + } + }) + break; + case ServerConstants.UPDATE_USER_LIST_SEARCH: + if (eventData.data.isStart) { + animateTo({ duration: 2000 }, () => { + this.searchResultPlayLists = [] + }) + } + animateTo( + { duration: 1000 }, + () => { + if (eventData.data !== undefined && eventData.data.item !== undefined) { + console.info('UPDATE_USER_LIST_SEARCH:' + (eventData.data.item as PlayListData).title); + this.searchResultPlayLists.push(eventData.data.item as PlayListData) + } + }) + break; + case ServerConstants.UPDATE_USER_LIST_FAVOR: + let existingIndex = this.customPlayLists.findIndex(item => item.title === ServerConstants.LIST_SONG_FAVOURITE); + animateTo( + { duration: 1000 }, + () => { + if (eventData.data !== undefined) { + if (eventData.data.item !== undefined) { + console.info('UPDATE_USER_LIST_FAVOR:' + (eventData.data.item as PlayListData).title); + if (existingIndex !== -1) { + this.customPlayLists[existingIndex] = eventData.data.item as PlayListData; + } else { + this.customPlayLists.unshift(eventData.data.item as PlayListData); + } + } else { + if (existingIndex !== -1) { + this.customPlayLists.splice(existingIndex, 1); + } + } + } + }) + break; + case ServerConstants.UPDATE_USER_LIST_SINGER: + if (eventData.data.isStart) { + animateTo({ duration: 2000 }, () => { + this.singerList = [] + }) + } + animateTo( + { duration: 1000 }, + () => { + if (eventData.data !== undefined && eventData.data.item !== undefined) { + console.info('UPDATE_USER_LIST_SINGER:' + (eventData.data.item as PlayListData).title); + this.singerList.push(eventData.data.item as PlayListData); + this.SingerAngle = 180; + } + }) + break; + default: + break; + } + } + } } \ No newline at end of file diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayList.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayList.ets index cc6d35ff..26ae890b 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayList.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayList.ets @@ -37,14 +37,7 @@ export default struct PlayList { }, 2000) emitter.on(ServerConstants.PLAYLIST_UPDATE_STATE, (eventData: emitter.EventData) => { - if (eventData !== undefined && eventData.data !== undefined && eventData.data.state === ServerConstants.PLAYER_STATE_PLAYING) { - animateTo({ duration: 2000 }, () => { - this.playingList = this.playerManager.getCurrentPlayList(); - }) - setTimeout(() => { - this.scrollerForList.scrollToIndex(this.playingList.indexOf(this.playerManager.getItem()), true, ScrollAlign.CENTER); - }, 2000) - } + this.updateState(eventData); }); } @@ -82,4 +75,15 @@ export default struct PlayList { .backgroundColor(Color.White) .borderRadius({ topLeft: '18vp', topRight: '18vp' }) } + + updateState(eventData: emitter.EventData) { + if (eventData !== undefined && eventData.data !== undefined && eventData.data.state === ServerConstants.PLAYER_STATE_PLAYING) { + animateTo({ duration: 2000 }, () => { + this.playingList = this.playerManager.getCurrentPlayList(); + }) + setTimeout(() => { + this.scrollerForList.scrollToIndex(this.playingList.indexOf(this.playerManager.getItem()), true, ScrollAlign.CENTER); + }, 2000) + } + } } diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerBar.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerBar.ets index ef4e1de9..e38eeba4 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerBar.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerBar.ets @@ -39,31 +39,16 @@ export default struct PlayerBar { aboutToAppear() { Logger.info(this.tag, 'aboutToAppear'); emitter.on(ServerConstants.MAIN_UPDATE_TIME, (eventData: emitter.EventData) => { - if (eventData !== undefined && eventData.data !== undefined) { - this.currentTime = eventData.data.currentTime; - this.durationTime = eventData.data.currentDuration; - } + this.updateTime(eventData); }); emitter.on(ServerConstants.MAIN_UPDATE_STATE, (eventData: emitter.EventData) => { - if (eventData !== undefined && eventData.data !== undefined) { - Logger.info(this.tag, 'state:' + eventData.data.state); - if (this.state !== ServerConstants.PLAYER_STATE_PLAYING && eventData.data.state === ServerConstants.PLAYER_STATE_PLAYING) { - this.item = this.PlayerManager.getItem(); - if (this.rotateAngle === 0) { - this.rotateAngle = 360; - Logger.info(this.tag, 'rotateAngle:360'); - } - } - if (eventData.data.state !== ServerConstants.PLAYER_STATE_PLAYING) { - this.rotateAngle = 0; - Logger.info(this.tag, 'rotateAngle:0'); - } - this.state = eventData.data.state; - } + this.updateState(eventData) }); } aboutToDisappear() { + emitter.off(ServerConstants.MAIN_UPDATE_TIME); + emitter.off(ServerConstants.MAIN_UPDATE_STATE); Logger.info(this.tag, 'aboutToDisappear'); } @@ -151,4 +136,28 @@ export default struct PlayerBar { } .width('100%') } + + updateTime(eventData: emitter.EventData) { + if (eventData !== undefined && eventData.data !== undefined) { + this.currentTime = eventData.data.currentTime; + this.durationTime = eventData.data.currentDuration; + } + } + updateState(eventData: emitter.EventData) { + if (eventData !== undefined && eventData.data !== undefined) { + Logger.info(this.tag, 'state:' + eventData.data.state); + if (this.state !== ServerConstants.PLAYER_STATE_PLAYING && eventData.data.state === ServerConstants.PLAYER_STATE_PLAYING) { + this.item = this.PlayerManager.getItem(); + if (this.rotateAngle === 0) { + this.rotateAngle = 360; + Logger.info(this.tag, 'rotateAngle:360'); + } + } + if (eventData.data.state !== ServerConstants.PLAYER_STATE_PLAYING) { + this.rotateAngle = 0; + Logger.info(this.tag, 'rotateAngle:0'); + } + this.state = eventData.data.state; + } + } } \ No newline at end of file diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerDetail.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerDetail.ets index d5a84ac8..12ea409a 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerDetail.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerDetail.ets @@ -117,50 +117,6 @@ export default struct PlayerDetail { } aboutToAppear() { - emitter.on(ServerConstants.DETAIL_UPDATE_FAVOR, () => { - animateTo({ duration: 2000 }, () => { - this.item = this.PlayerManager.getItem(); - }) - this.noItem = false; - }); - emitter.on(ServerConstants.DETAIL_UPDATE_TIME, (eventData: emitter.EventData) => { - if (eventData !== undefined && eventData.data !== undefined) { - //Logger.info(this.tag, 'currentTime:' + eventData.data.currentTime); - this.currentTime = eventData.data.currentTime; - this.durationTime = eventData.data.currentDuration; - let newLrcIndex = Math.floor(this.currentTime * this.lrc.length / this.durationTime); - if (newLrcIndex !== this.lrcIndex) { - this.lrcIndex = newLrcIndex; - this.scroller.scrollToIndex(this.lrcIndex - 5, true) - } - } - }); - emitter.on(ServerConstants.DETAIL_UPDATE_STATE, (eventData: emitter.EventData) => { - if (eventData !== undefined && eventData.data !== undefined) { - Logger.info(this.tag, 'state:' + eventData.data.state); - if (eventData.data.state === ServerConstants.PLAYER_STATE_IDLE) { - this.noItem = true; - } - if (this.state !== ServerConstants.PLAYER_STATE_PLAYING && eventData.data.state === ServerConstants.PLAYER_STATE_PLAYING) { - this.item = this.PlayerManager.getItem(); - this.noItem = false; - this.lrc = CommonUtils.getLrc(this.item.title); - this.listTitle = this.PlayerManager.getListTitle(); - this.scroller.scrollToIndex(0, true) - setTimeout(() => { - if (this.rotateAngle === 0) { - this.rotateAngle = 360; - Logger.info(this.tag, 'rotateAngle:360'); - } - }, 100); - } - if (eventData.data.state !== ServerConstants.PLAYER_STATE_PLAYING) { - this.rotateAngle = 0; - Logger.info(this.tag, 'rotateAngle:0'); - } - this.state = eventData.data.state; - } - }); if (this.state === ServerConstants.PLAYER_STATE_PLAYING) { setTimeout(() => { if (this.rotateAngle === 0) { @@ -169,13 +125,23 @@ export default struct PlayerDetail { } }, 100); } + + emitter.on(ServerConstants.DETAIL_UPDATE_FAVOR_AND_QUALITY, () => { + this.updateFavor(); + }); + emitter.on(ServerConstants.DETAIL_UPDATE_TIME, (eventData: emitter.EventData) => { + this.updateTime(eventData); + }); + emitter.on(ServerConstants.DETAIL_UPDATE_STATE, (eventData: emitter.EventData) => { + this.updateState(eventData); + }); } aboutToDisappear() { Logger.info(this.tag, 'aboutToDisappear'); emitter.off(ServerConstants.DETAIL_UPDATE_TIME); emitter.off(ServerConstants.DETAIL_UPDATE_STATE); - emitter.off(ServerConstants.DETAIL_UPDATE_FAVOR); + emitter.off(ServerConstants.DETAIL_UPDATE_FAVOR_AND_QUALITY); if (this.dialogController !== null) { this.dialogController.close(); this.isDialogOpened = false; @@ -253,7 +219,7 @@ export default struct PlayerDetail { if (this.dialogController != null) { this.listOptions = []; for (let item of this.customPlayLists) { - if(item.title !== ServerConstants.LIST_SONG_FAVOURITE && item.title !== ServerConstants.LIST_SONG_RECENT){ + if (item.title !== ServerConstants.LIST_SONG_FAVOURITE && item.title !== ServerConstants.LIST_SONG_RECENT) { this.listOptions.push({ value: item.title }); } } @@ -402,7 +368,7 @@ export default struct PlayerDetail { Row() { Text(CommonUtils.formatTime(this.currentTime / 1000)).fontSize('12fp') .id('detail_time_current') - if (this.item.quality === '.flac'){ + if (this.item.quality === '.flac') { Text('无损') .id('high_quality') } @@ -472,4 +438,50 @@ export default struct PlayerDetail { .height('100%') .backgroundColor(Color.White) } + + updateFavor() { + animateTo({ duration: 2000 }, () => { + this.item = this.PlayerManager.getItem(); + }) + this.noItem = false; + } + + updateTime(eventData: emitter.EventData) { + if (eventData !== undefined && eventData.data !== undefined) { + this.currentTime = eventData.data.currentTime; + this.durationTime = eventData.data.currentDuration; + let newLrcIndex = Math.floor(this.currentTime * this.lrc.length / this.durationTime); + if (newLrcIndex !== this.lrcIndex) { + this.lrcIndex = newLrcIndex; + this.scroller.scrollToIndex(this.lrcIndex - 5, true) + } + } + } + + updateState(eventData: emitter.EventData) { + if (eventData !== undefined && eventData.data !== undefined) { + Logger.info(this.tag, 'state:' + eventData.data.state); + if (eventData.data.state === ServerConstants.PLAYER_STATE_IDLE) { + this.noItem = true; + } + if (this.state !== ServerConstants.PLAYER_STATE_PLAYING && eventData.data.state === ServerConstants.PLAYER_STATE_PLAYING) { + this.item = this.PlayerManager.getItem(); + this.noItem = false; + this.lrc = CommonUtils.getLrc(this.item.title); + this.listTitle = this.PlayerManager.getListTitle(); + this.scroller.scrollToIndex(0, true) + setTimeout(() => { + if (this.rotateAngle === 0) { + this.rotateAngle = 360; + Logger.info(this.tag, 'rotateAngle:360'); + } + }, 100); + } + if (eventData.data.state !== ServerConstants.PLAYER_STATE_PLAYING) { + this.rotateAngle = 0; + Logger.info(this.tag, 'rotateAngle:0'); + } + this.state = eventData.data.state; + } + } } -- Gitee From df89197afe2e8d09cee03a082fb5671582e0d69d Mon Sep 17 00:00:00 2001 From: sunlian Date: Wed, 25 Sep 2024 15:33:25 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E9=A6=96=E9=A1=B5=E6=8E=A8=E8=8D=90?= =?UTF-8?q?=E7=83=AD=E9=97=A8=E6=AD=8C=E6=89=8B=EF=BC=8Chttp=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: sunlian --- scenario/MusicPlayerOnline/build-profile.json5 | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/scenario/MusicPlayerOnline/build-profile.json5 b/scenario/MusicPlayerOnline/build-profile.json5 index 4fac6ae1..08c1020e 100644 --- a/scenario/MusicPlayerOnline/build-profile.json5 +++ b/scenario/MusicPlayerOnline/build-profile.json5 @@ -1,19 +1,6 @@ { "app": { - "signingConfigs": [ - { - "name": "default", - "material": { - "certpath": "C:/Users/Administrator/.ohos/config/openharmony/default_MusicPlayerOnline_hk2UHQE6-ErE070Ws-glQcy1fqzzWJa-YFhy3hlWKdg=.cer", - "storePassword": "0000001AB22FBDBD4816BDDE71C489BBC1F9A76F7472B473A58321B20952F07AA2A077937F0FC72BF3F2", - "keyAlias": "debugKey", - "keyPassword": "0000001A240E59627ABDDF94AD4BE975DCAA2B17D1D2D6E89305F7794E5506071F20FA9E0AE0AF6600E1", - "profile": "C:/Users/Administrator/.ohos/config/openharmony/default_MusicPlayerOnline_hk2UHQE6-ErE070Ws-glQcy1fqzzWJa-YFhy3hlWKdg=.p7b", - "signAlg": "SHA256withECDSA", - "storeFile": "C:/Users/Administrator/.ohos/config/openharmony/default_MusicPlayerOnline_hk2UHQE6-ErE070Ws-glQcy1fqzzWJa-YFhy3hlWKdg=.p12" - } - } - ], + "signingConfigs": [], "products": [ { "name": "default", -- Gitee