diff options
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/fragments')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt | 49 | ||||
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt | 23 |
2 files changed, 66 insertions, 6 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 diff --git a/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt b/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt index 140d8df1..d7bf4061 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt @@ -1,7 +1,6 @@ package com.pitchedapps.frost.fragments import android.os.Bundle -import android.support.v4.app.Fragment import android.support.v4.widget.SwipeRefreshLayout import android.view.LayoutInflater import android.view.View @@ -9,10 +8,10 @@ import android.view.ViewGroup import butterknife.ButterKnife import butterknife.Unbinder import com.pitchedapps.frost.R -import com.pitchedapps.frost.facebook.FBURL +import com.pitchedapps.frost.facebook.FbUrl import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.bindView -import com.pitchedapps.frost.utils.withBundle +import com.pitchedapps.frost.utils.putString import com.pitchedapps.frost.views.FrostWebView import com.pitchedapps.frost.views.SwipeRefreshBase import com.pitchedapps.frost.views.WebStatus @@ -22,7 +21,11 @@ import com.pitchedapps.frost.views.WebStatus */ -class WebFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener { +class WebFragment : BaseFragment(), SwipeRefreshLayout.OnRefreshListener { + + override fun onActivityEvent(position: Int, key: Int) { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } override fun onRefresh() { web.reload() @@ -30,8 +33,8 @@ class WebFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener { companion object { private val ARG_URL = "arg_url" - fun newInstance(url: String) = WebFragment().withBundle { b -> b.putString(ARG_URL, url) } - fun newInstance(url: FBURL = FBURL.FEED) = newInstance(url.url) + fun newInstance(position: Int, url: String) = BaseFragment.newInstance(WebFragment(), position).putString(ARG_URL, url) + fun newInstance(position: Int, url: FbUrl = FbUrl.FEED) = newInstance(position, url.url) } val refresh: SwipeRefreshBase by bindView(R.id.swipe_refresh) @@ -72,4 +75,12 @@ class WebFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener { super.onDestroyView() unbinder.unbind() } + + override fun onBackPressed(): Boolean { + if (web.canGoBack()) { + web.goBack() + return true + } + return false + } }
\ No newline at end of file |