Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,37 @@ import RD_Network

import Foundation

extension DreamWriteResponse {
func toDomain() -> DreamWriteRequest? {
return nil
extension DreamWriteModifyResponse {
func toDomain() -> DreamWriteEntity {
return .init(main: .init(titleText: self.title,
contentText: self.content ?? "무슨 꿈을 꾸셨나요?",
recordTime: 0.0,
date: self.processedDate),
emotions: self.emotions,
genres: self.genres,
note: .init(noteText: self.note ?? "꿈에 대해 따로 기록할 게 있나요?"))
}

var emotions: [DreamWriteEntity.Emotion] {
var isSelectedArray = Array(repeating: false, count: 5)
if self.emotion != 6 {
isSelectedArray[self.emotion - 1] = true
}
return isSelectedArray.map { DreamWriteEntity.Emotion.init(isSelected: $0) }
}

var genres: [DreamWriteEntity.Genre] {
var isSelectedArray = Array(repeating: false, count: 10)
if self.genre.first != 0 {
for selectedItem in self.genre {
isSelectedArray[selectedItem - 1] = true
}
}
return isSelectedArray.map { DreamWriteEntity.Genre(isSelected: $0) }
}

var processedDate: String {
guard let date = self.date.split(separator: " ").first else { return "" }
return date.replacingOccurrences(of: "/", with: "-")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Domain
import RD_Network

import RxSwift
import AVFoundation

public class DefaultDreamWriteRepository {

Expand Down Expand Up @@ -45,25 +46,40 @@ extension DefaultDreamWriteRepository: DreamWriteRepository {
}
}

public func fetchDreamRecord(recordId: String) -> Observable<DreamWriteEntity> {
public func fetchDreamRecord(recordId: String) -> Observable<DreamWriteEntity?> {
return Observable.create { observer in
observer.onNext(.init(main: .init(titleText: "안녕하세요", contentText: "내용입니다", recordTime: "01:24", date: "2022-04-03"),
emotions: [.init(isSelected: false),
.init(isSelected: false),
.init(isSelected: false),
.init(isSelected: true),
.init(isSelected: false)],
genres: [.init(isSelected: false),
.init(isSelected: false),
.init(isSelected: true),
.init(isSelected: false),
.init(isSelected: false),
.init(isSelected: false),
.init(isSelected: true),
.init(isSelected: true),
.init(isSelected: false),
.init(isSelected: false)],
note: .init(noteText: "노트 샘플 내용")))
var tempResponse: DreamWriteModifyResponse?
var tempSeconds: Double?
self.recordService.fetchModifyRecord(recordId: recordId)
.do(onNext: { response in
guard let response = response else {
observer.onNext(nil)
return
}
tempResponse = response
guard tempResponse?.voice?.url != nil else {
observer.onNext(response.toDomain())
return
}
})
.compactMap { $0?.voice?.url }
.flatMap { self.recordService.downloadVoiceRecord(url: $0) }
.do(onNext: { fileURL in
guard let url = URL.init(string: fileURL),
let audioPlayer = try? AVAudioPlayer(contentsOf: url) else {
observer.onNext(tempResponse?.toDomain())
return
}
tempSeconds = audioPlayer.duration
})
.subscribe(onNext: { response in
guard var entity = tempResponse?.toDomain() else { return }
entity.changeRecordTime(time: tempSeconds!)
observer.onNext(entity)
}, onError: { err in
observer.onError(err)
})
.disposed(by: self.disposeBag)
return Disposables.create()
}
}
Expand All @@ -81,5 +97,19 @@ extension DefaultDreamWriteRepository: DreamWriteRepository {
return Disposables.create()
}
}

public func modifyDreamRecord(request: DreamWriteRequest, recordId: String) -> Observable<Void> {
return Observable.create { observer in
guard let title = request.title else { return Disposables.create() }
self.recordService.modifyRecord(title: title, date: request.date, content: request.content, emotion: request.emotion, genre: request.genre, note: request.note, voice: request.voice, recordId: recordId)
.subscribe(onNext: { _ in
observer.onNext(())
}, onError: { err in
observer.onError(err)
})
.disposed(by: self.disposeBag)
return Disposables.create()
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

public struct DreamWriteEntity {
public let main: Main
public var main: Main
public let emotions: [Emotion]
public let genres: [Genre]
public let note: Note
Expand Down Expand Up @@ -43,26 +43,34 @@ public struct DreamWriteEntity {
note: self.note.noteText,
voice: nil)
}

public mutating func changeRecordTime(time: Double) {
self.main.changeRecordTime(time: time)
}
}

public extension DreamWriteEntity {
struct Main: Hashable {
public let titleText: String
public let contentText: String
public let recordTime: String?
public var recordTime: Double
public let date: String

public init(titleText: String, contentText: String, recordTime: String?, date: String) {
public init(titleText: String, contentText: String, recordTime: Double, date: String) {
self.titleText = titleText
self.contentText = contentText
self.recordTime = recordTime
self.date = date
}

public mutating func changeRecordTime(time: Double) {
self.recordTime = time
}
}
}

public extension DreamWriteEntity {
struct Emotion: Hashable {
public struct Emotion: Hashable {
let id = UUID()
public let isSelected: Bool

Expand All @@ -73,7 +81,7 @@ public extension DreamWriteEntity {
}

public extension DreamWriteEntity {
struct Genre: Hashable {
public struct Genre: Hashable {
let id = UUID()
public let isSelected: Bool

Expand All @@ -84,7 +92,7 @@ public extension DreamWriteEntity {
}

public extension DreamWriteEntity {
struct Note: Hashable {
public struct Note: Hashable {
public let noteText: String

public init(noteText: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public struct DreamWriteRequest: Codable {
}

public func makeValidFileds(voiceId: String?) -> Self {
let newContent = (content == "무슨 꿈을 꾸셨나요?") ? nil : content
let newNote = (note == "꿈에 대해 따로 기록할 게 있나요?") ? nil : note
let newContent = (content == "무슨 꿈을 꾸셨나요?" || content == "") ? nil : content
let newNote = (note == "꿈에 대해 따로 기록할 게 있나요?" || note == "") ? nil : note
return .init(title: title,
date: date,
content: newContent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import RxSwift

public protocol DreamWriteRepository {
func writeDreamRecord(request: DreamWriteRequest) -> Observable<Void>
func fetchDreamRecord(recordId: String) -> Observable<DreamWriteEntity>
func fetchDreamRecord(recordId: String) -> Observable<DreamWriteEntity?>
func modifyDreamRecord(request: DreamWriteRequest, recordId: String) -> Observable<Void>
func uploadVoice(voiceData: Data) -> Observable<String?>
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import RxSwift
public protocol DreamWriteUseCase {
func writeDreamRecord(request: DreamWriteRequest, voiceId: String?)
func fetchDreamRecord(recordId: String)
func modifyDreamRecord(request: DreamWriteRequest, voiceId: String?, recordId: String)
func titleTextValidate(text: String)
func genreListCautionValidate(genreList: [Int]?)
func uploadVoice(voiceData: Data)

var writeSuccess: PublishSubject<Void> { get set }
var isWriteEnabled: PublishSubject<Bool> { get set }
var showCaution: PublishSubject<Bool> { get set }
var fetchedRecord: PublishSubject<DreamWriteEntity> { get set }
var fetchedRecord: PublishSubject<DreamWriteEntity?> { get set }
var uploadedVoice: PublishSubject<String?> { get set }
}

Expand All @@ -32,7 +33,7 @@ public class DefaultDreamWriteUseCase {
public var writeSuccess = PublishSubject<Void>()
public var isWriteEnabled = PublishSubject<Bool>()
public var showCaution = PublishSubject<Bool>()
public var fetchedRecord = PublishSubject<DreamWriteEntity>()
public var fetchedRecord = PublishSubject<DreamWriteEntity?>()
public var uploadedVoice = PublishSubject<String?>()

public init(repository: DreamWriteRepository) {
Expand All @@ -41,6 +42,7 @@ public class DefaultDreamWriteUseCase {
}

extension DefaultDreamWriteUseCase: DreamWriteUseCase {

public func titleTextValidate(text: String) {
let existDistinctTitle = (text.count > 0 && text != "꿈의 제목을 남겨주세요")
self.isWriteEnabled.onNext(existDistinctTitle)
Expand All @@ -58,6 +60,10 @@ extension DefaultDreamWriteUseCase: DreamWriteUseCase {
self.repository.fetchDreamRecord(recordId: recordId)
.withUnretained(self)
.subscribe(onNext: { strongSelf, entity in
guard let entity = entity else {
strongSelf.fetchedRecord.onNext(nil)
return
}
strongSelf.fetchedRecord.onNext(entity)
strongSelf.genreListCautionValidate(genreList: entity.genreList)
}).disposed(by: self.disposeBag)
Expand All @@ -72,6 +78,15 @@ extension DefaultDreamWriteUseCase: DreamWriteUseCase {
}).disposed(by: self.disposeBag)
}

public func modifyDreamRecord(request: DreamWriteRequest, voiceId: String?, recordId: String) {
let validRequest = request.makeValidFileds(voiceId: voiceId)
self.repository.modifyDreamRecord(request: validRequest, recordId: recordId)
.withUnretained(self)
.subscribe(onNext: { strongSelf, entity in
strongSelf.writeSuccess.onNext(())
}).disposed(by: self.disposeBag)
}

public func uploadVoice(voiceData: Data) {
self.repository.uploadVoice(voiceData: voiceData)
.withUnretained(self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ extension DreamWriteInteractionView {
return String(format: "%02d:%02d", intTime.miniuteDigitInt, intTime.secondsDigitInt)
}()
self.dataLabel.text = totalTimeText

let hasNoRecord = record == 0.0
self.updateEnabledStatus(hasNoRecord)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ extension DreamWriteTextView {
self.textColor = .white.withAlphaComponent(0.4)
}
}).disposed(by: disposeBag)

self.rx.text
.compactMap { $0 }
.subscribe(onNext: { [weak self] text in
guard let self = self else { return }
if text == self.placeHolderText {
self.textColor = .white.withAlphaComponent(0.4)
}
}).disposed(by: disposeBag)
}

@discardableResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,25 @@ extension BaseService {
return Disposables.create()
}
}

func downloadInRx(url: String) -> Observable<String> {
let destination: DownloadRequest.Destination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendingPathComponent("downloadedVoice.m4a")

return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}

return Observable<String>.create { observer in
self.AFManager.download(url, to: destination).response { response in
if response.error == nil, let path = response.fileURL?.path {
observer.onNext(path)
} else {
observer.onError(response.error!)
}
Comment on lines +191 to +206
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

신기방기... 👍

}
return Disposables.create()
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// DreamWriteModifyResponse.swift
// RD-Network
//
// Created by Junho Lee on 2022/12/26.
// Copyright © 2022 RecorDream-iOS. All rights reserved.
//

import Foundation

import Foundation

// MARK: - DreamWriteModifyResponse
public struct DreamWriteModifyResponse: Codable {
public let id, writer, date, title: String
public let voice: Voice?
public let content: String?
public let emotion: Int
public let genre: [Int]
public let note: String?

enum CodingKeys: String, CodingKey {
case id = "_id"
case writer, date, title, voice, content, emotion
case genre, note
}
}

// MARK: - Voice
public struct Voice: Codable {
public let id: String
public let url: String

enum CodingKeys: String, CodingKey {
case id = "_id"
case url
}
}
Loading