Last active
November 11, 2017 12:42
-
-
Save radityagumay/8d5c9d899a8606f23025253654390506 to your computer and use it in GitHub Desktop.
Fragment Navigation Pattern
This file contains hidden or 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 DataSourceBase | |
class DataSource { | |
companion object { | |
private var sInstance: DataSource? = null | |
private var LOCK = Any() | |
fun getInstance(): DataSource? { | |
synchronized(LOCK) { | |
if (sInstance == null) { | |
sInstance = DataSource() | |
} | |
} | |
return sInstance | |
} | |
} | |
private var map: WeakHashMap<Any, DataSourceBase> = WeakHashMap() | |
fun <R : DataSourceBase> push(key: Any, data: R?) { | |
data?.let { | |
map.put(key, data) | |
} | |
} | |
@Suppress("UNCHECKED_CAST") | |
fun <R : DataSourceBase> peek(key: Any): R? { | |
return map[key] as? R? | |
} | |
@Suppress("UNCHECKED_CAST") | |
fun <R : DataSourceBase> pop(key: Any): R? { | |
val data = map[key] as R | |
map.remove(key) | |
return data | |
} | |
fun remove(key: Any) { | |
map.remove(key) | |
} | |
} |
This file contains hidden or 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 FooFragment : BaseFragment<FooPresenter.View, FooPresenter>() { | |
companion object { | |
fun newInstance(data: Bar): FooFragment { | |
val fragment = FooFragment() | |
val bundle = Bundle() | |
bundle.putParcelable("bar", data) | |
fragment.arguments = bundle | |
return fragment | |
} | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
presenter.push(arguments.getParcelable<Bar>("bar")) | |
} | |
} |
This file contains hidden or 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 FooPresenter(private val dataSource: DataSource?) : BasePresenter<FooPresenter.View> { | |
fun push(data: Bar){ | |
dataSource?.push("someKey", data) | |
} | |
fun peek() : Bar { | |
return dataSource?.peek<Bar>("someKey") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment