From 9a2e88d01614822b7ff96da416804320e84e6cda Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Mon, 13 Jul 2020 16:39:24 +0800 Subject: [PATCH] add on click events for items --- src/app/app.component.html | 6 +- src/app/app.component.ts | 27 ++++++-- src/app/app.module.ts | 6 +- src/app/dataprovider.service.ts | 79 ++++++++++++++++++++-- src/app/filelist.ts | 21 ++++-- src/app/foldernav/foldernav.component.html | 2 +- src/app/foldernav/foldernav.component.ts | 7 +- src/app/playlist/playlist.component.html | 8 +-- src/app/playlist/playlist.component.ts | 14 +++- 9 files changed, 141 insertions(+), 29 deletions(-) diff --git a/src/app/app.component.html b/src/app/app.component.html index 2a0d526..54005d2 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -3,19 +3,19 @@ Folders - + - PCM + Private Cloud Music - + \ No newline at end of file diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 0c52e41..8d104b7 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -10,21 +10,36 @@ import { FileList, MusicItem } from './filelist'; export class AppComponent { title = 'Private Cloud Music'; playlists: string[] = []; + currentPlaylist: string; filelists: FileList; constructor(public provider : DataproviderService) {} ngOnInit() : void { - this.fetchPlaylists(); + this.fetchDefaultPlaylists(); } - fetchPlaylists() : void { - this.provider.getFileList("/") + fetchDefaultPlaylists() : void { + this.provider.getFileList("") .subscribe(filelist => { this.playlists = filelist.subFolderList; - let firstFolder = this.playlists[0]; - this.provider.getFileList(firstFolder) - .subscribe(filelist => this.filelists = filelist) + console.log(this.playlists); + this.fetchPlaylist(this.playlists[0]); }); } + + fetchPlaylist(playlistName : string) : void { + this.currentPlaylist = playlistName; + console.log(playlistName); + this.provider.getFileList(playlistName) + .subscribe(filelist => this.filelists = filelist) + } + + onRequestPlaylistEvent(playlistName : string) : void { + this.fetchPlaylist(playlistName); + } + + onRequestPlaybackEvent(musicItemName : string) : void { + console.log('baseurl/' + this.currentPlaylist + '/' + musicItemName); + } } diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 97105f4..b6ef6fb 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -1,13 +1,14 @@ import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; -import { AppComponent } from './app.component'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { MatSidenavModule } from '@angular/material/sidenav' import { MatListModule } from '@angular/material/list' import { MatToolbarModule } from '@angular/material/toolbar' import { MatIconModule } from '@angular/material/icon' import { MatButtonModule } from '@angular/material/button'; +import { HttpClientModule } from '@angular/common/http' +import { AppComponent } from './app.component'; import { FoldernavComponent } from './foldernav/foldernav.component'; import { PlaylistComponent } from './playlist/playlist.component'; @@ -24,7 +25,8 @@ import { PlaylistComponent } from './playlist/playlist.component'; MatIconModule, MatButtonModule, BrowserModule, - BrowserAnimationsModule + BrowserAnimationsModule, + HttpClientModule, ], providers: [], bootstrap: [AppComponent] diff --git a/src/app/dataprovider.service.ts b/src/app/dataprovider.service.ts index b010025..93e6bfa 100644 --- a/src/app/dataprovider.service.ts +++ b/src/app/dataprovider.service.ts @@ -1,14 +1,31 @@ import { Injectable } from '@angular/core'; -import { FileList } from './filelist' +import { FileList, FileListResponse } from './filelist' import { Observable, of } from 'rxjs'; +import { catchError, map, tap } from 'rxjs/operators'; +import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'; @Injectable({ providedIn: 'root' }) export class DataproviderService { - constructor() { - this.mock_FILELIST = { + private apiUrl = "http://127.0.0.1:616/api.php"; + + constructor( + private http: HttpClient + ) { + this.mock_EMPTYFILELIST = { + subFolderList: [], + musicList: [ + { + fileName: "", + fileSize: 0, + modifiedTime: 0 + } + ] + } + + this.mock_BASEFILELIST = { subFolderList: ["AAAA", "BBBB"], musicList: [ { @@ -18,11 +35,63 @@ export class DataproviderService { } ] } + + this.mock_FILELISTAAAA = { + subFolderList: ["DDDD", "EEEE"], + musicList: [ + { + fileName: "EEEEE.mp3", + fileSize: 9951652, + modifiedTime: 1556423252 + } + ] + } + + this.mock_FILELISTBBBB = { + subFolderList: ["FFFF", "GGGG"], + musicList: [ + { + fileName: "FFFFF.mp3", + fileSize: 9951652, + modifiedTime: 1556423252 + } + ] + } } - mock_FILELIST : FileList; + mock_EMPTYFILELIST : FileList; + mock_BASEFILELIST : FileList; + mock_FILELISTAAAA : FileList; + mock_FILELISTBBBB : FileList; getFileList(path : string) : Observable { - return of(this.mock_FILELIST); + switch (path) { + case "AAAA": + return of(this.mock_FILELISTAAAA); + case "BBBB": + return of(this.mock_FILELISTBBBB); + default: + return of(this.mock_BASEFILELIST); + } + } + + getFileListReal(path : string) : Observable { + const body = new HttpParams() + .set('do', 'getfilelist') + .set('folder', path); + return this.http.post(this.apiUrl, body.toString(), { + headers: new HttpHeaders() + .set('Content-Type', 'application/x-www-form-urlencoded') + }).pipe( + map(h => h.result.data), + catchError(this.handleError(this.mock_EMPTYFILELIST)) + ); + } + + private handleError(result?: T) { + return (error: any): Observable => { + console.error(error); // log to console instead + return of(result as T); + }; } } diff --git a/src/app/filelist.ts b/src/app/filelist.ts index 28331b9..642b891 100644 --- a/src/app/filelist.ts +++ b/src/app/filelist.ts @@ -1,10 +1,21 @@ export interface MusicItem { - fileName : string; - fileSize : number; - modifiedTime : number; + fileName: string; + fileSize: number; + modifiedTime: number; } export interface FileList { - subFolderList : string[]; - musicList : MusicItem[]; + subFolderList: string[]; + musicList: MusicItem[]; +} + +export interface FileListResult { + type: string; + data: FileList; +} + +export interface FileListResponse { + status: number; + message: string; + result: FileListResult; } \ No newline at end of file diff --git a/src/app/foldernav/foldernav.component.html b/src/app/foldernav/foldernav.component.html index 92ae2e2..d6ce53d 100644 --- a/src/app/foldernav/foldernav.component.html +++ b/src/app/foldernav/foldernav.component.html @@ -1,3 +1,3 @@ - {{ playlist }} + {{ playlist }} \ No newline at end of file diff --git a/src/app/foldernav/foldernav.component.ts b/src/app/foldernav/foldernav.component.ts index 2668d72..2dbcb3b 100644 --- a/src/app/foldernav/foldernav.component.ts +++ b/src/app/foldernav/foldernav.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit, Input } from '@angular/core'; +import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'app-foldernav', @@ -8,9 +8,14 @@ import { Component, OnInit, Input } from '@angular/core'; export class FoldernavComponent implements OnInit { @Input() playlists : string[] = []; + @Output() requestPlaylistEvent = new EventEmitter(); constructor() { } ngOnInit(): void { } + + requestPlaylist(playlistName : string): void { + this.requestPlaylistEvent.emit(playlistName); + } } diff --git a/src/app/playlist/playlist.component.html b/src/app/playlist/playlist.component.html index e8d4bca..a777e44 100644 --- a/src/app/playlist/playlist.component.html +++ b/src/app/playlist/playlist.component.html @@ -1,10 +1,10 @@ - + folder - {{ folderItem }} + {{ folderItem }} - + audiotrack - {{ musicItem.fileName }} + {{ musicItem.fileName }} \ No newline at end of file diff --git a/src/app/playlist/playlist.component.ts b/src/app/playlist/playlist.component.ts index 0a03300..b99c85d 100644 --- a/src/app/playlist/playlist.component.ts +++ b/src/app/playlist/playlist.component.ts @@ -1,5 +1,5 @@ -import { Component, OnInit, Input } from '@angular/core'; -import { FileList } from '../filelist' +import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core'; +import { FileList, MusicItem } from '../filelist' @Component({ selector: 'app-playlist', @@ -9,9 +9,19 @@ import { FileList } from '../filelist' export class PlaylistComponent implements OnInit { @Input() filelist : FileList; + @Output() requestPlaylistEvent = new EventEmitter(); + @Output() requestPlaybackEvent = new EventEmitter(); constructor() { } ngOnInit(): void { } + + requestPlaylist(playlistName : string): void { + this.requestPlaylistEvent.emit(playlistName); + } + + requestPlayback(musicItem : MusicItem): void { + this.requestPlaybackEvent.emit(musicItem.fileName); + } }