Skip to content

Commit

Permalink
[feat] 저장한 플레이리스트, 피드에서 플레이리스트 옮기기 (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaewift committed Nov 23, 2024
1 parent 887db5a commit a06924f
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 53 deletions.
6 changes: 6 additions & 0 deletions PLUV/Component/SaveMoveView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ final class SaveMoveView: UIView {
make.trailing.equalToSuperview().inset(24)
make.height.equalTo(58)
}
trasferButton.addTarget(self, action: #selector(transferButtonTapped), for: .touchUpInside)

shadow()
}
Expand All @@ -87,6 +88,11 @@ final class SaveMoveView: UIView {
isOriginalColor.toggle()
}

@objc func transferButtonTapped() {
feedDelegate?.transferFeed()
saveDelegate?.transferFeedSave()
}

func updateSaveButtonImage(isSaved: Bool) {
let imageName = isSaved ? "savebutton_icon" : "savebutton_icon2"
saveButton.setImage(UIImage(named: imageName), for: .normal)
Expand Down
9 changes: 8 additions & 1 deletion PLUV/Feed/FeedDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import RxCocoa
protocol SaveMoveViewFeedDelegate: AnyObject {
func setFeedSaveAPI()
func deleteFeedSaveAPI()
func transferFeed()
}

class FeedDetailViewController: UIViewController, SaveMoveViewFeedDelegate {
Expand Down Expand Up @@ -293,11 +294,17 @@ class FeedDetailViewController: UIViewController, SaveMoveViewFeedDelegate {
})
.disposed(by: disposeBag)
}

func transferFeed() {
let transferDestinationVC = TransferDestinationViewController()
transferDestinationVC.fromPlatform = LoadPluv.FromSave
transferDestinationVC.saveViewModel.saveItem = viewModel.selectFeedItem
self.navigationController?.pushViewController(transferDestinationVC, animated: true)
}
}


extension FeedDetailViewController: UITableViewDelegate {

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 66
}
Expand Down
2 changes: 1 addition & 1 deletion PLUV/Move/MoveMeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ import RxSwift
import RxCocoa

class MoveMeViewModel {
var meItem: Me = Me(id: 0, transferredAt: "", transferredSongCount: nil, title: "", imageURL: "")
var meItem: Me?
var musicItems: [Music] = []
}
12 changes: 6 additions & 6 deletions PLUV/Move/MovePlaylistViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,10 @@ class MovePlaylistViewController: UIViewController {
}

private func setPlaylistData() {
let thumbnailURL = URL(string: self.viewModel.playlistItem.thumbnailURL)
let thumbnailURL = URL(string: self.viewModel.playlistItem?.thumbnailURL ?? "")
sourceImageView.kf.setImage(with: thumbnailURL)
destinationImageView.image = UIImage(named: destinationPlatform.iconSelect)
playlistTitleLabel.text = self.viewModel.playlistItem.name
playlistTitleLabel.text = self.viewModel.playlistItem?.name
platformLabel.text = sourcePlatform!.name + " > " + destinationPlatform.name
}

