diff --git a/src/app/components/browse/result-table/result-table.component.html b/src/app/components/browse/result-table/result-table.component.html index 5c3d0da..c10b968 100644 --- a/src/app/components/browse/result-table/result-table.component.html +++ b/src/app/components/browse/result-table/result-table.component.html @@ -1,6 +1,5 @@ - +
- @@ -10,10 +9,10 @@ - - - - + + + + diff --git a/src/app/components/browse/result-table/result-table.component.ts b/src/app/components/browse/result-table/result-table.component.ts index 3fe2b23..71b4d3d 100644 --- a/src/app/components/browse/result-table/result-table.component.ts +++ b/src/app/components/browse/result-table/result-table.component.ts @@ -5,6 +5,7 @@ import { CheckboxDirective } from '../../../core/directives/checkbox.directive' import { SearchService } from '../../../core/services/search.service' import { SelectionService } from '../../../core/services/selection.service' import { SettingsService } from 'src/app/core/services/settings.service' +import Comparators from 'comparators' @Component({ selector: 'app-result-table', @@ -20,6 +21,8 @@ export class ResultTableComponent implements OnInit { results: SongResult[] activeRowID: number = null + sortDirection: 'ascending' | 'descending' = 'descending' + sortColumn: 'name' | 'artist' | 'album' | 'genre' | null = null constructor( private searchService: SearchService, @@ -35,6 +38,11 @@ export class ResultTableComponent implements OnInit { this.searchService.onSearchChanged(results => { this.activeRowID = null this.results = results + this.updateSort() + }) + + this.searchService.onNewSearch(() => { + this.sortColumn = null }) } @@ -43,6 +51,24 @@ export class ResultTableComponent implements OnInit { this.rowClicked.emit(result) } + onColClicked(column: 'name' | 'artist' | 'album' | 'genre') { + if (this.sortColumn != column) { + this.sortColumn = column + this.sortDirection = 'descending' + } else if (this.sortDirection == 'descending') { + this.sortDirection = 'ascending' + } else { + this.sortDirection = 'descending' + } + this.updateSort() + } + + private updateSort() { + if (this.sortColumn != null) { + this.results.sort(Comparators.comparing(this.sortColumn, { reversed: this.sortDirection == 'ascending' })) + } + } + /** * Called when the user checks the `checkboxColumn`. */ diff --git a/src/app/core/services/search.service.ts b/src/app/core/services/search.service.ts index 5379402..8c64774 100644 --- a/src/app/core/services/search.service.ts +++ b/src/app/core/services/search.service.ts @@ -32,8 +32,8 @@ export class SearchService { } this.awaitingResults = false - this.resultsChangedEmitter.emit(this.results) this.newResultsEmitter.emit(this.results) + this.resultsChangedEmitter.emit(this.results) } isLoading() { @@ -43,7 +43,7 @@ export class SearchService { /** * Event emitted when new search results are returned * or when more results are added to an existing search. - * (emitted before `onNewSearch`) + * (emitted after `onNewSearch`) */ onSearchChanged(callback: (results: SongResult[]) => void) { this.resultsChangedEmitter.subscribe(callback) @@ -51,7 +51,7 @@ export class SearchService { /** * Event emitted when a new search query is typed in. - * (emitted after `onSearchChanged`) + * (emitted before `onSearchChanged`) */ onNewSearch(callback: (results: SongResult[]) => void) { this.newResultsEmitter.subscribe(callback)
NameArtistAlbumGenreNameArtistAlbumGenre