Skip to content

Commit

Permalink
[Feat] #259 NearByUseCase 구성
Browse files Browse the repository at this point in the history
  • Loading branch information
isakatty committed Apr 25, 2024
1 parent 1e13675 commit 767635d
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 9 deletions.
9 changes: 9 additions & 0 deletions Projects/App/Sources/AppDelegate+Register.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Foundation

import Widget
import Core
import CoreDataService
import Data
Expand Down Expand Up @@ -92,5 +93,13 @@ extension AppDelegate {
type: RegularAlarmEditingService.self,
regularAlarmEditingService
)

// DIContainer.register(
// type: NearByStopUseCase.self,
// DefaultNearByStopUseCase(
// stationListRepository: stationListRepository,
// locationService: locationService
// )
// )
}
}
79 changes: 79 additions & 0 deletions Projects/App/Widget/NearByStop/DefaultNearByStopUseCase.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// DefaultNearByStopUseCase.swift
// Widget
//
// Created by Jisoo HAM on 4/25/24.
// Copyright © 2024 Pepsi-Club. All rights reserved.
//

import Foundation

import Domain

import RxSwift

final class DefaultNearByStopUseCase: NearByStopUseCase {
private let stationListRepository: StationListRepository
private let locationService: LocationService

public let nearByStopInfo = PublishSubject<(BusStopInfoResponse, String)>()

private let disposeBag = DisposeBag()

public init(
stationListRepository: StationListRepository,
locationService: LocationService
) {
self.stationListRepository = stationListRepository
self.locationService = locationService
}

public func updateNearByStop(
) -> Observable<(BusStopInfoResponse, String)> {
locationService.locationStatus
.withUnretained(self)
.map { useCase, status in
print("✅ : \(status) <- locationStatus")

var response: BusStopInfoResponse
var distanceStr: String
let requestMessage = "확인하려면 위치사용을 허용해주세요"
let waitingMessage = "위치 정보 가져오는 중..."
let errorMessage = "위치 정보를 가져올 수 없습니다"
switch status {
case .authorized(let location),
.alwaysAllowed(let location):
(response, distanceStr) = useCase.stationListRepository
.getNearByStopInfo(startPointLocation: location)
case .waitingForLocation:
response = .init(
busStopName: waitingMessage,
busStopId: "",
direction: "",
longitude: "126.979620",
latitude: "37.570028"
)
distanceStr = ""
case .notDetermined, .denied:
response = .init(
busStopName: requestMessage,
busStopId: "",
direction: "",
longitude: "126.979620",
latitude: "37.570028"
)
distanceStr = ""
case .error:
response = .init(
busStopName: errorMessage,
busStopId: "",
direction: "",
longitude: "126.979620",
latitude: "37.570028"
)
distanceStr = ""
}
return (response, distanceStr)
}
}
}
44 changes: 35 additions & 9 deletions Projects/App/Widget/NearByStop/NearByStopProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,21 @@

import WidgetKit

import Core
import Data
import Domain

import RxSwift

struct NearByStopProvider: TimelineProvider {
private let useCase = DefaultNearByStopUseCase(
stationListRepository: DefaultStationListRepository(),
locationService: DefaultLocationService()
)
// @Injected(NearByStopUseCase.self) var useCase: NearByStopUseCase

private let disposeBag = DisposeBag()

func placeholder(
in context: Context
) -> NearByStopEntry {
Expand All @@ -32,21 +46,33 @@ struct NearByStopProvider: TimelineProvider {
)
}

// 리프레시되는 주기 및 실질적으로 보여질 데이터를 처리하는 공간
func getTimeline(
in context: Context,
completion: @escaping (Timeline<NearByStopEntry>) -> Void
) {
let timeline = Timeline(
entries: [
NearByStopEntry(

useCase.updateNearByStop()
.subscribe(onNext: { response, distance in
var entries: [NearByStopEntry] = []

entries.append(NearByStopEntry(
date: .now,
busStopName: "강남역 2호선",
distance: 60
busStopName: response.busStopName,
distance: Int(distance) ?? 0
))

// 데이터 업데이트를 위한 타임라인 생성
let timeline = Timeline(
entries: entries,
policy: .never
)
],
policy: .never
)
completion(timeline)
print("❤️‍🔥 \(timeline)")

completion(timeline)
})
.disposed(by: disposeBag)

}

typealias Entry = NearByStopEntry
Expand Down
17 changes: 17 additions & 0 deletions Projects/App/Widget/NearByStop/NearByStopUseCase.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// NearByStopUseCase.swift
// Widget
//
// Created by Jisoo HAM on 4/25/24.
// Copyright © 2024 Pepsi-Club. All rights reserved.
//

import Foundation

import Domain

import RxSwift

protocol NearByStopUseCase {
func updateNearByStop() -> Observable<(BusStopInfoResponse, String)>
}

0 comments on commit 767635d

Please sign in to comment.