Skip to content

Commit

Permalink
all: refactor to new FragmentStateAdapter (fixes #3307) (#3310)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Mar 26, 2024
1 parent 4aa5b98 commit caef19e
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 72 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "org.ole.planet.myplanet"
minSdkVersion 21
targetSdkVersion 34
versionCode 1448
versionName "0.14.48"
versionCode 1449
versionName "0.14.49"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ package org.ole.planet.myplanet.ui.community

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import org.ole.planet.myplanet.MainApplication.Companion.context
import org.ole.planet.myplanet.R
import org.ole.planet.myplanet.ui.enterprises.EnterpriseCalendarFragment
import org.ole.planet.myplanet.ui.enterprises.FinanceFragment
import org.ole.planet.myplanet.ui.news.NewsFragment

class CommunityPagerAdapter(fm: FragmentManager, val id: String, var fromLogin: Boolean) :
FragmentStatePagerAdapter(fm) {
class CommunityPagerAdapter(fm: FragmentActivity, private val id: String, private var fromLogin: Boolean) : FragmentStateAdapter(fm) {
var titles = arrayOf(context.getString(R.string.news), context.getString(R.string.community_leaders), context.getString(R.string.calendar), context.getString(
R.string.services), context.getString(R.string.finances))
var titles_login = arrayOf(context.getString(R.string.news), context.getString(R.string.community_leaders), context.getString(R.string.calendar))
override fun getItem(position: Int): Fragment {
override fun createFragment(position: Int): Fragment {
val fragment: Fragment = when (position) {
0 -> {
NewsFragment()
Expand All @@ -40,11 +39,19 @@ class CommunityPagerAdapter(fm: FragmentManager, val id: String, var fromLogin:
return fragment
}

override fun getCount(): Int {
return if (fromLogin) titles_login.size else titles.size
override fun getItemCount(): Int {
return if (fromLogin) {
titles_login.size
} else {
titles.size
}
}

override fun getPageTitle(position: Int): CharSequence {
return titles[position]
fun getPageTitle(position: Int): CharSequence {
return if (fromLogin) {
if (position < titles_login.size) titles_login[position] else ""
} else {
if (position < titles.size) titles[position] else ""
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.google.android.material.tabs.TabLayoutMediator
import org.ole.planet.myplanet.databinding.FragmentTeamDetailBinding
import org.ole.planet.myplanet.service.UserProfileDbHandler
import org.ole.planet.myplanet.ui.sync.SyncActivity
Expand All @@ -14,9 +15,7 @@ import java.util.Date

class CommunityTabFragment : Fragment() {
private lateinit var fragmentTeamDetailBinding: FragmentTeamDetailBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
fragmentTeamDetailBinding = FragmentTeamDetailBinding.inflate(inflater, container, false)
return fragmentTeamDetailBinding.root
}
Expand All @@ -25,11 +24,14 @@ class CommunityTabFragment : Fragment() {
val settings = requireActivity().getSharedPreferences(SyncActivity.PREFS_NAME, Context.MODE_PRIVATE)
val sParentcode = settings.getString("parentCode", "")
val user = UserProfileDbHandler(requireActivity()).userModel
fragmentTeamDetailBinding.viewPager.adapter = CommunityPagerAdapter(childFragmentManager, user?.planetCode + "@" + sParentcode, false)
fragmentTeamDetailBinding.tabLayout.setupWithViewPager(fragmentTeamDetailBinding.viewPager)
fragmentTeamDetailBinding.viewPager2?.adapter = CommunityPagerAdapter(requireActivity(), user?.planetCode + "@" + sParentcode, false)
fragmentTeamDetailBinding.viewPager2?.let {
TabLayoutMediator(fragmentTeamDetailBinding.tabLayout, it) { tab, position ->
tab.text = (fragmentTeamDetailBinding.viewPager2?.adapter as CommunityPagerAdapter).getPageTitle(position)
}.attach()
}
fragmentTeamDetailBinding.title.text = user?.planetCode
fragmentTeamDetailBinding.subtitle.text = TimeUtils.getFormatedDateWithTime(Date().time)
fragmentTeamDetailBinding.llActionButtons.visibility = View.GONE
fragmentTeamDetailBinding.tabLayout.setupWithViewPager(fragmentTeamDetailBinding.viewPager)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.android.material.tabs.TabLayoutMediator
import org.ole.planet.myplanet.R
import org.ole.planet.myplanet.databinding.FragmentTeamDetailBinding
import org.ole.planet.myplanet.service.UserProfileDbHandler
Expand All @@ -16,9 +17,7 @@ import java.util.Date

class HomeCommunityDialogFragment : BottomSheetDialogFragment() {
private lateinit var fragmentTeamDetailBinding: FragmentTeamDetailBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
fragmentTeamDetailBinding = FragmentTeamDetailBinding.inflate(inflater, container, false)
return fragmentTeamDetailBinding.root
}
Expand All @@ -34,11 +33,16 @@ class HomeCommunityDialogFragment : BottomSheetDialogFragment() {
val sPlanetcode = settings.getString("planetCode", "")
val sParentcode = settings.getString("parentCode", "")
val user = UserProfileDbHandler(requireActivity()).userModel
fragmentTeamDetailBinding.viewPager.adapter = CommunityPagerAdapter(childFragmentManager, user?.planetCode + "@" + sParentcode, true)
fragmentTeamDetailBinding.viewPager2?.adapter = CommunityPagerAdapter(requireActivity(), user?.planetCode + "@" + sParentcode, true)
fragmentTeamDetailBinding.viewPager2?.let {
TabLayoutMediator(fragmentTeamDetailBinding.tabLayout, it) { tab, position ->
tab.text = (fragmentTeamDetailBinding.viewPager2?.adapter as CommunityPagerAdapter).getPageTitle(position)
}.attach()
}

fragmentTeamDetailBinding.title.text = user?.planetCode
fragmentTeamDetailBinding.title.setTextColor(ContextCompat.getColor(requireContext(), R.color.md_black_1000))
fragmentTeamDetailBinding.subtitle.setTextColor(ContextCompat.getColor(requireContext(), R.color.md_black_1000))
fragmentTeamDetailBinding.subtitle.text = TimeUtils.getFormatedDateWithTime(Date().time)
fragmentTeamDetailBinding.tabLayout.setupWithViewPager(fragmentTeamDetailBinding.viewPager)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package org.ole.planet.myplanet.ui.course

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager2.adapter.FragmentStateAdapter

class CoursePagerAdapter(fm: FragmentManager, private val courseId: String?, private val steps: Array<String?>) : FragmentStatePagerAdapter(fm) {
override fun getItem(position: Int): Fragment {
class CoursePagerAdapter(fm: Fragment, private val courseId: String?, private val steps: Array<String?>) : FragmentStateAdapter(fm) {
override fun createFragment(position: Int): Fragment {
val b = Bundle()
val f: Fragment
if (position == 0) {
Expand All @@ -21,7 +23,7 @@ class CoursePagerAdapter(fm: FragmentManager, private val courseId: String?, pri
return f
}

override fun getCount(): Int {
override fun getItemCount(): Int {
return steps.size + 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,22 @@ class TakeCourseFragment : Fragment(), ViewPager.OnPageChangeListener, View.OnCl
fragmentTakeCourseBinding.nextStep.visibility = View.GONE
fragmentTakeCourseBinding.previousStep.visibility = View.GONE
}
fragmentTakeCourseBinding.viewPagerCourse.adapter = CoursePagerAdapter(
childFragmentManager, courseId, getStepIds(mRealm, courseId)
// fragmentTakeCourseBinding.viewPager2.adapter = CoursePagerAdapter(
// childFragmentManager, courseId, getStepIds(mRealm, courseId)
// )
// fragmentTakeCourseBinding.viewPager2.addOnPageChangeListener(this)
fragmentTakeCourseBinding.viewPager2.adapter = CoursePagerAdapter(
this, courseId, getStepIds(mRealm, courseId)
)
fragmentTakeCourseBinding.viewPagerCourse.addOnPageChangeListener(this)
if (fragmentTakeCourseBinding.viewPagerCourse.currentItem == 0) {

// Disable swipe gestures
fragmentTakeCourseBinding.viewPager2.isUserInputEnabled = false
if (fragmentTakeCourseBinding.viewPager2.currentItem == 0) {
fragmentTakeCourseBinding.previousStep.visibility = View.GONE
}
setCourseData()
setListeners()
fragmentTakeCourseBinding.viewPagerCourse.currentItem = position
fragmentTakeCourseBinding.viewPager2.currentItem = position
}

private fun setListeners() {
Expand All @@ -89,7 +95,7 @@ class TakeCourseFragment : Fragment(), ViewPager.OnPageChangeListener, View.OnCl
val currentProgress =
getCurrentProgress(steps, mRealm, userModel.id, courseId)
if (b && i <= currentProgress + 1) {
fragmentTakeCourseBinding.viewPagerCourse.currentItem = i
fragmentTakeCourseBinding.viewPager2.currentItem = i
}
}

Expand Down Expand Up @@ -153,15 +159,15 @@ class TakeCourseFragment : Fragment(), ViewPager.OnPageChangeListener, View.OnCl
}

private fun onClickNext() {
if (fragmentTakeCourseBinding.viewPagerCourse.currentItem == steps.size) {
if (fragmentTakeCourseBinding.viewPager2.currentItem == steps.size) {
fragmentTakeCourseBinding.nextStep.setTextColor(ContextCompat.getColor(requireContext(), R.color.md_grey_500))
fragmentTakeCourseBinding.nextStep.visibility = View.GONE
fragmentTakeCourseBinding.finishStep.visibility = View.VISIBLE
}
}

private fun onClickPrevious() {
if (fragmentTakeCourseBinding.viewPagerCourse.currentItem - 1 == 0) {
if (fragmentTakeCourseBinding.viewPager2.currentItem - 1 == 0) {
fragmentTakeCourseBinding.previousStep.visibility = View.GONE
fragmentTakeCourseBinding.nextStep.visibility = View.VISIBLE
fragmentTakeCourseBinding.finishStep.visibility = View.GONE
Expand All @@ -172,7 +178,7 @@ class TakeCourseFragment : Fragment(), ViewPager.OnPageChangeListener, View.OnCl
when (view.id) {
R.id.next_step -> {
if (isValidClickRight) {
fragmentTakeCourseBinding.viewPagerCourse.currentItem += 1
fragmentTakeCourseBinding.viewPager2.currentItem += 1
fragmentTakeCourseBinding.previousStep.visibility = View.VISIBLE
}
onClickNext()
Expand All @@ -181,7 +187,7 @@ class TakeCourseFragment : Fragment(), ViewPager.OnPageChangeListener, View.OnCl
R.id.previous_step -> {
onClickPrevious()
if (isValidClickLeft) {
fragmentTakeCourseBinding.viewPagerCourse.currentItem -= 1
fragmentTakeCourseBinding.viewPager2.currentItem -= 1
}
}

Expand All @@ -207,10 +213,9 @@ class TakeCourseFragment : Fragment(), ViewPager.OnPageChangeListener, View.OnCl
}

private val isValidClickRight: Boolean
get() = fragmentTakeCourseBinding.viewPagerCourse.adapter != null && fragmentTakeCourseBinding.viewPagerCourse.currentItem < fragmentTakeCourseBinding.viewPagerCourse.adapter!!
.count
get() = fragmentTakeCourseBinding.viewPager2.adapter != null && fragmentTakeCourseBinding.viewPager2.currentItem < fragmentTakeCourseBinding.viewPager2.adapter?.itemCount!!
private val isValidClickLeft: Boolean
get() = fragmentTakeCourseBinding.viewPagerCourse.adapter != null && fragmentTakeCourseBinding.viewPagerCourse.currentItem > 0
get() = fragmentTakeCourseBinding.viewPager2.adapter != null && fragmentTakeCourseBinding.viewPager2.currentItem > 0

companion object {
@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import com.google.android.material.tabs.TabLayoutMediator
import io.realm.Realm
import org.ole.planet.myplanet.MainApplication
import org.ole.planet.myplanet.R
Expand All @@ -32,8 +33,12 @@ class TeamDetailFragment : Fragment() {
val user = UserProfileDbHandler(requireContext()).userModel!!
mRealm = DatabaseService(requireActivity()).realmInstance
team = mRealm.where(RealmMyTeam::class.java).equalTo("_id", requireArguments().getString("id")).findFirst()
fragmentTeamDetailBinding.viewPager.adapter = TeamPagerAdapter(childFragmentManager, team, isMyTeam)
fragmentTeamDetailBinding.tabLayout.setupWithViewPager(fragmentTeamDetailBinding.viewPager)
fragmentTeamDetailBinding.viewPager2?.adapter = TeamPagerAdapter(requireActivity(), team, isMyTeam)
fragmentTeamDetailBinding.viewPager2?.let {
TabLayoutMediator(fragmentTeamDetailBinding.tabLayout, it) { tab, position ->
tab.text = (fragmentTeamDetailBinding.viewPager2?.adapter as TeamPagerAdapter).getPageTitle(position)
}.attach()
}
if (!isMyTeam) {
fragmentTeamDetailBinding.llActionButtons.visibility = View.GONE
} else {
Expand All @@ -42,13 +47,18 @@ class TeamDetailFragment : Fragment() {
.setPositiveButton(R.string.yes) { _: DialogInterface?, _: Int ->
team?.leave(user, mRealm)
Utilities.toast(activity, getString(R.string.left_team))
fragmentTeamDetailBinding.viewPager.adapter = TeamPagerAdapter(childFragmentManager, team, false)
fragmentTeamDetailBinding.viewPager2?.adapter = TeamPagerAdapter(requireActivity(), team, false)
fragmentTeamDetailBinding.viewPager2?.let {
TabLayoutMediator(fragmentTeamDetailBinding.tabLayout, it) { tab, position ->
tab.text = (fragmentTeamDetailBinding.viewPager2?.adapter as TeamPagerAdapter).getPageTitle(position)
}.attach()
}
fragmentTeamDetailBinding.llActionButtons.visibility = View.GONE
}.setNegativeButton(R.string.no, null).show()
}
fragmentTeamDetailBinding.btnAddDoc.setOnClickListener {
MainApplication.showDownload = true
fragmentTeamDetailBinding.viewPager.currentItem = 6
fragmentTeamDetailBinding.viewPager2?.currentItem = 6
MainApplication.showDownload = false
if (MainApplication.listener != null) {
MainApplication.listener?.onAddDocument()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import android.os.Bundle
import android.text.TextUtils
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import org.ole.planet.myplanet.MainApplication
import org.ole.planet.myplanet.R
import org.ole.planet.myplanet.model.RealmMyTeam
Expand All @@ -20,7 +20,7 @@ import org.ole.planet.myplanet.ui.team.teamMember.MembersFragment
import org.ole.planet.myplanet.ui.team.teamResource.TeamResourceFragment
import org.ole.planet.myplanet.ui.team.teamTask.TeamTaskFragment

class TeamPagerAdapter(fm: FragmentManager?, team: RealmMyTeam?, private val isInMyTeam: Boolean) : FragmentStatePagerAdapter(fm!!) {
class TeamPagerAdapter(fm: FragmentActivity, team: RealmMyTeam?, private val isInMyTeam: Boolean) : FragmentStateAdapter(fm) {
private val teamId: String?
private val list: MutableList<String>
private val isEnterprise: Boolean
Expand Down Expand Up @@ -48,12 +48,12 @@ class TeamPagerAdapter(fm: FragmentManager?, team: RealmMyTeam?, private val isI
}
}

override fun getPageTitle(position: Int): CharSequence {
fun getPageTitle(position: Int): CharSequence {
return list[position]
}

@RequiresApi(Build.VERSION_CODES.O)
override fun getItem(position: Int): Fragment {
override fun createFragment(position: Int): Fragment {
val f: Fragment? = if (!isInMyTeam) {
if (position == 0) PlanFragment() else {
JoinedMemberFragment()
Expand Down Expand Up @@ -105,7 +105,7 @@ class TeamPagerAdapter(fm: FragmentManager?, team: RealmMyTeam?, private val isI
private val fragment: Fragment
get() = if (isEnterprise) FinanceFragment() else TeamCourseFragment()

override fun getCount(): Int {
override fun getItemCount(): Int {
return list.size
}
}

This file was deleted.

4 changes: 2 additions & 2 deletions app/src/main/res/layout/fragment_take_course.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@

</LinearLayout>

<org.ole.planet.myplanet.utilities.CustomViewPager
android:id="@+id/view_pager_course"
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/layout/fragment_team_detail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@
android:layout_height="wrap_content"
app:tabMode="scrollable" />

<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager2"
android:layout_width="match_parent"
android:layout_height="wrap_content">

</androidx.viewpager.widget.ViewPager>
</androidx.viewpager2.widget.ViewPager2>

</LinearLayout>

Expand Down

0 comments on commit caef19e

Please sign in to comment.