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

Refactor/quests repository (1) #84

Merged
merged 3 commits into from
Dec 6, 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 DailyQuest/DailyQuest.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
A5AC96DF292239CA003B7637 /* RealmSwift in Frameworks */ = {isa = PBXBuildFile; productRef = A5AC96DE292239CA003B7637 /* RealmSwift */; };
A5AC96E629223F06003B7637 /* QuestsStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5AC96E529223F06003B7637 /* QuestsStorage.swift */; };
A5AC96E829223F27003B7637 /* RealmQuestsStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5AC96E729223F27003B7637 /* RealmQuestsStorage.swift */; };
A5D3C813293DB1BE00F43B76 /* DataConfigure.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5D3C812293DB1BE00F43B76 /* DataConfigure.swift */; };
B50078D629222F3F0070AFC4 /* CircleCheckView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B50078D529222F3F0070AFC4 /* CircleCheckView.swift */; };
B58DFC0A29227DA800C68A4B /* CalendarCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58DFC0929227DA800C68A4B /* CalendarCell.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -290,6 +291,7 @@
A5AC96DB292237C3003B7637 /* QuestEntity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuestEntity.swift; sourceTree = "<group>"; };
A5AC96E529223F06003B7637 /* QuestsStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuestsStorage.swift; sourceTree = "<group>"; };
A5AC96E729223F27003B7637 /* RealmQuestsStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RealmQuestsStorage.swift; sourceTree = "<group>"; };
A5D3C812293DB1BE00F43B76 /* DataConfigure.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataConfigure.swift; sourceTree = "<group>"; };
B50078D529222F3F0070AFC4 /* CircleCheckView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CircleCheckView.swift; sourceTree = "<group>"; };
B58DFC0929227DA800C68A4B /* CalendarCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalendarCell.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -791,6 +793,7 @@
34ACC36A291DF04000741371 /* Data */ = {
isa = PBXGroup;
children = (
A5D3C812293DB1BE00F43B76 /* DataConfigure.swift */,
9B1CFB3C292B580C00CCE97A /* Network */,
A5AC96D42922356E003B7637 /* Repositories */,
A5AC96D62922359F003B7637 /* PersistentStorages */,
Expand Down Expand Up @@ -1159,6 +1162,7 @@
34A529D329247903001BAD34 /* TabCoordinator.swift in Sources */,
3417B1462935DA9D00900454 /* DefaultBrowseUseCase.swift in Sources */,
347D258D292C6E220038FCA2 /* MessageBubble.swift in Sources */,
A5D3C813293DB1BE00F43B76 /* DataConfigure.swift in Sources */,
34EE6EB92924CAA1005AF583 /* QuestViewModel.swift in Sources */,
A5656459292BBDD40033E763 /* String+.swift in Sources */,
3429084F29383D73001812B1 /* UserRepository.swift in Sources */,
Expand Down
14 changes: 14 additions & 0 deletions DailyQuest/DailyQuest/Data/DataConfigure.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// DataConfigure.swift
// DailyQuest
//
// Created by 이전희 on 2022/12/05.
//

import Foundation

enum DateFilter {
Copy link
Collaborator

Choose a reason for hiding this comment

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

이게 μš°μ„  month같은 데이터λ₯Ό κ°€μ Έμ˜¬λ•Œ ν•„μš”ν•œκ²ƒμ΄μ§€μš”?
근데 아직은 RealmServiceμ—λ§Œ μžˆλŠ” 것이죠?

case today(_ date: Date)
case month(_ date: Date)
case year(_date: Date)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ struct QuestDTO: DTO {
self.currentCount = 0
self.totalCount = 0
}

init(groupId: UUID, uuid: String, date: String, title: String, currentCount: Int, totalCount: Int) {
self.groupId = groupId
self.uuid = uuid
self.date = date
self.title = title
self.currentCount = currentCount
self.totalCount = totalCount
}
}

extension QuestDTO {
Expand All @@ -35,3 +44,14 @@ extension QuestDTO {
totalCount: totalCount)
}
}

extension Quest {
func toDTO() -> QuestDTO {
return QuestDTO(groupId: groupId,
uuid: uuid.uuidString,
date: date.toString,
title: title,
currentCount: currentCount,
totalCount: totalCount)
}
}
102 changes: 53 additions & 49 deletions DailyQuest/DailyQuest/Data/Repositories/DefaultQuestsRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,80 +11,84 @@ import Foundation
final class DefaultQuestsRepository {

private let persistentStorage: QuestsStorage
private let networkService: NetworkService

init(persistentStorage: QuestsStorage) {
init(persistentStorage: QuestsStorage, networkService: NetworkService = FirebaseService.shared) {
self.persistentStorage = persistentStorage
self.networkService = networkService
}
}

extension DefaultQuestsRepository: QuestsRepository {
func save(with quest: [Quest]) -> Single<[Quest]> {
return persistentStorage.saveQuests(with: quest)
.flatMap { quests in
return Observable.from(quests)
.concatMap { quest in
return self.networkService.create(userCase: .currentUser,
access: .quests,
dto: quest.toDTO())
}
.map { $0.toDomain() }
.toArray()
.asObservable()
.asSingle()
Comment on lines +32 to +35
Copy link
Collaborator

Choose a reason for hiding this comment

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

asObservable()없이 asSingle()은 μ•ˆλ˜λ‚˜μš”?

Copy link
Member Author

Choose a reason for hiding this comment

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

toArrayλ₯Ό ν•˜λŠ” 경우 μžλ£Œν˜•μ΄ λ°”λ€Œμ–΄λ²„λ €μ„œ asSingle을 λ°”λ‘œ 싀행이 μ•ˆ λ˜μ–΄ asObservable둜 obseravale둜 λ°”κΎΌλ’€ single둜 λ°”κΎΈμ—ˆμŠ΅λ‹ˆλ‹€ 😊

Copy link
Collaborator

@jinwoong16 jinwoong16 Dec 5, 2022

Choose a reason for hiding this comment

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

μ œκ°€ μ•ŒκΈ°λ‘  toArray()의 κ²°κ³ΌλŠ” single이 λ˜λŠ”κ±Έλ‘œ κΈ°μ–΅ν•΄μš”. (μ •ν™•ν•˜μ§„ μ•Šμ§€λ§Œ..!)
λ¨Έμ§€λ˜λ©΄ ν•œλ²ˆλ” μ•Œμ•„λ΄μ•Όκ² λ„€μš”. λ§Œμ•½ λ§žλ‹€λ©΄ 34,35λ²ˆμ€„μ€ 없어도 될지도..?

Copy link
Member Author

Choose a reason for hiding this comment

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

ν™•μΈν•΄λ³΄λ‹ˆ κ·Έλ ‡λ„€μš”. 방금 κ³ μ³μ„œ μ»€λ°‹ν•΄λ†¨μŠ΅λ‹ˆλ‹€. toArray의 λ°˜ν™˜κ°’μ΄ PrimitiveSequence<SingleTrait, [Quest]>λΌμ„œ 고쳀던건데 κ°€λŠ₯ν•˜λ„€μš” κ°μ‚¬ν•©λ‹ˆλ‹€

Copy link
Collaborator

Choose a reason for hiding this comment

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

사싀 Single은 PrimitiveSequence<SingleTrait, Element>의 νƒ€μž…μ—˜λ¦¬μ–΄μŠ€(typealias)라고 ν•©λ‹ˆλ‹€!
여기글을 확인해보면 μ’‹μ„κ²ƒκ°™μ•„μš”:)

}
}

func fetch(by date: Date) -> Observable<[Quest]> {
return persistentStorage.fetchQuests(by: date)
.catch { _ in
self.networkService.read(type: QuestDTO.self, userCase: .currentUser, access: .quests, filter: .month(Date()))
.map { $0.toDomain() }
.toArray()
.asObservable()
}
}

func update(with quest: Quest) -> Single<Quest> {
return persistentStorage.updateQuest(with: quest)
.flatMap { quest in
self.networkService.update(userCase: .currentUser, access: .quests, dto: quest.toDTO())
}
.map { $0.toDomain() }
.asObservable()
.asSingle()
}

func delete(with questId: UUID) -> Single<Quest> {
return persistentStorage.deleteQuest(with: questId)
.flatMap { quest in
self.networkService.delete(userCase: .currentUser, access: .quests, dto: quest.toDTO())
}
.map { $0.toDomain() }
.asObservable()
.asSingle()
}

func deleteAll(with groupId: UUID) -> Single<[Quest]> {
return persistentStorage.deleteQuestGroup(with: groupId)
}

func fetch(by uuid: String) -> Observable<[Quest]> {
return .just([])
}
}

extension DefaultQuestsRepository {
static func test() {
let persistentStorage = RealmQuestsStorage()
let repository = DefaultQuestsRepository(persistentStorage: persistentStorage)
let dummyDate = ["2022-11-17".toDate()!,
"2022-11-18".toDate()!,
"2022-11-19".toDate()!,
"2022-11-20".toDate()!,
"2022-11-21".toDate()!,
"2022-11-22".toDate()!,
"2022-11-23".toDate()!,
"2022-11-24".toDate()!]
let dummyData = (0...10).compactMap { i in
Quest(groupId: UUID(), uuid: UUID(), date: dummyDate[i % dummyDate.count], title: "\(i) dummyData", currentCount: 0, totalCount: i % 5)
}

print("πŸ€save")
let _ = repository.save(with: dummyData)
.subscribe { event in
print(event)
}

print("πŸ€fetch \(Date())")

let _ = repository.fetch(by: Date())
.subscribe { event in
print(event)
}

let temp = dummyData[0]
print("πŸ€update \(temp.uuid)")
let quest = Quest(groupId: temp.groupId, uuid: temp.uuid, date: temp.date, title: "change", currentCount: temp.currentCount + 1, totalCount: temp.totalCount)
let _ = repository.update(with: quest)
.subscribe { event in
print(event)
}

print("πŸ€delete \(dummyData[1].uuid)")
let _ = repository.delete(with: dummyData[1].uuid)
.subscribe { event in
print(event)
.flatMap { quests in
return Observable.from(quests)
.concatMap { quest in
self.networkService.delete(userCase: .currentUser,
access: .quests,
dto: quest.toDTO())
}
.map { $0.toDomain() }
.toArray()
.asObservable()
.asSingle()
}
}

func fetch(by uuid: String) -> Observable<[Quest]> { // 받을 λ‚ μ§œκΉŒμ§€ 받아와야함
return self.networkService.read(type: QuestDTO.self,
userCase: .anotherUser(uuid),
access: .quests,
filter: .month(Date()))
.map { $0.toDomain() }
.toArray()
.asObservable()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ final class FirebaseService: NetworkService {
/// - access: quests / receiveQuests / userInfo
/// - condition: quests - today(date) / month(date) / year(date)
/// - Returns: Observable<T>
func read<T: DTO>(type: T.Type, userCase: UserCase, access: Access, filter: NetworkDateFilter? = nil) -> Observable<T> {
func read<T: DTO>(type: T.Type, userCase: UserCase, access: Access, filter: DateFilter? = nil) -> Observable<T> {
return Observable<T>.create { [weak self] observer in
do {
guard let self = self else { throw NetworkServiceError.noNetworkService }
Expand Down
6 changes: 0 additions & 6 deletions DailyQuest/DailyQuest/Infrastructure/NetworkConfigure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ enum CRUD {
case delete
}

enum NetworkDateFilter {
case today(_ date: Date)
case month(_ date: Date)
case year(_date: Date)
}

enum StoragePath {
case profileImages
case backgroundImages
Expand Down
2 changes: 1 addition & 1 deletion DailyQuest/DailyQuest/Infrastructure/NetworkService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protocol NetworkService {
func signOut() -> Single<Bool>

func create<T: DTO>(userCase: UserCase, access: Access, dto: T) -> Single<T>
func read<T: DTO>(type: T.Type, userCase: UserCase, access: Access, filter: NetworkDateFilter?) -> Observable<T>
func read<T: DTO>(type: T.Type, userCase: UserCase, access: Access, filter: DateFilter?) -> Observable<T>
func update<T: DTO>(userCase: UserCase, access: Access, dto: T) -> Single<T>
func delete<T: DTO>(userCase: UserCase, access: Access, dto: T) -> Single<T>

Expand Down