-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#171 feat: 위치 버튼을 눌렀을 때 현재 위치의 좌표를 받아오는 일련의 흐름 구축
- Loading branch information
1 parent
f7346a3
commit cf8ab4c
Showing
5 changed files
with
235 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// | ||
// LocationSession.swift | ||
// Segno | ||
// | ||
// Created by YOONJONG on 2022/12/08. | ||
// | ||
|
||
import CoreLocation | ||
import Foundation | ||
|
||
import RxSwift | ||
|
||
protocol LocationRepository { | ||
var locationSubject: PublishSubject<Location> { get set } | ||
var addressSubject: PublishSubject<String> { get set } | ||
func getLocation() | ||
func stopLocation() | ||
} | ||
|
||
final class LocationRepositoryImpl: NSObject, LocationRepository { | ||
var locationSubject = PublishSubject<Location>() | ||
var addressSubject = PublishSubject<String>() | ||
private var locationManager = CLLocationManager() | ||
|
||
override init() { | ||
|
||
} | ||
|
||
func getLocation() { | ||
locationManager.delegate = self | ||
locationManager.desiredAccuracy = kCLLocationAccuracyBest | ||
locationManager.requestWhenInUseAuthorization() | ||
DispatchQueue.global().async { | ||
if CLLocationManager.locationServicesEnabled() { | ||
debugPrint("위치 서비스 on") | ||
self.locationManager.startUpdatingLocation() | ||
} else { | ||
print("위치 서비스 off 상태") | ||
} | ||
} | ||
} | ||
|
||
func stopLocation() { | ||
DispatchQueue.global().async { | ||
self.locationManager.stopUpdatingLocation() | ||
} | ||
} | ||
|
||
func getAddress(location: CLLocation) { | ||
let geocoder = CLGeocoder() | ||
let locale = Locale(identifier: "Ko-kr") | ||
geocoder.reverseGeocodeLocation(location, preferredLocale: locale) { placemarks, error in | ||
guard let placemarks = placemarks, | ||
let address = placemarks.last else { return } | ||
// debugPrint("description : ", address.description) | ||
let fullAddress = address.description.components(separatedBy: ", ")[1] | ||
let array = Array(fullAddress.components(separatedBy: " ").dropFirst()) | ||
let refinedAddress = array.joined(separator: " ") | ||
debugPrint("full : ", refinedAddress) | ||
|
||
self.addressSubject.onNext(refinedAddress) | ||
} | ||
} | ||
} | ||
|
||
extension LocationRepositoryImpl: CLLocationManagerDelegate { | ||
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { | ||
debugPrint("didUpdateLocations") | ||
if let cllocation = locations.first { | ||
print("위도 : \(cllocation.coordinate.latitude)") | ||
print("경도 : \(cllocation.coordinate.longitude)") | ||
let location = Location(latitude: cllocation.coordinate.latitude, longitude: cllocation.coordinate.longitude) | ||
getAddress(location: cllocation) | ||
locationSubject.onNext(location) | ||
} | ||
} | ||
|
||
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { | ||
debugPrint("didFailWithError") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// LocationUseCase.swift | ||
// Segno | ||
// | ||
// Created by YOONJONG on 2022/12/08. | ||
// | ||
|
||
import Foundation | ||
|
||
import RxSwift | ||
|
||
protocol LocationUseCase { | ||
var locationSubject: PublishSubject<Location> { get set } | ||
var addressSubject: PublishSubject<String> { get set } | ||
func getLocation() | ||
func stopLocation() | ||
} | ||
|
||
final class LocationUseCaseImpl: LocationUseCase { | ||
var locationSubject = PublishSubject<Location>() | ||
var addressSubject = PublishSubject<String>() | ||
let repository: LocationRepository | ||
private let disposeBag = DisposeBag() | ||
|
||
init(repository: LocationRepository = LocationRepositoryImpl()) { | ||
self.repository = repository | ||
subscribeResults() | ||
} | ||
|
||
func getLocation() { | ||
repository.getLocation() | ||
} | ||
|
||
func stopLocation() { | ||
repository.stopLocation() | ||
} | ||
|
||
private func subscribeResults() { | ||
repository.addressSubject | ||
.bind(to: addressSubject) | ||
.disposed(by: disposeBag) | ||
|
||
repository.locationSubject | ||
.bind(to: locationSubject) | ||
.disposed(by: disposeBag) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.