diff options
author | Allan Wang <me@allanwang.ca> | 2017-05-29 19:47:52 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2017-05-29 19:47:52 -0700 |
commit | 461425eb6054f18cea1990a4117fe8c78e888ddf (patch) | |
tree | 98c8d84cfb231479cf88c3cc413b7c4a8366e42b /app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt | |
parent | e33120d454aeb72acc2c1315bddea31dd2f15bb2 (diff) | |
download | frost-461425eb6054f18cea1990a4117fe8c78e888ddf.tar.gz frost-461425eb6054f18cea1990a4117fe8c78e888ddf.tar.bz2 frost-461425eb6054f18cea1990a4117fe8c78e888ddf.zip |
Add icon tabs and webview observables
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt b/app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt new file mode 100644 index 00000000..435b87a2 --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt @@ -0,0 +1,49 @@ +package com.pitchedapps.frost.fragments + +import android.content.Context +import android.support.v4.app.Fragment +import com.pitchedapps.frost.utils.KeyPairObservable +import com.pitchedapps.frost.utils.L +import com.pitchedapps.frost.utils.putInt +import io.reactivex.disposables.Disposable +import io.reactivex.functions.Consumer + +/** + * Created by Allan Wang on 2017-05-29. + */ +interface BaseFragmentContract { + fun onActivityEvent(position: Int, key: Int) + fun onBackPressed(): Boolean +} + +abstract class BaseFragment : Fragment(), Consumer<Pair<Int, Int>>, BaseFragmentContract { + var disposable: Disposable? = null + val position: Int by lazy { arguments.getInt(ARG_POSITION) } + + companion object { + val ARG_POSITION = "arg_position" + + fun <T : BaseFragment> newInstance(fragment: T, position: Int): T { + fragment.putInt(ARG_POSITION, position) + return fragment + } + } + + override fun onAttach(context: Context?) { + super.onAttach(context) + if (activity is KeyPairObservable && disposable == null) + disposable = (activity as KeyPairObservable).observable.subscribe(this, Consumer { + t: Throwable -> + L.e(t.message ?: "Observable error") + }) + } + + override fun onDestroyView() { + disposable?.dispose() + disposable = null + super.onDestroyView() + } + + override fun accept(t: Pair<Int, Int>) = onActivityEvent(t.first, t.second) + +}
\ No newline at end of file |