Skip to content

Commit

Permalink
Artist/Albums/Songs: sort by rating
Browse files Browse the repository at this point in the history
  • Loading branch information
BLeeEZ committed May 15, 2022
1 parent e8b3042 commit 462f75a
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 32 deletions.
63 changes: 59 additions & 4 deletions Amperfy/Screens/ViewController/AlbumsVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ class AlbumsVC: SingleFetchedResultsTableViewController<AlbumMO> {

private var fetchedResultsController: AlbumFetchedResultsController!
private var filterButton: UIBarButtonItem!
private var sortButton: UIBarButtonItem!
private var displayFilter: DisplayCategoryFilter = .all
private var sortType: ElementSortType = .name

override func viewDidLoad() {
super.viewDidLoad()
appDelegate.userStatistics.visited(.albums)

fetchedResultsController = AlbumFetchedResultsController(managedObjectContext: appDelegate.persistentStorage.context, isGroupedInAlphabeticSections: true)
singleFetchedResultsController = fetchedResultsController
change(sortType: appDelegate.persistentStorage.settings.albumsSortSetting)

configureSearchController(placeholder: "Search in \"Albums\"", scopeButtonTitles: ["All", "Cached"], showSearchBarAtEnter: false)
tableView.register(nibName: GenericTableCell.typeName)
tableView.rowHeight = GenericTableCell.rowHeight

filterButton = UIBarButtonItem(image: UIImage.filter, style: .plain, target: self, action: #selector(filterButtonPressed))
navigationItem.rightBarButtonItem = filterButton
sortButton = UIBarButtonItem(image: UIImage.sort, style: .plain, target: self, action: #selector(sortButtonPressed))
navigationItem.rightBarButtonItems = [filterButton, sortButton]
self.refreshControl?.addTarget(self, action: #selector(Self.handleRefresh), for: UIControl.Event.valueChanged)

containableAtIndexPathCallback = { (indexPath) in
Expand All @@ -33,6 +35,17 @@ class AlbumsVC: SingleFetchedResultsTableViewController<AlbumMO> {
}
}

func change(sortType: ElementSortType) {
self.sortType = sortType
appDelegate.persistentStorage.settings.albumsSortSetting = sortType
singleFetchedResultsController?.clearResults()
tableView.reloadData()
fetchedResultsController = AlbumFetchedResultsController(managedObjectContext: appDelegate.persistentStorage.context, sortType: sortType, isGroupedInAlphabeticSections: true)
fetchedResultsController.fetchResultsController.sectionIndexType = sortType == .rating ? .rating : .alphabet
singleFetchedResultsController = fetchedResultsController
tableView.reloadData()
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
updateFilterButton()
Expand All @@ -49,6 +62,28 @@ class AlbumsVC: SingleFetchedResultsTableViewController<AlbumMO> {
return cell
}

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
switch sortType {
case .name:
return 0.0
case .rating:
return CommonScreenOperations.tableSectionHeightLarge
}
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch sortType {
case .name:
return super.tableView(tableView, titleForHeaderInSection: section)
case .rating:
if let sectionNameInitial = super.tableView(tableView, titleForHeaderInSection: section), sectionNameInitial != SectionIndexType.noRatingIndexSymbol {
return "\(sectionNameInitial) Star\(sectionNameInitial != "1" ? "s" : "")"
} else {
return "Not rated"
}
}
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let album = fetchedResultsController.getWrappedEntity(at: indexPath)
performSegue(withIdentifier: Segues.toAlbumDetail.rawValue, sender: album)
Expand All @@ -74,7 +109,27 @@ class AlbumsVC: SingleFetchedResultsTableViewController<AlbumMO> {
fetchedResultsController.search(searchText: searchText, onlyCached: searchController.searchBar.selectedScopeButtonIndex == 1, displayFilter: displayFilter)
tableView.reloadData()
}


@objc private func sortButtonPressed() {
let alert = UIAlertController(title: "Albums sorting", message: nil, preferredStyle: .actionSheet)
if sortType != .name {
alert.addAction(UIAlertAction(title: "Sort by name", style: .default, handler: { _ in
self.change(sortType: .name)
self.updateSearchResults(for: self.searchController)
}))
}
if sortType != .rating {
alert.addAction(UIAlertAction(title: "Sort by rating", style: .default, handler: { _ in
self.change(sortType: .rating)
self.updateSearchResults(for: self.searchController)
}))
}
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
alert.pruneNegativeWidthConstraintsToAvoidFalseConstraintWarnings()
alert.setOptionsForIPadToDisplayPopupCentricIn(view: self.view)
present(alert, animated: true, completion: nil)
}

@objc private func filterButtonPressed() {
let alert = UIAlertController(title: "Albums filter", message: nil, preferredStyle: .actionSheet)

Expand Down
61 changes: 58 additions & 3 deletions Amperfy/Screens/ViewController/ArtistsVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ class ArtistsVC: SingleFetchedResultsTableViewController<ArtistMO> {

private var fetchedResultsController: ArtistFetchedResultsController!
private var filterButton: UIBarButtonItem!
private var sortButton: UIBarButtonItem!
private var displayFilter: DisplayCategoryFilter = .all
private var sortType: ElementSortType = .name

override func viewDidLoad() {
super.viewDidLoad()
appDelegate.userStatistics.visited(.artists)

fetchedResultsController = ArtistFetchedResultsController(managedObjectContext: appDelegate.persistentStorage.context, isGroupedInAlphabeticSections: true)
singleFetchedResultsController = fetchedResultsController
change(sortType: appDelegate.persistentStorage.settings.artistsSortSetting)

configureSearchController(placeholder: "Search in \"Artists\"", scopeButtonTitles: ["All", "Cached"], showSearchBarAtEnter: false)
tableView.register(nibName: GenericTableCell.typeName)
tableView.rowHeight = GenericTableCell.rowHeight

filterButton = UIBarButtonItem(image: UIImage.filter, style: .plain, target: self, action: #selector(filterButtonPressed))
navigationItem.rightBarButtonItem = filterButton
sortButton = UIBarButtonItem(image: UIImage.sort, style: .plain, target: self, action: #selector(sortButtonPressed))
navigationItem.rightBarButtonItems = [filterButton, sortButton]
self.refreshControl?.addTarget(self, action: #selector(Self.handleRefresh), for: UIControl.Event.valueChanged)

containableAtIndexPathCallback = { (indexPath) in
Expand All @@ -33,6 +35,17 @@ class ArtistsVC: SingleFetchedResultsTableViewController<ArtistMO> {
}
}

func change(sortType: ElementSortType) {
self.sortType = sortType
appDelegate.persistentStorage.settings.artistsSortSetting = sortType
singleFetchedResultsController?.clearResults()
tableView.reloadData()
fetchedResultsController = ArtistFetchedResultsController(managedObjectContext: appDelegate.persistentStorage.context, sortType: sortType, isGroupedInAlphabeticSections: true)
fetchedResultsController.fetchResultsController.sectionIndexType = sortType == .rating ? .rating : .alphabet
singleFetchedResultsController = fetchedResultsController
tableView.reloadData()
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
updateFilterButton()
Expand All @@ -49,6 +62,28 @@ class ArtistsVC: SingleFetchedResultsTableViewController<ArtistMO> {
return cell
}

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
switch sortType {
case .name:
return 0.0
case .rating:
return CommonScreenOperations.tableSectionHeightLarge
}
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch sortType {
case .name:
return super.tableView(tableView, titleForHeaderInSection: section)
case .rating:
if let sectionNameInitial = super.tableView(tableView, titleForHeaderInSection: section), sectionNameInitial != SectionIndexType.noRatingIndexSymbol {
return "\(sectionNameInitial) Star\(sectionNameInitial != "1" ? "s" : "")"
} else {
return "Not rated"
}
}
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let artist = fetchedResultsController.getWrappedEntity(at: indexPath)
performSegue(withIdentifier: Segues.toArtistDetail.rawValue, sender: artist)
Expand All @@ -75,6 +110,26 @@ class ArtistsVC: SingleFetchedResultsTableViewController<ArtistMO> {
tableView.reloadData()
}

@objc private func sortButtonPressed() {
let alert = UIAlertController(title: "Artists sorting", message: nil, preferredStyle: .actionSheet)
if sortType != .name {
alert.addAction(UIAlertAction(title: "Sort by name", style: .default, handler: { _ in
self.change(sortType: .name)
self.updateSearchResults(for: self.searchController)
}))
}
if sortType != .rating {
alert.addAction(UIAlertAction(title: "Sort by rating", style: .default, handler: { _ in
self.change(sortType: .rating)
self.updateSearchResults(for: self.searchController)
}))
}
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
alert.pruneNegativeWidthConstraintsToAvoidFalseConstraintWarnings()
alert.setOptionsForIPadToDisplayPopupCentricIn(view: self.view)
present(alert, animated: true, completion: nil)
}

@objc private func filterButtonPressed() {
let alert = UIAlertController(title: "Artists filter", message: nil, preferredStyle: .actionSheet)

Expand Down
8 changes: 2 additions & 6 deletions Amperfy/Screens/ViewController/BasicTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,8 @@ extension BasicTableViewController: NSFetchedResultsControllerDelegate {
case .delete:
tableView.deleteRows(at: [indexPath!], with: .left)
case .move:
if indexPath! != newIndexPath! {
tableView.insertRows(at: [newIndexPath!], with: .bottom)
tableView.deleteRows(at: [indexPath!], with: .left)
} else if !isEditLockedDueToActiveSwipe {
tableView.reloadRows(at: [indexPath!], with: .none)
}
tableView.insertRows(at: [newIndexPath!], with: .bottom)
tableView.deleteRows(at: [indexPath!], with: .left)
case .update:
if !isEditLockedDueToActiveSwipe {
tableView.reloadRows(at: [indexPath!], with: .none)
Expand Down
6 changes: 3 additions & 3 deletions Amperfy/Screens/ViewController/SearchVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class SearchVC: BasicTableViewController {

playlistFetchedResultsController = PlaylistFetchedResultsController(managedObjectContext: appDelegate.persistentStorage.context, sortType: .name, isGroupedInAlphabeticSections: false)
playlistFetchedResultsController.delegate = self
artistFetchedResultsController = ArtistFetchedResultsController(managedObjectContext: appDelegate.persistentStorage.context, isGroupedInAlphabeticSections: false)
artistFetchedResultsController = ArtistFetchedResultsController(managedObjectContext: appDelegate.persistentStorage.context, sortType: .name, isGroupedInAlphabeticSections: false)
artistFetchedResultsController.delegate = self
albumFetchedResultsController = AlbumFetchedResultsController(managedObjectContext: appDelegate.persistentStorage.context, isGroupedInAlphabeticSections: false)
albumFetchedResultsController = AlbumFetchedResultsController(managedObjectContext: appDelegate.persistentStorage.context, sortType: .name, isGroupedInAlphabeticSections: false)
albumFetchedResultsController.delegate = self
songFetchedResultsController = SongsFetchedResultsController(managedObjectContext: appDelegate.persistentStorage.context, isGroupedInAlphabeticSections: false)
songFetchedResultsController = SongsFetchedResultsController(managedObjectContext: appDelegate.persistentStorage.context, sortType: .name, isGroupedInAlphabeticSections: false)
songFetchedResultsController.delegate = self

configureSearchController(placeholder: "Playlists, Songs and more", scopeButtonTitles: ["All", "Cached"])
Expand Down
63 changes: 59 additions & 4 deletions Amperfy/Screens/ViewController/SongsVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ class SongsVC: SingleFetchedResultsTableViewController<SongMO> {
private var fetchedResultsController: SongsFetchedResultsController!
private var optionsButton: UIBarButtonItem!
private var filterButton: UIBarButtonItem!
private var sortButton: UIBarButtonItem!
private var displayFilter: DisplayCategoryFilter = .all
private var sortType: ElementSortType = .name

override func viewDidLoad() {
super.viewDidLoad()
appDelegate.userStatistics.visited(.songs)

fetchedResultsController = SongsFetchedResultsController(managedObjectContext: appDelegate.persistentStorage.context, isGroupedInAlphabeticSections: true)
singleFetchedResultsController = fetchedResultsController
change(sortType: appDelegate.persistentStorage.settings.songsSortSetting)

configureSearchController(placeholder: "Search in \"Songs\"", scopeButtonTitles: ["All", "Cached"], showSearchBarAtEnter: false)
tableView.register(nibName: SongTableCell.typeName)
tableView.rowHeight = SongTableCell.rowHeight

optionsButton = UIBarButtonItem(image: UIImage.ellipsis, style: .plain, target: self, action: #selector(optionsPressed))
filterButton = UIBarButtonItem(image: UIImage.filter, style: .plain, target: self, action: #selector(filterButtonPressed))
sortButton = UIBarButtonItem(image: UIImage.sort, style: .plain, target: self, action: #selector(sortButtonPressed))
self.refreshControl?.addTarget(self, action: #selector(Self.handleRefresh), for: UIControl.Event.valueChanged)

containableAtIndexPathCallback = { (indexPath) in
Expand All @@ -33,13 +35,24 @@ class SongsVC: SingleFetchedResultsTableViewController<SongMO> {
}
}

func change(sortType: ElementSortType) {
self.sortType = sortType
appDelegate.persistentStorage.settings.songsSortSetting = sortType
singleFetchedResultsController?.clearResults()
tableView.reloadData()
fetchedResultsController = SongsFetchedResultsController(managedObjectContext: appDelegate.persistentStorage.context, sortType: sortType, isGroupedInAlphabeticSections: true)
fetchedResultsController.fetchResultsController.sectionIndexType = sortType == .rating ? .rating : .alphabet
singleFetchedResultsController = fetchedResultsController
tableView.reloadData()
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
updateFilterButton()
if appDelegate.persistentStorage.settings.isOnlineMode {
navigationItem.rightBarButtonItems = [optionsButton, filterButton]
navigationItem.rightBarButtonItems = [optionsButton, filterButton, sortButton]
} else {
navigationItem.rightBarButtonItems = [filterButton]
navigationItem.rightBarButtonItems = [filterButton, sortButton]
}
}

Expand All @@ -54,6 +67,28 @@ class SongsVC: SingleFetchedResultsTableViewController<SongMO> {
return cell
}

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
switch sortType {
case .name:
return 0.0
case .rating:
return CommonScreenOperations.tableSectionHeightLarge
}
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch sortType {
case .name:
return super.tableView(tableView, titleForHeaderInSection: section)
case .rating:
if let sectionNameInitial = super.tableView(tableView, titleForHeaderInSection: section), sectionNameInitial != SectionIndexType.noRatingIndexSymbol {
return "\(sectionNameInitial) Star\(sectionNameInitial != "1" ? "s" : "")"
} else {
return "Not rated"
}
}
}

func convertIndexPathToPlayContext(songIndexPath: IndexPath) -> PlayContext? {
let song = fetchedResultsController.getWrappedEntity(at: songIndexPath)
return PlayContext(containable: song)
Expand Down Expand Up @@ -83,6 +118,26 @@ class SongsVC: SingleFetchedResultsTableViewController<SongMO> {
tableView.reloadData()
}

@objc private func sortButtonPressed() {
let alert = UIAlertController(title: "Songs sorting", message: nil, preferredStyle: .actionSheet)
if sortType != .name {
alert.addAction(UIAlertAction(title: "Sort by name", style: .default, handler: { _ in
self.change(sortType: .name)
self.updateSearchResults(for: self.searchController)
}))
}
if sortType != .rating {
alert.addAction(UIAlertAction(title: "Sort by rating", style: .default, handler: { _ in
self.change(sortType: .rating)
self.updateSearchResults(for: self.searchController)
}))
}
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
alert.pruneNegativeWidthConstraintsToAvoidFalseConstraintWarnings()
alert.setOptionsForIPadToDisplayPopupCentricIn(view: self.view)
present(alert, animated: true, completion: nil)
}

@objc private func filterButtonPressed() {
let alert = UIAlertController(title: "Songs filter", message: nil, preferredStyle: .actionSheet)

Expand Down
Loading

0 comments on commit 462f75a

Please sign in to comment.