Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

뮤직 세션 레이어 분리 #173

Merged
merged 5 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Segno/Segno.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
4F4E0D7629252236005ABA8F /* LoginEndpoint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F4E0D7529252236005ABA8F /* LoginEndpoint.swift */; };
4F4E0D79292522B7005ABA8F /* BaseURL.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F4E0D78292522B7005ABA8F /* BaseURL.swift */; };
4F4E0D7B29252526005ABA8F /* TokenDTO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F4E0D7A29252526005ABA8F /* TokenDTO.swift */; };
4F51A6FD294096480039D86A /* PlayMusicUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F51A6FC294096480039D86A /* PlayMusicUseCase.swift */; };
4F5291DE293F065D00DF930A /* DiaryEditViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F5291DD293F065D00DF930A /* DiaryEditViewModel.swift */; };
4F589DD6293FB9AB00DB39E5 /* ShazamSongDTO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F589DD5293FB9AB00DB39E5 /* ShazamSongDTO.swift */; };
4F589DD9293FBA0900DB39E5 /* ShazamError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F589DD8293FBA0900DB39E5 /* ShazamError.swift */; };
Expand Down Expand Up @@ -121,6 +122,7 @@
4F4E0D7529252236005ABA8F /* LoginEndpoint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginEndpoint.swift; sourceTree = "<group>"; };
4F4E0D78292522B7005ABA8F /* BaseURL.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseURL.swift; sourceTree = "<group>"; };
4F4E0D7A29252526005ABA8F /* TokenDTO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TokenDTO.swift; sourceTree = "<group>"; };
4F51A6FC294096480039D86A /* PlayMusicUseCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayMusicUseCase.swift; sourceTree = "<group>"; };
4F5291DD293F065D00DF930A /* DiaryEditViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiaryEditViewModel.swift; sourceTree = "<group>"; };
4F589DD5293FB9AB00DB39E5 /* ShazamSongDTO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShazamSongDTO.swift; sourceTree = "<group>"; };
4F589DD8293FBA0900DB39E5 /* ShazamError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShazamError.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -337,6 +339,7 @@
9894EAF429373385005F2B15 /* SettingsUseCase.swift */,
4F307A49293889C200FA36A0 /* SearchMusicUseCase.swift */,
9825F41A29377875005F2163 /* ChangeNicknameUseCase.swift */,
4F51A6FC294096480039D86A /* PlayMusicUseCase.swift */,
);
path = UseCase;
sourceTree = "<group>";
Expand Down Expand Up @@ -627,6 +630,7 @@
66A8CF612935F44100C17F84 /* MyPageViewModel.swift in Sources */,
988414D929235345007C9132 /* DiaryCollectionViewModel.swift in Sources */,
4F589DD6293FB9AB00DB39E5 /* ShazamSongDTO.swift in Sources */,
4F51A6FD294096480039D86A /* PlayMusicUseCase.swift in Sources */,
4FEBFAAF291CF9F300E78139 /* MusicInfo.swift in Sources */,
4FEBFAAB291CF30E00E78139 /* DiaryListItem.swift in Sources */,
7918380829233F7100BC6992 /* UIButton+.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ enum DiaryDetailEndpoint: Endpoint {
}

var path: String {
return "items"
return "diary"
}

var parameters: HTTPRequestParameter? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import Foundation

// TODO: 서버 구현이 완료된 후 그에 맞게 바뀔 예정입니다.
enum DiaryListItemEndpoint: Endpoint {
case item

Expand All @@ -20,7 +19,7 @@ enum DiaryListItemEndpoint: Endpoint {
}

var path: String {
return "items"
return "diary"
}

var parameters: HTTPRequestParameter? {
Expand Down
14 changes: 12 additions & 2 deletions Segno/Segno/Data/Repository/DTO/DiaryDetailDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import Foundation

struct DiaryDetailDTO: Decodable {
//TODO: MusicInfo, Location 추가 및 수정
let id: String
let title: String
let tags: [String]
Expand All @@ -17,7 +16,13 @@ struct DiaryDetailDTO: Decodable {
let musicInfo: MusicInfo?
let location: Location?

init(id: String, title: String, tags: [String], imagePath: String, bodyText: String?, musicInfo: MusicInfo? = nil, location: Location? = nil) {
init(id: String,
title: String,
tags: [String],
imagePath: String,
bodyText: String? = nil,
musicInfo: MusicInfo? = nil,
location: Location? = nil) {
self.id = id
self.title = title
self.tags = tags
Expand All @@ -27,6 +32,11 @@ struct DiaryDetailDTO: Decodable {
self.location = location
}

enum CodingKeys: String, CodingKey {
case id = "_id"
case title, tags, imagePath, bodyText, musicInfo, location
}

#if DEBUG
static let example = DiaryDetailDTO(
id: "id",
Expand Down
5 changes: 2 additions & 3 deletions Segno/Segno/Data/Repository/DTO/DiaryListDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
//

struct DiaryListDTO: Decodable {
let data: [DiaryListItemDTO]
let diaries: [DiaryListItemDTO]

#if DEBUG
static let example = DiaryListDTO(data: [DiaryListItemDTO.exampleData1,
static let example = DiaryListDTO(diaries: [DiaryListItemDTO.exampleData1,
DiaryListItemDTO.exampleData2,
DiaryListItemDTO.exampleData3,
DiaryListItemDTO.exampleData4,
Expand All @@ -21,7 +21,6 @@ struct DiaryListDTO: Decodable {
}

struct DiaryListItemDTO: Decodable {
// TODO: 일단은 다이어리 리스트 아이템과 동일하게 작성. 이후 서버 사이드에 따라 바꾸겠습니다.
let identifier: String
let title: String
let thumbnailPath: String
Expand Down
31 changes: 16 additions & 15 deletions Segno/Segno/Data/Repository/DiaryRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,29 @@ protocol DiaryRepository {

final class DiaryRepositoryImpl: DiaryRepository {
func getDiaryListItem() -> Single<DiaryListDTO> {
// let endpoint = DiaryListItemEndpoint.item
let endpoint = DiaryListItemEndpoint.item

return NetworkManager.shared.call(endpoint)
.map {
let diaryListItemDTO = try JSONDecoder().decode(DiaryListDTO.self, from: $0)
return diaryListItemDTO
}
//
// return NetworkManager.shared.call(endpoint)
// .map {
// let diaryListItemDTO = try JSONDecoder().decode(DiaryListDTO.self, from: $0)
// return diaryListItemDTO
// }

// TODO: 추후에 NetworkManager로 변경
return Single.create { observer -> Disposable in
let dto = DiaryListDTO.example
observer(.success(dto))

return Disposables.create()
}
// // TODO: 추후에 NetworkManager로 변경
// return Single.create { observer -> Disposable in
// let dto = DiaryListDTO.example
// observer(.success(dto))
//
// return Disposables.create()
// }
}

func getDiary(id: String) -> Single<DiaryDetailDTO> {
let endpoint = DiaryDetailEndpoint.item(id)

return NetworkManager.shared.call(endpoint).map {
try JSONDecoder().decode(DiaryDetailDTO.self, from: $0)
debugPrint("repository: \($0)")
return try JSONDecoder().decode(DiaryDetailDTO.self, from: $0)
}

// // TODO: 추후에 NetworkManager로 변경
Expand Down
16 changes: 13 additions & 3 deletions Segno/Segno/Data/Repository/MusicRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ protocol MusicRepository {

func startSearchingMusic()
func stopSearchingMusic()
func playMusic()
func setupMusic(_ song: MusicInfo?)
func toggleMusicPlayer()
func stopPlayingMusic()
}

final class MusicRepositoryImpl: MusicRepository {
Expand All @@ -30,6 +32,10 @@ final class MusicRepositoryImpl: MusicRepository {
subscribeSearchresult()
}

func setupMusic(_ song: MusicInfo?) {
musicSession.fetchMusic(term: song)
}

func startSearchingMusic() {
shazamSession.start()
}
Expand All @@ -38,8 +44,12 @@ final class MusicRepositoryImpl: MusicRepository {
shazamSession.stop()
}

func playMusic() {

func toggleMusicPlayer() {
musicSession.togglePlayer()
}

func stopPlayingMusic() {
musicSession.stopMusic()
}
}

Expand Down
16 changes: 4 additions & 12 deletions Segno/Segno/Data/Session/MusicSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import MusicKit

final class MusicSession {
private var song: Song?

private lazy var player = ApplicationMusicPlayer.shared
private lazy var playerState = player.state

Expand All @@ -28,9 +26,8 @@ final class MusicSession {
let request = MusicCatalogResourceRequest<Song>(matching: \.isrc, equalTo: term.isrc)
let response = try await request.response()
if let item = response.items.first {
song = item
player.queue = [item]
}

} catch (let error) {
// TODO: 에러 처리
print(error.localizedDescription)
Expand All @@ -44,20 +41,14 @@ final class MusicSession {

// 음악을 재생하는 함수
func togglePlayer() {
guard let song else { return }
if !isPlaying {
playMusic(song: song)
playMusic()
} else {
player.pause()
}

// 뷰 컨트롤러를 나갈 때 큐를 비워 준다.
// 뮤직세션, 샤잠세션은 싱글턴 인스턴스로 만들어 주는 것이 좋겠다.
}

private func playMusic(song: Song) {
player.queue = [song]

private func playMusic() {
Task {
do {
try await player.play()
Expand All @@ -70,5 +61,6 @@ final class MusicSession {

func stopMusic() {
player.stop()
player.queue = []
}
}
9 changes: 8 additions & 1 deletion Segno/Segno/Domain/UseCase/DiaryDetailUseCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ final class DiaryDetailUseCaseImpl: DiaryDetailUseCase {

func getDiary(id: String) -> Single<DiaryDetail> {
return repository.getDiary(id: id).map {
DiaryDetail(identifier: $0.id, title: $0.title, tags: $0.tags, imagePath: $0.imagePath, bodyText: $0.bodyText, musicInfo: nil, location: nil)
debugPrint("usecase: \($0)")
return DiaryDetail(identifier: $0.id,
title: $0.title,
tags: $0.tags,
imagePath: $0.imagePath,
bodyText: $0.bodyText,
musicInfo: $0.musicInfo,
location: $0.location)
}
}
}
2 changes: 1 addition & 1 deletion Segno/Segno/Domain/UseCase/DiaryListUseCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class DiaryListUseCaseImpl: DiaryListUseCase {
func getDiaryList() -> Single<[DiaryListItem]> {
return repository.getDiaryListItem()
.map {
$0.data.map { diaryData in
$0.diaries.map { diaryData in
DiaryListItem(identifier: diaryData.identifier, title: diaryData.title, thumbnailPath: diaryData.thumbnailPath)
}
}
Expand Down
34 changes: 34 additions & 0 deletions Segno/Segno/Domain/UseCase/PlayMusicUseCase.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// PlayMusicUseCase.swift
// Segno
//
// Created by Gordon Choi on 2022/12/07.
//

import Foundation

protocol PlayMusicUseCase {
func setupPlayer(_ song: MusicInfo?)
func togglePlayer()
func stopPlaying()
}

final class PlayMusicUseCaseImpl: PlayMusicUseCase {
let musicRepository: MusicRepository

init(musicRepository: MusicRepository = MusicRepositoryImpl()) {
self.musicRepository = musicRepository
}

func setupPlayer(_ song: MusicInfo?) {
musicRepository.setupMusic(song)
}

func togglePlayer() {
musicRepository.toggleMusicPlayer()
}

func stopPlaying() {
musicRepository.stopPlayingMusic()
}
}
2 changes: 0 additions & 2 deletions Segno/Segno/Presentation/View/MusicContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ final class MusicContentView: UIView {
super.init(frame: frame)

setLayout()
// 임시로 정해진 데이터를 넣었습니다.
setMusic(info: MusicInfo.yokohama)
}

required init?(coder: NSCoder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ final class DiaryDetailViewController: UIViewController {
}

private let disposeBag = DisposeBag()
private let musicSession = MusicSession() // 임시로 여기에 놓습니다
private let viewModel: DiaryDetailViewModel
weak var delegate: DiaryDetailViewDelegate?

Expand Down Expand Up @@ -119,12 +118,11 @@ final class DiaryDetailViewController: UIViewController {
bindDiaryItem()
getDiary()

// 뮤직세션 테스트용으로, 임시로 정해진 데이터를 넣어 두었습니다.
musicSession.fetchMusic(term: MusicInfo.yokohama)
// viewModel.testDataInsert() // 임시 투입 메서드입니다.
}

override func viewWillDisappear(_ animated: Bool) {
musicSession.stopMusic()
viewModel.stopMusic()
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
Expand Down Expand Up @@ -242,7 +240,7 @@ final class DiaryDetailViewController: UIViewController {

extension DiaryDetailViewController: MusicContentViewDelegate {
func playButtonTapped() {
musicSession.togglePlayer()
viewModel.toggleMusicPlayer()
}
}

Expand Down
Loading