Expand Down Expand Up @@ -366,12 +366,12 @@ class MovePlaylistViewController: UIViewController {

let url = EndPoint.musicSpotifyAdd.path
let params = [
"playListName": self.viewModel.playlistItem.name,
"playListName": self.viewModel.playlistItem?.name ?? "",
"destinationAccessToken": TokenManager.shared.spotifyAccessToken,
"musicIds": musicIdsArr,
"transferFailMusics": [
],
"thumbNailUrl": self.viewModel.playlistItem.thumbnailURL,
"thumbNailUrl": self.viewModel.playlistItem?.thumbnailURL ?? "",
"source": "apple"
] as [String : Any]

Expand Down Expand Up @@ -399,12 +399,12 @@ class MovePlaylistViewController: UIViewController {

let url = EndPoint.musicAppleAdd.path
let params = [
"playListName": self.viewModel.playlistItem.name,
"playListName": self.viewModel.playlistItem?.name ?? "",
"destinationAccessToken": musicUserToken,
"musicIds": musicIdsArr,
"transferFailMusics": [
],
"thumbNailUrl": self.viewModel.playlistItem.thumbnailURL,
"thumbNailUrl": self.viewModel.playlistItem?.thumbnailURL ?? "",
"source": "spotify"
] as [String : Any]

Expand Down
2 changes: 1 addition & 1 deletion PLUV/Move/MovePlaylistViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ import RxSwift
import RxCocoa

class MovePlaylistViewModel {
var playlistItem: Playlist = Playlist(id: "", thumbnailURL: "", songCount: nil, name: "", source: .apple)
var playlistItem: Playlist?
var musicItems: [Music] = []
}
2 changes: 1 addition & 1 deletion PLUV/Move/MoveSaveViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ import RxSwift
import RxCocoa

class MoveSaveViewModel {
var saveItem: Feed = Feed(id: 0, title: "", thumbNailURL: "", artistNames: "", creatorName: "", transferredAt: "", totalSongCount: nil)
var saveItem: Feed?
var musicItems: [Music] = []
}
16 changes: 13 additions & 3 deletions PLUV/Platform/TransferCheckViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class TransferCheckViewController: UIViewController {

var sourcePlatform: PlatformRepresentable?
var destinationPlatform: PlatformRepresentable?

var saveViewModel = SelectSaveViewModel()

private let checkTitleView = UIView()
private let backButton = UIButton().then {
Expand Down Expand Up @@ -214,9 +216,17 @@ class TransferCheckViewController: UIViewController {
} else if let musicPlatform = sourcePlatform as? MusicPlatform, musicPlatform == .Spotify {
connectSpotifySession()
}

let selectPlaylistVC = SelectPlaylistViewController(source: sourcePlatform ?? MusicPlatform.AppleMusic, destination: destinationPlatform as! MusicPlatform)
self.navigationController?.pushViewController(selectPlaylistVC, animated: true)

if saveViewModel.saveItem != nil {
let selectMusicVC = SelectMusicViewController()
selectMusicVC.saveViewModel.saveItem = saveViewModel.saveItem
selectMusicVC.sourcePlatform = sourcePlatform
selectMusicVC.destinationPlatform = destinationPlatform as! MusicPlatform
self.navigationController?.pushViewController(selectMusicVC, animated: true)
} else {
let selectPlaylistVC = SelectPlaylistViewController(source: sourcePlatform ?? MusicPlatform.AppleMusic, destination: destinationPlatform as! MusicPlatform)
self.navigationController?.pushViewController(selectPlaylistVC, animated: true)
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions PLUV/Platform/TransferDestinationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class TransferDestinationViewController: UIViewController {
updatePlatformView()
}
}

var saveViewModel = SelectSaveViewModel()

private var destinationList = Observable.just(MusicPlatform.allCases)

private let destinationTitleView = UIView()
Expand Down Expand Up @@ -165,6 +168,9 @@ extension TransferDestinationViewController {
transferCheckVC.sourcePlatform = updatedFromPlatform
}
transferCheckVC.destinationPlatform = platform
if self?.saveViewModel.saveItem != nil {
transferCheckVC.saveViewModel.saveItem = self?.saveViewModel.saveItem
}
self?.navigationController?.pushViewController(transferCheckVC, animated: true)
})
.disposed(by: disposeBag)
Expand Down
8 changes: 8 additions & 0 deletions PLUV/Save/SaveDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Alamofire

protocol SaveMoveViewSaveDelegate: AnyObject {
func deleteFeedSaveAPI()
func transferFeedSave()
}

class SaveDetailViewController: UIViewController, SaveMoveViewSaveDelegate {
Expand Down Expand Up @@ -235,6 +236,13 @@ class SaveDetailViewController: UIViewController, SaveMoveViewSaveDelegate {
}
}
}

func transferFeedSave() {
let transferDestinationVC = TransferDestinationViewController()
transferDestinationVC.fromPlatform = LoadPluv.FromSave
transferDestinationVC.saveViewModel.saveItem = viewModel.selectSaveItem!
self.navigationController?.pushViewController(transferDestinationVC, animated: true)
}
}

