Skip to content

Instantly share code, notes, and snippets.

@radityagumay
Last active November 11, 2017 12:42

Revisions

  1. radityagumay revised this gist Nov 11, 2017. 2 changed files with 0 additions and 26 deletions.
    16 changes: 0 additions & 16 deletions FooFragment.kt
    Original file line number Diff line number Diff line change
    @@ -1,16 +0,0 @@
    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"))
    }
    }
    10 changes: 0 additions & 10 deletions FooPresenter.kt
    Original file line number Diff line number Diff line change
    @@ -1,10 +0,0 @@
    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")
    }
    }
  2. radityagumay revised this gist Nov 11, 2017. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions FooPresenter.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    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")
    }
    }
  3. radityagumay revised this gist Nov 11, 2017. 1 changed file with 41 additions and 0 deletions.
    41 changes: 41 additions & 0 deletions DataSource.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    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)
    }
    }
  4. radityagumay revised this gist Nov 11, 2017. 1 changed file with 13 additions and 1 deletion.
    14 changes: 13 additions & 1 deletion FooFragment.kt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,16 @@
    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"))
    }
    }
  5. radityagumay renamed this gist Nov 11, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. radityagumay created this gist Nov 11, 2017.
    4 changes: 4 additions & 0 deletions FooFragment
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    class FooFragment : BaseFragment<FooPresenter.View, FooPresenter>() {


    }