Skip to content

Commit

Permalink
[feat] 곡 검색 API 완료 (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
BAEKYUJEONG committed Aug 19, 2024
1 parent bfca136 commit ed239cd
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
86 changes: 85 additions & 1 deletion PLUV/Move/MovePlaylistViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import UIKit
import MusicKit

class MovePlaylistViewController: UIViewController {

Expand Down Expand Up @@ -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
}
Expand All @@ -83,6 +85,7 @@ class MovePlaylistViewController: UIViewController {

setUI()
setPlaylistData()
searchAPI()

circleLoadingIndicator.isAnimating = true

Expand Down Expand Up @@ -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)
}
}
}
3 changes: 3 additions & 0 deletions PLUV/Move/MovePlaylistViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
}
11 changes: 9 additions & 2 deletions PLUV/Select/SelectMusicViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit ed239cd

Please sign in to comment.