From ed239cd4c13bdff4f814d03b34b07c3cd496d42e Mon Sep 17 00:00:00 2001 From: YuJeong Date: Tue, 20 Aug 2024 07:07:22 +0900 Subject: [PATCH] =?UTF-8?q?[feat]=20=EA=B3=A1=20=EA=B2=80=EC=83=89=20API?= =?UTF-8?q?=20=EC=99=84=EB=A3=8C=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PLUV/Move/MovePlaylistViewController.swift | 86 ++++++++++++++++++++- PLUV/Move/MovePlaylistViewModel.swift | 3 + PLUV/Select/SelectMusicViewController.swift | 11 ++- 3 files changed, 97 insertions(+), 3 deletions(-) diff --git a/PLUV/Move/MovePlaylistViewController.swift b/PLUV/Move/MovePlaylistViewController.swift index 1dacea5..bc9d5e5 100644 --- a/PLUV/Move/MovePlaylistViewController.swift +++ b/PLUV/Move/MovePlaylistViewController.swift @@ -6,6 +6,7 @@ // import UIKit +import MusicKit class MovePlaylistViewController: UIViewController { @@ -67,9 +68,10 @@ class MovePlaylistViewController: UIViewController { private let stopView = ActionBottomView(actionName: "작업 중단하기") - init(playlistItem: Playlist, source: MusicPlatform, destination: MusicPlatform) { + init(playlistItem: Playlist, musicItems: [Music], source: MusicPlatform, destination: MusicPlatform) { super.init(nibName: nil, bundle: nil) self.viewModel.playlistItem = playlistItem + self.viewModel.musicItems = musicItems self.sourcePlatform = source self.destinationPlatform = destination } @@ -83,6 +85,7 @@ class MovePlaylistViewController: UIViewController { setUI() setPlaylistData() + searchAPI() circleLoadingIndicator.isAnimating = true @@ -225,4 +228,85 @@ class MovePlaylistViewController: UIViewController { } } } + + private func searchAPI() { + if sourcePlatform == .AppleMusic { + Task { + await self.searchAppleToSpotifyAPI(musics: self.viewModel.musicItems) + } + } else if sourcePlatform == .Spotify { + Task { + await self.searchSpotifyToAppleAPI(musics: self.viewModel.musicItems) + } + } + } + + /// 애플에 있는 것 스포티파이에서 검색 + private func searchAppleToSpotifyAPI(musics: [Music]) async { + do { + let jsonData = try JSONEncoder().encode(musics) + let jsonString = String(data: jsonData, encoding: .utf8) ?? "" + let musicParams = jsonString.replacingOccurrences(of: "\\", with: "").replacingOccurrences(of: "artistNames", with: "artistName") + + if let parameterJsonData = musicParams.data(using: .utf8) { + do { + if let parameterJsonArray = try JSONSerialization.jsonObject(with: parameterJsonData, options: []) as? [[String: Any]] { + + let url = EndPoint.musicSpotifySearch.path + let params = ["destinationAccessToken" : TokenManager.shared.spotifyAccessToken, + "musics" : parameterJsonArray] as [String : Any] + + APIService().post(of: APIResponse<[Search]>.self, url: url, parameters: params) { response in + switch response.code { + case 200: + print(response.data, "애플에 있는 것 스포티파이에서 검색") + default: + AlertController(message: response.msg).show() + } + } + } + } catch { + print("JSON 변환 실패: \(error.localizedDescription)") + } + } + } catch { + print(error) + } + } + + /// 스포티파이에 있는 것 애플에서 검색 + private func searchSpotifyToAppleAPI(musics: [Music]) async { + do { + let developerToken = try await DefaultMusicTokenProvider.init().developerToken(options: .ignoreCache) + let userToken = try await MusicUserTokenProvider.init().userToken(for: developerToken, options: .ignoreCache) + + let jsonData = try JSONEncoder().encode(musics) + let jsonString = String(data: jsonData, encoding: .utf8) ?? "" + let musicParams = jsonString.replacingOccurrences(of: "\\", with: "").replacingOccurrences(of: "artistNames", with: "artistName") + + if let parameterJsonData = musicParams.data(using: .utf8) { + do { + if let parameterJsonArray = try JSONSerialization.jsonObject(with: parameterJsonData, options: []) as? [[String: Any]] { + + let url = EndPoint.musicAppleSearch.path + let params = ["destinationAccessToken" : userToken, + "musics" : parameterJsonArray] as [String : Any] + + APIService().post(of: APIResponse<[Search]>.self, url: url, parameters: params) { response in + switch response.code { + case 200: + print(response.data, "스포티파이에 있는 것 애플에서 검색") + default: + AlertController(message: response.msg).show() + } + } + } + } catch { + print("JSON 변환 실패: \(error.localizedDescription)") + } + } + } catch { + print(error) + } + } } diff --git a/PLUV/Move/MovePlaylistViewModel.swift b/PLUV/Move/MovePlaylistViewModel.swift index adaa8ce..aa0aa66 100644 --- a/PLUV/Move/MovePlaylistViewModel.swift +++ b/PLUV/Move/MovePlaylistViewModel.swift @@ -6,7 +6,10 @@ // import Foundation +import RxSwift +import RxCocoa class MovePlaylistViewModel { var playlistItem: Playlist = Playlist(id: "", thumbnailURL: "", songCount: nil, name: "", source: .apple) + var musicItems: [Music] = [] } diff --git a/PLUV/Select/SelectMusicViewController.swift b/PLUV/Select/SelectMusicViewController.swift index 1d381e6..05a0364 100644 --- a/PLUV/Select/SelectMusicViewController.swift +++ b/PLUV/Select/SelectMusicViewController.swift @@ -241,8 +241,15 @@ class SelectMusicViewController: UIViewController { } @objc private func clickTransferButton() { - let movePlaylistVC = MovePlaylistViewController(playlistItem: self.viewModel.playlistItem, source: sourcePlatform, destination: destinationPlatform) - self.navigationController?.pushViewController(movePlaylistVC, animated: true) + self.viewModel.musicItem + .map { musicArray in + let movePlaylistVC = MovePlaylistViewController(playlistItem: self.viewModel.playlistItem, musicItems: musicArray, source: self.sourcePlatform, destination: self.destinationPlatform) + self.navigationController?.pushViewController(movePlaylistVC, animated: true) + } + .subscribe { musicArray in + print(musicArray) + } + .disposed(by: disposeBag) } private func setPlaylistData() {