|
| 1 | +package fr.free.nrw.commons.bookmarks |
| 2 | + |
| 3 | +import android.os.Bundle |
| 4 | +import android.view.LayoutInflater |
| 5 | +import android.view.View |
| 6 | +import android.view.ViewGroup |
| 7 | +import fr.free.nrw.commons.contributions.ContributionController |
| 8 | +import fr.free.nrw.commons.contributions.MainActivity |
| 9 | +import fr.free.nrw.commons.databinding.FragmentBookmarksBinding |
| 10 | +import fr.free.nrw.commons.di.CommonsDaggerSupportFragment |
| 11 | +import fr.free.nrw.commons.kvstore.JsonKvStore |
| 12 | +import fr.free.nrw.commons.theme.BaseActivity |
| 13 | +import javax.inject.Inject |
| 14 | +import javax.inject.Named |
| 15 | + |
| 16 | +class BookmarkFragment : CommonsDaggerSupportFragment() { |
| 17 | + private var adapter: BookmarksPagerAdapter? = null |
| 18 | + |
| 19 | + @JvmField |
| 20 | + var binding: FragmentBookmarksBinding? = null |
| 21 | + |
| 22 | + @JvmField |
| 23 | + @Inject |
| 24 | + var controller: ContributionController? = null |
| 25 | + |
| 26 | + /** |
| 27 | + * To check if the user is loggedIn or not. |
| 28 | + */ |
| 29 | + @JvmField |
| 30 | + @Inject |
| 31 | + @Named("default_preferences") |
| 32 | + var applicationKvStore: JsonKvStore? = null |
| 33 | + |
| 34 | + fun setScroll(canScroll: Boolean) { |
| 35 | + binding?.let { |
| 36 | + it.viewPagerBookmarks.isCanScroll = canScroll |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + override fun onCreateView( |
| 41 | + inflater: LayoutInflater, |
| 42 | + container: ViewGroup?, |
| 43 | + savedInstanceState: Bundle? |
| 44 | + ): View { |
| 45 | + super.onCreateView(inflater, container, savedInstanceState) |
| 46 | + binding = FragmentBookmarksBinding.inflate(inflater, container, false) |
| 47 | + |
| 48 | + // Activity can call methods in the fragment by acquiring a |
| 49 | + // reference to the Fragment from FragmentManager, using findFragmentById() |
| 50 | + val supportFragmentManager = childFragmentManager |
| 51 | + |
| 52 | + adapter = BookmarksPagerAdapter( |
| 53 | + supportFragmentManager, requireContext(), |
| 54 | + applicationKvStore!!.getBoolean("login_skipped") |
| 55 | + ) |
| 56 | + binding!!.viewPagerBookmarks.adapter = adapter |
| 57 | + binding!!.tabLayout.setupWithViewPager(binding!!.viewPagerBookmarks) |
| 58 | + |
| 59 | + (requireActivity() as MainActivity).showTabs() |
| 60 | + (requireActivity() as BaseActivity).supportActionBar!!.setDisplayHomeAsUpEnabled(false) |
| 61 | + |
| 62 | + setupTabLayout() |
| 63 | + return binding!!.root |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * This method sets up the tab layout. If the adapter has only one element it sets the |
| 68 | + * visibility of tabLayout to gone. |
| 69 | + */ |
| 70 | + fun setupTabLayout() { |
| 71 | + binding!!.tabLayout.visibility = View.VISIBLE |
| 72 | + if (adapter!!.count == 1) { |
| 73 | + binding!!.tabLayout.visibility = View.GONE |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + fun onBackPressed() { |
| 79 | + if (((adapter!!.getItem(binding!!.tabLayout.selectedTabPosition)) as BookmarkListRootFragment).backPressed()) { |
| 80 | + // The event is handled internally by the adapter , no further action required. |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + // Event is not handled by the adapter ( performed back action ) change action bar. |
| 85 | + (requireActivity() as BaseActivity).supportActionBar!!.setDisplayHomeAsUpEnabled(false) |
| 86 | + } |
| 87 | + |
| 88 | + override fun onDestroy() { |
| 89 | + super.onDestroy() |
| 90 | + binding = null |
| 91 | + } |
| 92 | + |
| 93 | + companion object { |
| 94 | + fun newInstance(): BookmarkFragment = BookmarkFragment().apply { |
| 95 | + retainInstance = true |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments