Skip to content

Commit

Permalink
Merge pull request #8 from halpz/nowPlaying
Browse files Browse the repository at this point in the history
now playing new
  • Loading branch information
halpz authored Oct 16, 2023
2 parents 9c23796 + 0892194 commit ac12e1b
Show file tree
Hide file tree
Showing 16 changed files with 249 additions and 318 deletions.
4 changes: 4 additions & 0 deletions HalpoPlayer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
DA13D7FF2A6D94DD00BADCB6 /* BatteryManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA13D7FE2A6D94DD00BADCB6 /* BatteryManager.swift */; };
DA1FC7DA2AD94F8D00B2DCBA /* VolumeSlider.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA1FC7D92AD94F8D00B2DCBA /* VolumeSlider.swift */; };
DA26224A2A7940FE006A2CDD /* CreatePlaylistResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA2622492A7940FE006A2CDD /* CreatePlaylistResponse.swift */; };
DA3DFF642AB8595900D40D7A /* ViewFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA3DFF632AB8595900D40D7A /* ViewFactory.swift */; };
DA3E7C352A69213900DD5180 /* SideMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA3E7C342A69213900DD5180 /* SideMenu.swift */; };
Expand Down Expand Up @@ -112,6 +113,7 @@

/* Begin PBXFileReference section */
DA13D7FE2A6D94DD00BADCB6 /* BatteryManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BatteryManager.swift; sourceTree = "<group>"; };
DA1FC7D92AD94F8D00B2DCBA /* VolumeSlider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VolumeSlider.swift; sourceTree = "<group>"; };
DA2622492A7940FE006A2CDD /* CreatePlaylistResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreatePlaylistResponse.swift; sourceTree = "<group>"; };
DA3DFF632AB8595900D40D7A /* ViewFactory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ViewFactory.swift; path = HalpoPlayer/ViewFactory.swift; sourceTree = SOURCE_ROOT; };
DA3E7C342A69213900DD5180 /* SideMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SideMenu.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -324,6 +326,7 @@
DA3E7C3A2A693C8100DD5180 /* ArtistView.swift */,
DA3E7C3E2A69420100DD5180 /* ArtistCell.swift */,
DA47D8302A6E74B200CBAAE4 /* NowPlayingView.swift */,
DA1FC7D92AD94F8D00B2DCBA /* VolumeSlider.swift */,
);
path = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -523,6 +526,7 @@
DA7F1EA42A652E14006D8934 /* Account.swift in Sources */,
DA3DFF642AB8595900D40D7A /* ViewFactory.swift in Sources */,
DA7F1EA02A652E14006D8934 /* AuthenticationResponse.swift in Sources */,
DA1FC7DA2AD94F8D00B2DCBA /* VolumeSlider.swift in Sources */,
DA7F1EA12A652E14006D8934 /* GetIndexesResponse.swift in Sources */,
DA47D8312A6E74B200CBAAE4 /* NowPlayingView.swift in Sources */,
DA7F1E942A652DF4006D8934 /* Extensions.swift in Sources */,
Expand Down
31 changes: 18 additions & 13 deletions HalpoPlayer/AudioManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class AudioManager: ObservableObject {

func loadSavedState() {
guard !initialLoad else { return }
guard AccountHolder.shared.account != nil else { return }
self.play(songs: self.playerState.songs, index: self.playerState.index, paused: true, currentTime: self.playerState.currentTime)
initialLoad = true
}
Expand Down Expand Up @@ -119,20 +120,24 @@ class AudioManager: ObservableObject {
}

func addSongToQueue(song: Song) {
self.songs?.append(song)
let item: DefaultAudioItem
if let cachedURL = Database.shared.retrieveSong(song: song) {
item = DefaultAudioItem(audioUrl: cachedURL.path(), sourceType: .file)
} else {
let url = SubsonicClient.shared.stream(id: song.id, mp3: true).absoluteString
item = DefaultAudioItem(audioUrl: url, sourceType: .stream)
do {
self.songs?.append(song)
let item: DefaultAudioItem
if let cachedURL = Database.shared.retrieveSong(song: song) {
item = DefaultAudioItem(audioUrl: cachedURL.path(), sourceType: .file)
} else {
let url = try SubsonicClient.shared.stream(id: song.id, mp3: true).absoluteString
item = DefaultAudioItem(audioUrl: url, sourceType: .stream)
}
item.albumTitle = song.album
item.artist = song.artist
item.title = song.title
item.artwork = albumArt
try? self.queue.add(item: item)
self.updatePlaylist()
} catch {
print(error)
}
item.albumTitle = song.album
item.artist = song.artist
item.title = song.title
item.artwork = albumArt
try? self.queue.add(item: item)
self.updatePlaylist()
}

func handleAudioPlayerError(fail: AudioPlayer.FailEventData) {
Expand Down
15 changes: 13 additions & 2 deletions HalpoPlayer/HalpoPlayerApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ struct halpoplayerApp: App {
@ObservedObject var downloadsCoordinator = Coordinator()
@ObservedObject var playlistsCoordinator = Coordinator()
@ObservedObject var searchCoordinator = Coordinator()
@ObservedObject var mediaControlBarMinimized = MediaControlBarMinimized.shared
@State var selectedTab: AppTab = .library
@State var presentTEst = false
let batteryManager = BatteryManager.shared
var body: some Scene {
WindowGroup {
Expand Down Expand Up @@ -51,6 +51,18 @@ struct halpoplayerApp: App {
}
}
MediaControlBar()
.onTapGesture {
self.presentTEst.toggle()
}
.sheet(isPresented: $presentTEst, content: {
NowPlayingView(goToAlbum: { albumId, songId in
if self.coordinatorForTab(tab: self.selectedTab).viewingAlbum != albumId {
self.coordinatorForTab(tab: self.selectedTab).albumTapped(albumId: albumId, scrollToSong: songId)
}
}, goToArtist: { artistId, artistName in
self.coordinatorForTab(tab: self.selectedTab).goToArtist(artistId: artistId, artistName: artistName)
})
})

HStack {
ForEach(AppTab.allCases, id: \.self) { tab in
Expand All @@ -76,7 +88,6 @@ struct halpoplayerApp: App {
.frame(maxWidth: .infinity)
}
.ignoresSafeArea(.keyboard)
.environmentObject(mediaControlBarMinimized)
.environmentObject(coordinatorForTab(tab: selectedTab))
.onAppear {
initApp()
Expand Down
5 changes: 3 additions & 2 deletions HalpoPlayer/Network/SubsonicClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ class SubsonicClient {
let cachedSong = CachedSong(song: song, album: album.subsonicResponse.album, imageUrl: coverArtUrl, path: "")
return (data, response, cachedSong)
}
func stream(id: String, mp3: Bool = false) -> URL {
func stream(id: String, mp3: Bool = false) throws -> URL {
guard let currentAddress = currentAddress else { throw HalpoError.noAccount }
let api = SubsonicAPI.stream(id: id, mp3: mp3)
let url = URL(string: "\(currentAddress!)/rest/\(api.pathComponent)\(userString)")!
let url = URL(string: "\(currentAddress)/rest/\(api.pathComponent)\(userString)")!
return url
}
func coverArt(albumId: String) async throws -> UIImage {
Expand Down
1 change: 1 addition & 0 deletions HalpoPlayer/View Models/ArtistViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class ArtistViewModel: ObservableObject {
let respones = try await SubsonicClient.shared.getAlbum(id: album.id)
songs.append(contentsOf: respones.subsonicResponse.album.song)
}
songs = songs.shuffled()
let finalSongs = songs
DispatchQueue.main.async {
self.player.play(songs:finalSongs, index: 0)
Expand Down
4 changes: 0 additions & 4 deletions HalpoPlayer/View Models/PlaylistViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ class PlaylistViewModel: ObservableObject {
self.player.play(songs: songs, index: 0)
}
}
func cellDidAppear(song: Song) {
guard MediaControlBarMinimized.shared.isCompact == false else { return }
MediaControlBarMinimized.shared.isCompact = true
}
func move(from source: IndexSet, to destination: Int) {
guard !reordering else { return }
reordering = true
Expand Down
4 changes: 0 additions & 4 deletions HalpoPlayer/View Models/PlaylistsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ class PlaylistsViewModel: ObservableObject {
func goToPlaylist(playlist: GetPlaylistsResponse.Playlist, coordinator: Coordinator) {
coordinator.goToPlaylist(playlist: playlist)
}
func cellDidAppear(playlist: GetPlaylistsResponse.Playlist) {
guard MediaControlBarMinimized.shared.isCompact == false else { return }
MediaControlBarMinimized.shared.isCompact = true
}
func addSongsToPlaylist(playlistId: String, coordinator: Coordinator) {
Task {
let songIds = songs.map { $0.id }
Expand Down
9 changes: 0 additions & 9 deletions HalpoPlayer/Views/AlbumDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,6 @@ struct AlbumDetailView: View {
}
.id(song.id)
.listRowSeparator(.hidden)
.onAppear {
self.songAppeared(song: song)
}
}
}
.listStyle(.plain)
Expand Down Expand Up @@ -160,10 +157,4 @@ struct AlbumDetailView: View {
ProgressView()
}
}
func songAppeared(song: Song) {
guard MediaControlBarMinimized.shared.isCompact == false else { return }
withAnimation {
MediaControlBarMinimized.shared.isCompact = true
}
}
}
9 changes: 0 additions & 9 deletions HalpoPlayer/Views/DownloadsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ struct DownloadsView: View {
.tint(.blue)
}
.listRowSeparator(.hidden)
.onAppear {
self.songAppeared(song: file.song)
}
}
case .albums:
ForEach(viewModel.albums) { album in
Expand Down Expand Up @@ -98,12 +95,6 @@ struct DownloadsView: View {
}
}
}
func songAppeared(song: Song) {
guard MediaControlBarMinimized.shared.isCompact == false else { return }
withAnimation {
MediaControlBarMinimized.shared.isCompact = true
}
}
}

enum DownloadsType: String, CaseIterable {
Expand Down
33 changes: 10 additions & 23 deletions HalpoPlayer/Views/LibraryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ 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 All @@ -20,12 +19,15 @@ struct LibraryView: View {
case .albums:
AlbumListView(viewModel: viewModel)
.onAppear {
Task {
do {
try await viewModel.loadContent(force: true)
} catch {
print(error)
if viewModel.albums.isEmpty {
Task {
do {
try await viewModel.loadContent(force: true)
} catch {
print(error)
}
}

}
}
}
Expand Down Expand Up @@ -72,11 +74,6 @@ struct AlbumListView: View {
}
.padding(8)
}
.simultaneousGesture(DragGesture().onChanged({ value in
withAnimation {
MediaControlBarMinimized.shared.isCompact = true
}
}))
.refreshable {
do {
try await viewModel.loadContent(force: true)
Expand Down Expand Up @@ -122,7 +119,7 @@ struct AlbumListView: View {
}
}
} else {
List(viewModel.albums) { album in
List(viewModel.filteredAlbums) { album in
Button {
if viewModel.selectMode {
if viewModel.selectedAlbums.contains(album) {
Expand All @@ -147,11 +144,6 @@ struct AlbumListView: View {
viewModel.albumAppeared(album: album)
}
}
.simultaneousGesture(DragGesture().onChanged({ value in
withAnimation {
MediaControlBarMinimized.shared.isCompact = true
}
}))
.refreshable {
do {
try await viewModel.loadContent(force: true)
Expand Down Expand Up @@ -237,19 +229,14 @@ struct ArtistListView: View {
}
}
}
List(viewModel.artists) { artist in
List(viewModel.filteredArtists) { artist in
Button {
coordinator.goToArtist(artistId: artist.id, artistName: artist.name)
} label: {
ArtistCell(artist: artist)
}
.listRowSeparator(.hidden)
}
.simultaneousGesture(DragGesture().onChanged({ value in
withAnimation {
MediaControlBarMinimized.shared.isCompact = true
}
}))
.listStyle(.plain)
.searchable(text: $viewModel.searchText, prompt: "Search artists")
.scrollDismissesKeyboard(.immediately)
Expand Down
9 changes: 8 additions & 1 deletion HalpoPlayer/Views/LoginView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ struct LoginView: View {

Task {
do {
let success = try await SubsonicClient.shared.testAddressesForPermission(ad1: "\(address):\(port)", ad2: "\(otherAddress):\(port)")
let success = try await SubsonicClient.shared.testAddressesForPermission(ad1: self.combineAddressWithPort(address: address, port: port), ad2: self.combineAddressWithPort(address: otherAddress, port: port))
if success {
let account = Account(username: username, password: password, address: address, otherAddress: otherAddress, port: port)
DispatchQueue.main.async {
Expand All @@ -86,6 +86,13 @@ struct LoginView: View {
}
dismiss()
}
func combineAddressWithPort(address: String, port: String) -> String {
if address.contains(":") {
return address
} else {
return "\(address)\(port)"
}
}
func logout() {
self.address = ""
self.otherAddress = ""
Expand Down
Loading

0 comments on commit ac12e1b

Please sign in to comment.