Refactoring: use arguments + bundle to pass data to the fragment

od
Laurent 6 years ago
parent 31b9607284
commit b7a4b313b6
  1. 7
      app/src/main/java/net/pokeranalytics/android/ui/activity/BillingActivity.kt
  2. 18
      app/src/main/java/net/pokeranalytics/android/ui/fragment/SubscriptionFragment.kt
  3. 3
      app/src/main/java/net/pokeranalytics/android/ui/fragment/components/BaseFragment.kt

@ -38,15 +38,12 @@ class BillingActivity : BaseActivity() {
private fun initUI() { private fun initUI() {
val fragmentManager = supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
val fragment = SubscriptionFragment()
val showSessionMessage = intent.getBooleanExtra(IntentKey.SHOW_MESSAGE.keyName, false) val showSessionMessage = intent.getBooleanExtra(IntentKey.SHOW_MESSAGE.keyName, false)
val fragment = SubscriptionFragment.newInstance(showSessionMessage)
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.add(R.id.container, fragment) fragmentTransaction.add(R.id.container, fragment)
fragmentTransaction.commit() fragmentTransaction.commit()
fragment.setData(showSessionMessage)
} }

@ -44,6 +44,14 @@ class SubscriptionFragment : BaseFragment(), SkuDetailsResponseListener, Purchas
companion object { companion object {
val parallax: Float = 64f.px val parallax: Float = 64f.px
fun newInstance(showSessionMessage: Boolean) : SubscriptionFragment {
val fragment = SubscriptionFragment()
val bundle = Bundle()
bundle.putBoolean(BundleKey.SHOW_MESSAGE.value, showSessionMessage)
fragment.arguments = bundle
return fragment
}
} }
private var pagerAdapter: ScreenSlidePagerAdapter? = null private var pagerAdapter: ScreenSlidePagerAdapter? = null
@ -71,20 +79,20 @@ class SubscriptionFragment : BaseFragment(), SkuDetailsResponseListener, Purchas
} }
} }
fun setData(showSessionMessage: Boolean) {
this.showSessionMessage = showSessionMessage
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_subscription, container, false) return inflater.inflate(R.layout.fragment_subscription, container, false)
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
initData()
initUI() initUI()
} }
private fun initData() {
this.showSessionMessage = arguments?.getBoolean(BundleKey.SHOW_MESSAGE.value) ?: false
}
private fun initUI() { private fun initUI() {
val upgradeString = requireContext().getString(R.string.pro_upgrade) val upgradeString = requireContext().getString(R.string.pro_upgrade)

@ -17,7 +17,8 @@ abstract class BaseFragment : Fragment() {
enum class BundleKey(val value: String) { enum class BundleKey(val value: String) {
STYLE("style"), STYLE("style"),
PRIMARY_KEY("primary_key"), PRIMARY_KEY("primary_key"),
DATA_TYPE("data_type") DATA_TYPE("data_type"),
SHOW_MESSAGE("show_message")
} }
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {

Loading…
Cancel
Save