extension SaveDetailViewController: UITableViewDelegate {
Expand Down
2 changes: 1 addition & 1 deletion PLUV/Select/SelectMePlaylistViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ class SelectMePlaylistViewModel {
// 선택된 셀의 인덱스 패스를 저장하는 BehaviorRelay
let selectedIndexPath = BehaviorRelay<IndexPath?>(value: nil)
var mePlaylistItems: Observable<[Me]> = Observable.just([])
var mePlaylistItem: Me = Me(id: 0, transferredAt: "", transferredSongCount: nil, title: "", imageURL: "")
var mePlaylistItem: Me?
}
2 changes: 1 addition & 1 deletion PLUV/Select/SelectMeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import RxSwift
import RxCocoa

class SelectMeViewModel {
var meItem: Me = Me(id: 0, transferredAt: "", transferredSongCount: nil, title: "", imageURL: "")
var meItem: Me?
var musicItem = BehaviorRelay<[Music]>(value: [])
let selectedMusic = BehaviorRelay<[Music]>(value: [])
let disposeBag = DisposeBag()
Expand Down
82 changes: 48 additions & 34 deletions PLUV/Select/SelectMusicViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -342,61 +342,75 @@ class SelectMusicViewController: UIViewController {
}
}

@objc private func clickTransferButton() {
if destinationPlatform == .Spotify {
self.viewModel.selectedMusic
@objc private func clickTransferButton() {
guard let sourcePlatform = sourcePlatform else { return }

let selectedMusicObservable: BehaviorRelay<[Music]>?
let transferAPI: ([Music]) async -> Void

switch (sourcePlatform, destinationPlatform) {
case (let platform as MusicPlatform, .Spotify) where platform == .AppleMusic:
selectedMusicObservable = viewModel.selectedMusic
transferAPI = searchToSpotifyAPI
case (let platform as LoadPluv, .Spotify) where platform == .FromRecent:
selectedMusicObservable = meViewModel.selectedMusic
transferAPI = searchToSpotifyAPI
case (let platform as LoadPluv, .Spotify) where platform == .FromSave:
selectedMusicObservable = saveViewModel.selectedMusic
transferAPI = searchToSpotifyAPI
case (let platform as MusicPlatform, .AppleMusic) where platform == .Spotify:
selectedMusicObservable = viewModel.selectedMusic
transferAPI = searchToAppleAPI
case (let platform as LoadPluv, .AppleMusic) where platform == .FromRecent:
selectedMusicObservable = meViewModel.selectedMusic
transferAPI = searchToAppleAPI
case (let platform as LoadPluv, .AppleMusic) where platform == .FromSave:
selectedMusicObservable = saveViewModel.selectedMusic
transferAPI = searchToAppleAPI
default:
return
}

selectedMusicObservable?
.map { musicArray in
Task {
await self.searchAppleToSpotifyAPI(musics: musicArray)
self.goNextStep()
}
Task {
await transferAPI(musicArray)
self.goNextStep()
}
}
.subscribe { musicArray in
print(musicArray)
print(musicArray)
}
.disposed(by: disposeBag)
} else {
self.viewModel.selectedMusic
.map { musicArray in
Task {
await self.searchSpotifyToAppleAPI(musics: musicArray)
self.goNextStep()
}
}
.subscribe { musicArray in
print(musicArray)
}
.disposed(by: disposeBag)
}
}
}

private func setPlaylistData() {
if let musicPlatform = sourcePlatform as? MusicPlatform, musicPlatform == .AppleMusic || musicPlatform == .Spotify {
let thumbnailURL = URL(string: self.viewModel.playlistItem.thumbnailURL)
let thumbnailURL = URL(string: self.viewModel.playlistItem?.thumbnailURL ?? "")
playlistThumnailImageView.kf.setImage(with: thumbnailURL)
sourceToDestinationLabel.text = sourcePlatform!.name + " > " + destinationPlatform.name
sourcePlatformLabel.text = sourcePlatform!.name
playlistNameLabel.text = self.viewModel.playlistItem.name
playlistNameLabel.text = self.viewModel.playlistItem?.name
self.viewModel.musicItemCount { count in
self.playlistSongCountLabel.text = "\(count)"
self.songCountLabel.text = "\(count)"
}
} else if let musicPlatform = sourcePlatform as? LoadPluv, musicPlatform == .FromRecent {
let thumbnailURL = URL(string: self.meViewModel.meItem.imageURL)
let thumbnailURL = URL(string: self.meViewModel.meItem?.imageURL ?? "")
playlistThumnailImageView.kf.setImage(with: thumbnailURL)
sourceToDestinationLabel.text = sourcePlatform!.name + " > " + destinationPlatform.name
sourcePlatformLabel.text = sourcePlatform!.name
playlistNameLabel.text = self.meViewModel.meItem.title
playlistNameLabel.text = self.meViewModel.meItem?.title
self.meViewModel.musicItemCount { count in
self.playlistSongCountLabel.text = "\(count)"
self.songCountLabel.text = "\(count)"
}
} else {
let thumbnailURL = URL(string: self.saveViewModel.saveItem.thumbNailURL)
let thumbnailURL = URL(string: self.saveViewModel.saveItem?.thumbNailURL ?? "")
playlistThumnailImageView.kf.setImage(with: thumbnailURL)
sourceToDestinationLabel.text = sourcePlatform!.name + " > " + destinationPlatform.name
sourcePlatformLabel.text = sourcePlatform!.name
playlistNameLabel.text = self.saveViewModel.saveItem.title
playlistNameLabel.text = self.saveViewModel.saveItem?.title
self.saveViewModel.musicItemCount { count in
self.playlistSongCountLabel.text = "\(count)"
self.songCountLabel.text = "\(count)"
Expand Down Expand Up @@ -549,7 +563,7 @@ class SelectMusicViewController: UIViewController {
let developerToken = try await DefaultMusicTokenProvider.init().developerToken(options: .ignoreCache)
let userToken = try await MusicUserTokenProvider.init().userToken(for: developerToken, options: .ignoreCache)

let url = EndPoint.playlistAppleMusicRead(self.viewModel.playlistItem.id).path
let url = EndPoint.playlistAppleMusicRead(self.viewModel.playlistItem?.id ?? "").path
let params = ["musicUserToken" : userToken]

APIService().post(of: APIResponse<[Music]>.self, url: url, parameters: params) { response in
Expand All @@ -570,7 +584,7 @@ class SelectMusicViewController: UIViewController {
}

private func setSpotifyMusicListAPI() {
let url = EndPoint.playlistMusicSpotifyRead(self.viewModel.playlistItem.id).path
let url = EndPoint.playlistMusicSpotifyRead(self.viewModel.playlistItem?.id ?? "").path
let params = ["accessToken" : TokenManager.shared.spotifyAccessToken]

APIService().post(of: APIResponse<[Music]>.self, url: url, parameters: params) { response in
Expand All @@ -588,7 +602,7 @@ class SelectMusicViewController: UIViewController {
}

private func setMeMusicListAPI() {
let recentId = meViewModel.meItem.id
let recentId = meViewModel.meItem?.id ?? 0
let loginToken = UserDefaults.standard.string(forKey: APIService.shared.loginAccessTokenKey)!
let url = EndPoint.historySuccess("\(recentId)").path

Expand All @@ -607,7 +621,7 @@ class SelectMusicViewController: UIViewController {
}

private func setSaveMusicListAPI() {
let saveId = saveViewModel.saveItem.id
let saveId = saveViewModel.saveItem?.id ?? 0
let url = EndPoint.feedIdMusic("\(saveId)").path

APIService().get(of: APIResponse<[Music]>.self, url: url) { response in
Expand Down Expand Up @@ -646,7 +660,7 @@ class SelectMusicViewController: UIViewController {
}
}

private func searchAppleToSpotifyAPI(musics: [Music]) async {
private func searchToSpotifyAPI(musics: [Music]) async {
self.setSearchView()
do {
let jsonData = try JSONEncoder().encode(musics)
Expand Down Expand Up @@ -693,7 +707,7 @@ class SelectMusicViewController: UIViewController {
}
}

private func searchSpotifyToAppleAPI(musics: [Music]) async {
private func searchToAppleAPI(musics: [Music]) async {
self.setSearchView()
do {
let developerToken = try await DefaultMusicTokenProvider.init().developerToken(options: .ignoreCache)
Expand Down
2 changes: 1 addition & 1 deletion PLUV/Select/SelectMusicViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import RxSwift
import RxCocoa

class SelectMusicViewModel {
var playlistItem: Playlist = Playlist(id: "", thumbnailURL: "", songCount: nil, name: "", source: .apple)
var playlistItem: Playlist?
var musicItem = BehaviorRelay<[Music]>(value: [])
let selectedMusic = BehaviorRelay<[Music]>(value: [])
let disposeBag = DisposeBag()
Expand Down
2 changes: 1 addition & 1 deletion PLUV/Select/SelectPlaylistViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ class SelectPlaylistViewModel {
// 선택된 셀의 인덱스 패스를 저장하는 BehaviorRelay
let selectedIndexPath = BehaviorRelay<IndexPath?>(value: nil)
var playlistItems: Observable<[Playlist]> = Observable.just([])
var playlistItem: Playlist = Playlist(id: "", thumbnailURL: "", songCount: nil, name: "", source: .apple)
var playlistItem: Playlist?
}
2 changes: 1 addition & 1 deletion PLUV/Select/SelectSavePlaylistViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ class SelectSavePlaylistViewModel {
// 선택된 셀의 인덱스 패스를 저장하는 BehaviorRelay
let selectedIndexPath = BehaviorRelay<IndexPath?>(value: nil)
var savePlaylistItems: Observable<[Feed]> = Observable.just([])
var savePlaylistItem: Feed = Feed(id: 0, title: "", thumbNailURL: "", artistNames: "", creatorName: "", transferredAt: "", totalSongCount: nil)
var savePlaylistItem: Feed?
}
2 changes: 1 addition & 1 deletion PLUV/Select/SelectSaveViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import RxSwift
import RxCocoa

class SelectSaveViewModel {
var saveItem: Feed = Feed(id: 0, title: "", thumbNailURL: "", artistNames: "", creatorName: "", transferredAt: "", totalSongCount: nil)
var saveItem: Feed?
var musicItem = BehaviorRelay<[Music]>(value: [])
let selectedMusic = BehaviorRelay<[Music]>(value: [])
let disposeBag = DisposeBag()
Expand Down

0 comments on commit a06924f

Please sign in to comment.