Skip to content

Commit

Permalink
feat : UserSingleton 생성 #2
Browse files Browse the repository at this point in the history
- 빈번하게 유저정보에 접근하므로 Singleton 객체로 생성하여 관리.
  • Loading branch information
wongbingg committed Jul 13, 2024
1 parent 13a5d33 commit e67421f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
6 changes: 3 additions & 3 deletions Particle/Particle/Sources/Data/Network/RecordMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protocol RecordMapperProtocol {
struct RecordMapper: RecordMapperProtocol {

func mapRecords(model: [RecordReadDTO]) -> [SectionOfRecordTag] {
guard let userInterestedTags = UserDefaults.standard.object(forKey: "INTERESTED_TAGS") as? [String] else {
guard let userInterestedTags = UserSingleton.shared.info?.interestedTags else {
return []
}
guard model.isEmpty == false else { return [] }
Expand All @@ -24,8 +24,8 @@ struct RecordMapper: RecordMapperProtocol {
a.fetchDate().timeIntervalSince1970 > b.fetchDate().timeIntervalSince1970
}
var sectionList: [SectionOfRecordTag] = [.init(header: "My", items: sortedModel)]
let tags = userInterestedTags.map { $0.replacingOccurrences(of: "#", with: "")}
for tag in tags {
// let tags = userInterestedTags.map { $0.replacingOccurrences(of: "#", with: "")}
for tag in userInterestedTags {
let filteredList = sortedModel.filter { $0.tags.contains(tag) }
if filteredList.isEmpty == false {
sectionList.append(.init(header: tag, items: filteredList))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ final class SetAdditionalInformationViewController: UIViewController,
}

private func fetchInterestedTags() {
guard let userInterestedTags = UserDefaults.standard.object(forKey: "INTERESTED_TAGS") as? [String] else { return }
let fetchedTags = userInterestedTags.map { ($0, false) }
guard let userInterestedTags = UserSingleton.shared.info?.interestedTags else { return }
let fetchedTags = userInterestedTags.map { ("#\($0)", false) }
tags.accept(fetchedTags)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ final class RecordDetailViewController: UIViewController,
}
.disposed(by: disposeBag)

guard let interestedRecords = UserSingleton.shared.info?.interestedRecords else { return }
let bool = interestedRecords.contains(data.value.id)
isLike.accept(bool)
heartButton.setImage(UIImage(named: bool ? "heartFull" : "heart"), for: .normal)

isLike
.skip(1)
.bind { [weak self] bool in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ final class Accordion: UIView {

private var disposeBag = DisposeBag()
private var interestedTags: [String] {
if let interestedTags = UserDefaults.standard.object(forKey: "INTERESTED_TAGS") as? [String] {
return interestedTags
if let interestedTags = UserSingleton.shared.info?.interestedTags {
return interestedTags.map { "#\($0)" }
} else {
return []
}
Expand Down
27 changes: 27 additions & 0 deletions Particle/Particle/Sources/Utils/UserSingleton.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// UserSingleton.swift
// Particle
//
// Created by 이원빈 on 7/13/24.
//

import Foundation

final class UserSingleton {
static let shared = UserSingleton()
private let userRepository: UserRepository

var info: UserReadDTO? {
do {
return try userRepository.getMyProfile()
} catch {
Console.error(error.localizedDescription)
return nil
}
}

private init() {
let userDataSource = LocalUserDataSource(coreData: .shared)
self.userRepository = DefaultUserRepository(userDataSource: userDataSource)
}
}

0 comments on commit e67421f

Please sign in to comment.