Skip to content

Commit

Permalink
added grid mode to library view iphone
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Halpin committed Oct 10, 2023
1 parent 8b9ae48 commit 9c23796
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions HalpoPlayer/Storage/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import Foundation
import UIKit

class Database: ObservableObject {
static let shared = Database()
Expand All @@ -17,6 +18,7 @@ class Database: ObservableObject {
@Published var searchScope: SearchScope
@Published var searchText: String
@Published var libraryViewType: LibraryViewType = .albums
@Published var libraryGridMode = UIDevice.current.userInterfaceIdiom == .pad
var albumPage = 0
@Published var musicCache: [String: CachedSong] {
didSet {
Expand Down
23 changes: 22 additions & 1 deletion HalpoPlayer/Views/LibraryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct LibraryView: View {
@StateObject var viewModel = LibraryViewModel()
@EnvironmentObject var coordinator: Coordinator
@ObservedObject var accountHolder = AccountHolder.shared

var body: some View {
if accountHolder.account != nil {
switch viewModel.viewType {
Expand Down Expand Up @@ -45,14 +46,15 @@ struct AlbumListView: View {
@StateObject var viewModel: LibraryViewModel
@EnvironmentObject var coordinator: Coordinator
@ObservedObject var accountHolder = AccountHolder.shared
@ObservedObject var database = Database.shared
func gridItems(width: Double) -> ([GridItem], Double) {
let count = Int((width / 200.0).rounded())
let item = GridItem(.flexible(), spacing: 8, alignment: .top)
let itemWidth: Double = (width-(8*(Double(count)+1)))/Double(count)
return (Array(repeating: item, count: count), itemWidth)
}
var body: some View {
if UIDevice.current.userInterfaceIdiom == .pad {
if database.libraryGridMode {
GeometryReader { geometry in
ScrollView {
let (gridItems, width) = gridItems(width: geometry.size.width)
Expand Down Expand Up @@ -108,6 +110,15 @@ struct AlbumListView: View {
Image(systemName: "shuffle").imageScale(.large)
}
}
ToolbarItem(placement: .navigationBarTrailing) {
Button {
withAnimation {
database.libraryGridMode.toggle()
}
} label: {
Image(systemName: database.libraryGridMode ? "line.3.horizontal" : "square.grid.2x2").imageScale(.large)
}
}
}
}
} else {
Expand Down Expand Up @@ -185,6 +196,15 @@ struct AlbumListView: View {
}
}
}
ToolbarItem(placement: .navigationBarTrailing) {
Button {
withAnimation {
database.libraryGridMode.toggle()
}
} label: {
Image(systemName: database.libraryGridMode ? "line.3.horizontal" : "square.grid.2x2").imageScale(.large)
}
}
// ToolbarItem(placement: .navigationBarTrailing) {
// Button {
// viewModel.selectedAlbums = []
Expand Down Expand Up @@ -257,6 +277,7 @@ struct ArtistListView: View {
Image(systemName: "shuffle").imageScale(.large)
}
}

}
}
}

0 comments on commit 9c23796

Please sign in to comment.