Skip to content

Commit

Permalink
#141 feat: ShazamSession을 임시로 뷰 컨트롤러에 연결해 음악 검색 구성
Browse files Browse the repository at this point in the history
  • Loading branch information
radiantchoi committed Dec 1, 2022
1 parent b6fd93c commit 77e5f7e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Segno/Segno.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,7 @@
DEVELOPMENT_TEAM = 4QG3GC35LA;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Segno/Resource/Info.plist;
INFOPLIST_KEY_NSMicrophoneUsageDescription = "음악 검색 기능을 위해 마이크 사용 허가가 필요합니다.";
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
Expand Down Expand Up @@ -812,6 +813,7 @@
DEVELOPMENT_TEAM = 4QG3GC35LA;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Segno/Resource/Info.plist;
INFOPLIST_KEY_NSMicrophoneUsageDescription = "음악 검색 기능을 위해 마이크 사용 허가가 필요합니다.";
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
Expand Down
4 changes: 2 additions & 2 deletions Segno/Segno/Application/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ final class AppCoordinator: Coordinator {

func start() {
// TODO: login이 안되어있으면 LoginCoordinator 실행
startLoginCoordinator()
// startLoginCoordinator()
// TODO: login이 되어있으면 TabBarCoordinator 실행
// startTabBarCoordinator()
startTabBarCoordinator()
}

func startLoginCoordinator() {
Expand Down
18 changes: 6 additions & 12 deletions Segno/Segno/Entity/MusicInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import Foundation
import ShazamKit

struct MusicInfo: Encodable {
let isrc: String
Expand All @@ -15,16 +14,11 @@ struct MusicInfo: Encodable {
let album: String
let imageURL: URL?

init?(mediaItem: SHMatchedMediaItem) {
guard let isrc = mediaItem.isrc,
let title = mediaItem.title,
let artist = mediaItem.artist,
let album = mediaItem.album else { return nil }

self.isrc = isrc
self.title = title
self.artist = artist
self.album = album
self.imageURL = mediaItem.artworkURL
init(shazamSong: ShazamSong) {
self.isrc = shazamSong.isrc
self.title = shazamSong.title
self.artist = shazamSong.artist
self.album = shazamSong.album
self.imageURL = shazamSong.imageURL
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ final class DiaryEditViewController: UIViewController {
}

// let viewModel: DiaryEditViewModel
private let shazamSession = ShazamSession() // 임시로 연동 - 추후 분리 예정
private var disposeBag = DisposeBag()

private lazy var mainScrollView: UIScrollView = {
Expand Down Expand Up @@ -186,6 +187,11 @@ final class DiaryEditViewController: UIViewController {

setupView()
bindImageView()

// 샤잠킷 연동 메서드입니다. 향후 조정 예정입니다.
bindLabel()
bindButtonAction()
bindSearchResult()
}

private func setupView() {
Expand Down Expand Up @@ -280,6 +286,58 @@ final class DiaryEditViewController: UIViewController {
}
}

// 샤잠킷 로직 부분 - 추후 뜯어서 옮길 계획입니다.
extension DiaryEditViewController {
private func bindLabel() {
shazamSession.isSearching
.observe(on: MainScheduler.instance)
.subscribe(onNext: { searchState in
switch searchState {
case true:
self.musicInfoLabel.text = "검색 중입니다..."
case false:
self.musicInfoLabel.text = "지금 이 음악은 뭘까요?"
}
})
.disposed(by: disposeBag)
}

private func bindButtonAction() {
addMusicButton.rx.tap
.withUnretained(self)
.bind { _ in
self.searchTapped()
}
.disposed(by: disposeBag)
}

private func bindSearchResult() {
shazamSession.result
.subscribe(onNext: { result in
switch result {
case .success(let song):
let title = song.title
let artist = song.artist

let musicInfo = MusicInfo(shazamSong: song) // 뷰모델에서 이 작업을 할 때, 향후 사용될 엔티티

DispatchQueue.main.async {
self.musicInfoLabel.text = "\(artist) - \(title)"
}
case .failure(_):
DispatchQueue.main.async {
self.musicInfoLabel.text = "음악을 찾지 못했어요."
}
}
})
.disposed(by: disposeBag)
}

private func searchTapped() {
shazamSession.toggleSearch()
}
}

#if canImport(SwiftUI) && DEBUG
import SwiftUI

Expand Down

0 comments on commit 77e5f7e

Please sign in to comment.