diff --git a/README.md b/README.md index 5cd200932bee365e196190ba0900a697d6e5b1f3..1ec3b5e91ed0141a41a20e78220f32d83ace6836 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) | 详细测试项参考各模块说明。 diff --git a/scenario/MusicPlayerOnline/README_zh.md b/scenario/MusicPlayerOnline/README_zh.md index dbea8b082431c5e54bbab90eeba088de721070c3..33f7ff4085eacb060505b68a9d6014b8e475395a 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 a60181d1fd18899feedb9aa5c5fbe5caec60698e..87f26ac2f756ab25511cb7afc9029f626bf4e028 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_AND_QUALITY = '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 0000000000000000000000000000000000000000..59411276151d36c92c48f75e2aaf7d69181f1788 --- /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.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_AND_QUALITY, 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_AND_QUALITY, 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"]); + prompt.showToast({ + message: jsonObject["message"] + }) + this.getCustomListFromServer(); + } 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 79f3c75ce155396333768d94a60c2a639ee72a11..7e65f9b75d2e998475140e3a6ce2d0f740d6f9a6 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 fa80dcd1853925b719e5858c0807737d56ce4b8f..7e58bb29cc133d22c30854559e6148089d8269d6 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/pages/Index.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/pages/Index.ets @@ -16,17 +16,15 @@ 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'; 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); @@ -34,14 +32,15 @@ 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[] = []; + @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,20 +48,21 @@ 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 = '' - dialogController: CustomDialogController | null = new CustomDialogController({ + private dialogController: CustomDialogController | null = new CustomDialogController({ builder: LoginCustomDialog({ cancel: () => { if (this.dialogController !== null) { this.dialogController.close() + this.loginButton = ServerConstants.LOGIN_BUTTON_IN; } }, confirm: (isRegister: boolean) => { @@ -98,37 +98,27 @@ 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) => { + 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_CUSTOM: - this.getCustomListFromServer(); - break; - case ServerConstants.UPDATE_USER_LIST_RECENT: - this.getRecentListFromServer(); - break; - case ServerConstants.UPDATE_USER_LIST_FAVOR: - this.getFavorListFromServer(); - break; - default: - break; - } - } + this.updateLists(eventData); }); } aboutToDisappear(): void { - emitter.off(ServerConstants.UPDATE_TIME_EVENT_ID); - emitter.off(ServerConstants.UPDATE_STATE_EVENT_ID); emitter.off(ServerConstants.UPDATE_USER_LIST); + emitter.off(ServerConstants.LOGIN_RESULT); if (this.dialogController !== null) { this.dialogController.close(); this.dialogController = null; @@ -232,8 +222,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 +277,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 +311,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 +374,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 +443,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,462 +545,156 @@ 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: '两次密码输入不同!' - }) + this.loginButton = ServerConstants.LOGIN_INFO_ING; + HttpManager.getInstance().register(userName, pwd, pwd2); + } + + searchSongsFromServer(search_word: string) { + search_word = search_word.trim(); + if (!search_word) { + prompt.showToast({ message: ServerConstants.EMPTY_SEARCH_WORD }); 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 + animateTo( + { duration: 1000 }, + () => { + this.searchSongTitle = search_word + ServerConstants.SEARCHING; + this.searchResultPlayLists = []; }) - } + HttpManager.getInstance().searchSongsFromServer(search_word); } - 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) - }) + 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(); } - }); - } catch (err) { - console.info('HttpRequest error:' + JSON.stringify(err)); - prompt.showToast({ - message: JSON.stringify(err) - }) + break; + case ServerConstants.LOGIN_RESULT_FAILED: + this.loginButton = ServerConstants.LOGIN_BUTTON_IN; + break; + default: + break; + } } } - getFavorListFromServer() { - try { - this.httpRequest.request(ServerConstants.FAVOR_LIST_URL, - { - method: http.RequestMethod.GET, - header: { - 'Cookie': AppStorage.get('loginCookie') + 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 = []; + }) } - }, - (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); + 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); } - 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) + }) + break; + case ServerConstants.UPDATE_USER_LIST_CUSTOM: + if (eventData.data.isStart) { + animateTo({ duration: 2000 }, () => { + this.customPlayLists = []; }) } - }); - } 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); - } - }) - }); + 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); } - 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) + }) + 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 = [] }) } - }); - } 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; + 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) } - - let jsonObject: PlaylistItem[] = JSON.parse(data.result as string); - let groupedByPlaylistName = jsonObject.reduce((accumulator, item) => { - if (!accumulator[item.playlist_name]) { - accumulator[item.playlist_name] = []; + }) + 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); + } } - 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)); - } - } - - searchSongsFromServer(search_word: string) { - search_word = search_word.trim(); - if (!search_word) { - prompt.showToast({ message: ServerConstants.EMPTY_SEARCH_WORD }); - return; - } - animateTo( - { duration: 1000 }, - () => { - 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) + break; + case ServerConstants.UPDATE_USER_LIST_SINGER: + if (eventData.data.isStart) { + animateTo({ duration: 2000 }, () => { + this.singerList = [] }) - this.searchSongTitle = ServerConstants.SEARCH_DEFAULT; } - }); - } catch (err) { - console.info('HttpRequest error:' + JSON.stringify(err)); - this.searchSongTitle = ServerConstants.SEARCH_DEFAULT; + 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 ad1c96357adb507ff9783cf15a57c2040b60c099..26ae890bdd5deb05a738bd6d919d46d2b86860b8 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayList.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayList.ets @@ -35,19 +35,14 @@ 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) => { - 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) - } + + emitter.on(ServerConstants.PLAYLIST_UPDATE_STATE, (eventData: emitter.EventData) => { + this.updateState(eventData); }); } aboutToDisappear(): void { + emitter.off(ServerConstants.PLAYLIST_UPDATE_STATE); } build() { @@ -80,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/PlayListItem.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayListItem.ets index 42b7290704abb40ce886ba7f726dfde5ae64d887..4d84ee4c42e9d337a13aaa0ef73dcc11cada9287 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 2b9944ba43cbab5d59f1e08538cab22ac54e0bda..e38eeba437cfdc18874a09283bdbc1f5103b2e93 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerBar.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerBar.ets @@ -38,36 +38,18 @@ export default struct PlayerBar { aboutToAppear() { Logger.info(this.tag, 'aboutToAppear'); - emitter.on(ServerConstants.UPDATE_TIME_EVENT_ID, (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.MAIN_UPDATE_TIME, (eventData: emitter.EventData) => { + this.updateTime(eventData); }); - emitter.on(ServerConstants.UPDATE_STATE_EVENT_ID, (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; - } + emitter.on(ServerConstants.MAIN_UPDATE_STATE, (eventData: emitter.EventData) => { + this.updateState(eventData) }); } aboutToDisappear() { + emitter.off(ServerConstants.MAIN_UPDATE_TIME); + emitter.off(ServerConstants.MAIN_UPDATE_STATE); Logger.info(this.tag, 'aboutToDisappear'); - emitter.off(ServerConstants.UPDATE_TIME_EVENT_ID); - emitter.off(ServerConstants.UPDATE_STATE_EVENT_ID); } build() { @@ -154,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 7fde4661f072939d36e85a6936a69222b12ee12e..12ea409a8f286c81bac0426aaac2b2c8340e4c39 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,50 +117,6 @@ export default struct PlayerDetail { } aboutToAppear() { - emitter.on(ServerConstants.UPDATE_FAVOR_EVENT_ID_DETAIL, () => { - animateTo({ duration: 2000 }, () => { - this.item = this.PlayerManager.getItem(); - }) - this.noItem = false; - }); - emitter.on(ServerConstants.UPDATE_TIME_EVENT_ID_DETAIL, (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.UPDATE_STATE_EVENT_ID_DETAIL, (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) { @@ -168,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.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_AND_QUALITY); if (this.dialogController !== null) { this.dialogController.close(); this.isDialogOpened = false; @@ -252,7 +219,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 +282,7 @@ export default struct PlayerDetail { .margin({ right: '8vp' }) .clickEffect(UIConstants.CLICK_EFFECT) .onClick(() => { - this.PlayerManager.set_favourite(); + HttpManager.getInstance().set_favourite(); }) } .width('80%') @@ -399,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') } @@ -469,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; + } + } } diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/view/addToListDialog.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/view/addToListDialog.ets index 28648afca0dead12a3ac989a54225d317a7cd8f2..cbab527097d3fb5cb922e4cce4e57d6d873ab12c 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 7484cf4f6cea4c9b5329e487f907538aee2ed3fb..b901aa71c8cea68e542869ad9f521a7c94733186 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 683bb2c43861192546cdb62108feb55b89067534..ac57a47fc9858cf918b9cce4e890b7b125489730 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 99e4d74f87b14bd3e1269bb1861f19e7f3b5572c..37be2ffa4a825efec53e74dfabf7130fa677607f 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": "我的歌单"