Skip to content

Commit

Permalink
[feat] HomeVC UI 수정 (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
BAEKYUJEONG committed Aug 20, 2024
1 parent aebde6d commit dc6a4e8
Showing 1 changed file with 79 additions and 126 deletions.
205 changes: 79 additions & 126 deletions PLUV/HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,33 @@ import MusicKit

class HomeViewController: UIViewController {

private var destinationAccessToken = ""
private let backgroundImageView = UIImageView().then {
$0.image = UIImage(named: "backgroundhome_image")
}
private let logoImageView = UIImageView().then {
$0.image = UIImage(named: "homelogo_image")
$0.contentMode = .scaleAspectFit
}
private let transferDirectButton = UIButton().then {
$0.titleLabel?.text = "직접 옮기기"
$0.titleLabel?.font = .systemFont(ofSize: 14)
$0.backgroundColor = .systemGray
$0.setImage(UIImage(named: "homebutton_direct_image"), for: .normal)
$0.contentMode = .scaleAspectFit
}
private let transferScreenshotButton = UIButton().then {
$0.setImage(UIImage(named: "homebutton_screenshot_image"), for: .normal)
$0.contentMode = .scaleAspectFit
}
private let myPlaylistImageView = UIImageView().then {
$0.image = UIImage(named: "myplaylist_image")
}
private let recentListImageView = UIImageView().then {
$0.image = UIImage(named: "recentlist_image")
$0.contentMode = .scaleAspectFill
$0.backgroundColor = .white
}
private let saveListImageView = UIImageView().then {
$0.image = UIImage(named: "savelist_image")
$0.contentMode = .scaleAspectFill
$0.backgroundColor = .white
}

var musicList: [Music] = []
Expand All @@ -26,147 +48,78 @@ class HomeViewController: UIViewController {
super.viewDidLoad()

setUI()
setTestAppleMusic()
Task {
await self.setApplePlaylistMusicAPI()
}
}
}

extension HomeViewController {

private func setUI() {
self.view.backgroundColor = .white
//self.navigationItem.setHidesBackButton(true, animated: false)
self.view.backgroundColor = .gray100
self.navigationItem.setHidesBackButton(true, animated: false)

self.view.addSubview(backgroundImageView)
backgroundImageView.snp.makeConstraints { make in
make.top.leading.trailing.equalToSuperview()
make.height.equalTo(359)
}

self.view.addSubview(logoImageView)
logoImageView.snp.makeConstraints { make in
make.top.equalToSuperview().offset(62)
make.leading.equalToSuperview().offset(16)
make.width.equalTo(66)
make.height.equalTo(24)
}

self.view.addSubview(transferDirectButton)
transferDirectButton.snp.makeConstraints { make in
make.center.equalToSuperview()
make.height.width.equalTo(100)
make.top.equalTo(logoImageView.snp.bottom).offset(15)
make.leading.trailing.equalToSuperview().inset(16)
make.height.equalTo(106)
}

transferDirectButton.addTarget(self, action: #selector(clickTransferDirectButton), for: .touchUpInside)

self.view.addSubview(transferScreenshotButton)
transferScreenshotButton.snp.makeConstraints { make in
make.top.equalTo(transferDirectButton.snp.bottom).offset(12)
make.leading.trailing.equalToSuperview().inset(16)
make.height.equalTo(106)
}
transferScreenshotButton.addTarget(self, action: #selector(clickTransferScreenshotButton), for: .touchUpInside)

self.view.addSubview(myPlaylistImageView)
myPlaylistImageView.snp.makeConstraints { make in
make.top.equalTo(backgroundImageView.snp.bottom)
make.leading.trailing.equalToSuperview()
make.height.equalTo(48)
}

self.view.addSubview(recentListImageView)
recentListImageView.snp.makeConstraints { make in
make.top.equalTo(myPlaylistImageView.snp.bottom)
make.leading.trailing.equalToSuperview()
make.height.equalTo(56)
}

self.view.addSubview(saveListImageView)
saveListImageView.snp.makeConstraints { make in
make.top.equalTo(recentListImageView.snp.bottom).offset(176)
make.leading.trailing.equalToSuperview()
make.height.equalTo(56)
}
}

@objc private func clickTransferDirectButton() {
let transferSourceViewController = TransferSourceViewController()
self.navigationController?.pushViewController(transferSourceViewController, animated: true)
}

private func setTestAppleMusic() {
// MusicKitManager.shared.fetchMusic("알레프")
@objc private func clickTransferScreenshotButton() {
AlertController(message: "추후 공개될 예정입니다!").show()
}

private func setApplePlaylistMusicAPI() async {
do {
let developerToken = try await DefaultMusicTokenProvider.init().developerToken(options: .ignoreCache)
let userToken = try await MusicUserTokenProvider.init().userToken(for: developerToken, options: .ignoreCache)
print(userToken, "apple music user token")

self.destinationAccessToken = userToken

let url = EndPoint.playlistAppleMusicRead("p.YJXV7dEIekpNVAQ").path
let params = ["musicUserToken" : userToken]

APIService().post(of: APIResponse<[Music]>.self, url: url, parameters: params) { response in
switch response.code {
case 200:
self.musicList = response.data
print(params, "setApplePlaylistMusicAPI params 확인")
print(response.data, "song 확인")
// Task {
// await self.searchAppleToSpotifyAPI(musics: response.data)
// }
//self.addAppleToSpotify()
default:
AlertController(message: response.msg).show()
}
}
} catch {
print("error")
}
}

private func searchAppleToSpotifyAPI(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: "{", with: "[").replacingOccurrences(of: "}", with: "]").replacingOccurrences(of: "artistNames", with: "artistName")

print(musicParams, "musicParams")

let url = EndPoint.musicSpotifySearch.path
let params = ["destinationAccessToken" : userToken,
"musics" : [
[
"title": "좋은 날",
"artistName": "아이유",
"isrcCode": "KRA381001057",
"imageUrl": "imageUrl"
],
[
"title": "SPOT!",
"artistName": "지코,제니",
"isrcCode": "",
"imageUrl": "imageUrl"
],
[
"title": "세상에 존재하지 않는 음악",
"artistName": "세상에 존재하지 않는 가수",
"isrcCode": "",
"imageUrl": "imageUrl"
]
]] as [String : Any]

APIService().post(of: APIResponse<Search>.self, url: url, parameters: params) { response in
switch response.code {
case 200:
print(response.data, "searchAppleToSpotifyAPI")
default:
AlertController(message: response.msg).show()
}
}
} catch {
print(error)
}
}

private func addAppleToSpotify() {
let url = EndPoint.musicSpotifyAdd.path
let params = [
"playListName": "추가할 playlistName",
"destinationAccessToken": destinationAccessToken,
"musicIds": [
"124nkd3fh",
"uo890df1"
],
"transferFailMusics": [
[
"title": "조회되지 못한 음악",
"artistName": "조회되지 못한 아티스트",
"imageUrl": "imageUrl"
],
[
"title": "유사하지만 선택하지 않은 음악",
"artistName": "아티스트",
"imageUrl": "imageUrl"
]
],
"thumbNailUrl": "thumbNailUrl",
"source": "spotify"
] as [String : Any]

APIService().post(of: APIResponse<String>.self, url: url, parameters: params) { response in
switch response.code {
case 200:
print(response.data, "addAppleToSpotify")
default:
print("here")
AlertController(message: response.msg).show()
}
}
/// 테스트
private func setTestAppleMusic() {
MusicKitManager.shared.fetchMusic("알레프")
}
}

0 comments on commit dc6a4e8

Please sign in to comment.