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
class ExampleViewModel(private val locationProvider: LocationProvider, | |
private val latLongSnapInteractor: LatLongSnapInteractor) { | |
fun onInit(): Observable<LatLong> { | |
return locationProvider.latLong() | |
.doOnNext { latLongSnapInteractor.setLatLong(it) } | |
} | |
} |
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
class ExampleActivity: AppCompatActivity { | |
@Inject lateinit var interactorController: InteractorController | |
private var destroyDisposable: Disposable? = null | |
private var pauseDisposable: Disposable? = null | |
fun onCreated() { | |
destroyDisposable = interactorController.onInit() | |
} | |
fun onDestroyed() { | |
destroyDisposable?.dispose() | |
} |
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
class InteractorController(private val mapProvider:MapProvider, | |
private varargs val interactors: Interactor) { | |
fun onInit(): Completable { | |
return mapProvider.initMap() | |
.switchMapCompletable { map -> | |
Observable.fromIterable(mapInteractors.asIterable()) | |
.flatMapCompletable { mapInteractor -> | |
mapInteractor.onInit(map) | |
} | |
} |
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
class LatLongSnapInteractor : MapBehavior { | |
private var latLong = ObservableField<LatLong>(LatLong.DEFAULT) | |
fun setLatLong(latLong: LatLong) { | |
this.latLong.set(latLong) | |
} | |
// will activate during resume-pause | |
fun onVisible(map:Map): Completable { | |
return latLong.asObservable() |
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
class GoogleMapProvider: MapProvider { | |
private val mapCache: BehaviorSubject<Map> = BehaviorSubject.createDefault(DEFAULT) | |
override fun initMap(): Observable<Map> { | |
return Observable.create(this::addMapFragment) | |
.switchMapSingle(this::waitingForLoading) | |
.doOnNext(mapCache::onNext) | |
} | |
override fun getMap(): Observable<Map> = mapCache.hide() |
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
Single.create { emitter: SingleEmitter<Map> -> | |
mapFragment.getMapAsync { map: GoogleMap? -> | |
map?.let { actualMap -> | |
actualMap.setOnMapLoadedCallback { | |
emitter.onSuccess(GoogleMapImpl(actualMap)) | |
} | |
} ?: run { emitter.tryOnError(IllegalStateException("No Map")) } | |
} | |
} |
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
emitter.setCancellable { | |
fragmentManager.beginTransaction() | |
.remove(mapFragment) | |
.commitAllowingStateLoss() | |
mapCache.onNext(DEFAULT) | |
} |
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
val mapFragment = SupportMapFragment() | |
mapFragment.retainInstance = false | |
fragmentManager.beginTransaction() | |
.replace(mapParams.resId, mapFragment) | |
.commit() |
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
interface MapProvider { | |
fun initiateMap(): Observable<Map> | |
fun getMap(): Observable<Map> | |
} |
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
// 기존 | |
lifecycleController.addObserver(onInit { Observable.create().subscribe() }) | |
// 코드 추가 | |
interface LifecycleDelegate { operator fun LifecycleObserver.unaryPlus() } | |
class LifecycleController : LifecycleDelegate { | |
fun LifecycleObserver.unaryPlus() { | |
this@LifecycleController.observers += this | |
} | |
} |