ARTICLE AD BOX
My question is not android specific. it is rather more design-specific.
Previously I had a BaseFragment that many child fragments were derived from. base fragment had objects of mapbox map to handle map for app features. after a while the company wanted me to add another map sdk to the project and handle them both on runtime by a feature flag. So i change the design from this
abstract class BaseFragment: MapboxCallBack1,MapBoxCallBacl2 { val mapBoxMap val mapView ... fun config(){ mapBoxMap.addCallback1Listener(this) mapBoxMap.addCallback2Listener(this) } } class ChildFragment1 : BaseFragment { override fun mapBoxCallBackFun1Impl: { ... } override fun mapBoxCallBackFun2Impl: { ... } } class ChildFragment2 : BaseFragment { override fun mapBoxCallBackFun1Impl: { ... } override fun mapBoxCallBackFun2Impl: { ... } }to this mix of provider and factory pattern style
interface MapProvider { fun bareMapFeature1() fun bareMapFeature2() fun bareMapFeature3() } class MapBoxProviderImpl : MapProvider { override fun bareMapFeature1(){ ... } override fun bareMapFeature2(){ ... } override fun bareMapFeature3(){ ... } } class NewMapSdkProviderImpl : MapProvider { override fun bareMapFeature1(){ ... } override fun bareMapFeature2(){ ... } override fun bareMapFeature3(){ ... } } abstract class BaseFragment { open val mapProvider: MapProvider ... } class ChildFragment1 : BaseFragment { override val mapProvider by lazy { MapProviderFactory.create(mapType) } } class ChildFragment2 : BaseFragment { override val mapProvider by lazy { MapProviderFactory.create(mapType) } }the question is. how do i make sdk map callbacks (rather mapbox or any other sdk) clean and abstract to fragments.
i did this. defined some abstract custom callbacks based on sdk callbacks. made the base fragment implement it so that because base fragment is abstract the childs would override them like before. but this time not the map specific callbacks. then moved each map-specific callback to the corresponding mapProvider implementation so implementations would override them. then injecting those abstract callbacks to the constructor of map provider implementations. then map provider implementations can call costume callback's methods when map calls the sdk-specific callbacks itself.
interface CustomeSdkAgnosticCallBack1 { fun onClick() } class NewMapSdkProviderImpl(customeCallback1: CustomeSdkAgnosticCallBack1) : MapProvider, MapBoxCallback { fun config(){ mapbox.addlistener(this) } override fun bareMapFeature1(){ ... } override fun bareMapFeature2(){ ... } override fun bareMapFeature3(){ ... } //mapbox specific sdk callback method override fun onClick(){ customeCallback1.onClick() } } abstract class BaseFragment, CutomeCallback1 { open val mapProvider: MapProvider ... } class ChildFragment1 : BaseFragment { override val mapProvider by lazy { MapProviderFactory.create(mapType) } //custome callback method override fun onClick(){ //bluf1 //bluf2 } } class ChildFragment2 : BaseFragment { override val mapProvider by lazy { MapProviderFactory.create(mapType) } //custome callback method override fun onClick(){ //bluf1 //bluf2 //bluf3 //bluf4 } }my problem with solution is that i think refrencing child fragments in map provider implementation even if the refrence is the customecallbacks is not good and correct.
please give your advise or sugesstion on making it clean. my goal is to make base fragment and child fragments clean and agnostic from any map sdk or library while there are callbacks where child fragments should react to those callbacks whitout knowing the sdk. and again i think refrencing fragments in map implementations by the custome callback is bad
thanks in advance
