Created
November 1, 2022 15:46
-
-
Save erdalkaymak/a23eb66a152a59695f9d271d15d6bbc2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @ExperimentalCoroutinesApi | |
| @AndroidEntryPoint | |
| class ViewPagerChapterFragment : | |
| BaseFragment<FragmentViewPagerChapterBinding, ViewPagerChapterViewModel>(R.layout.fragment_view_pager_chapter), | |
| OnRequestAction { | |
| // var region | |
| override val viewModel: ViewPagerChapterViewModel by viewModels() | |
| @Inject | |
| lateinit var authService: HwAuthService | |
| private var chapterId = 0 | |
| private var chapterContentList: List<ChapterContentModel>? = null | |
| private val tabTitles = arrayListOf<String>() | |
| private val fragmentList = arrayListOf<Fragment>() | |
| private var contentQuestionIndexList = ArrayList<Int>() | |
| private var designList: List<String>? = null | |
| private val args: ViewPagerChapterFragmentArgs by navArgs() | |
| // end region | |
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
| super.onViewCreated(view, savedInstanceState) | |
| chapterId = args.chapterId | |
| CommonConstants.CHAPTER_ID = chapterId | |
| val userId = authService.getUserId() | |
| showProgressDialog() | |
| viewModel.getChapter(userId, chapterId) | |
| initObserver() | |
| } | |
| /** | |
| * initialize the fragmentList, adapter,viewPager2 and tabTitles | |
| */ | |
| private fun initObserver() { | |
| viewModel.chapter.observe(viewLifecycleOwner, Observer { | |
| when (it.status) { | |
| Resource.Status.SUCCESS -> { | |
| it.data.let { res -> | |
| hideProgressDialog() | |
| if (res?.contents != null) { | |
| chapterContentList = res.contents!! | |
| designList = res.design | |
| val designSequence = 0 until designList!!.size | |
| for (i in designSequence) { | |
| contentQuestionIndexList = | |
| getContentAndQuestionIndex(chapterContentList!!, i) | |
| val contentIndex = contentQuestionIndexList[0] | |
| val questionIndex = contentQuestionIndexList[1] | |
| if (designList!![i].equals("info")) { | |
| fragmentList.add( | |
| i, | |
| ChapterDescriptionFragment(chapterContentList!![contentIndex]) | |
| ) | |
| tabTitles.add(i, "I") | |
| } else { | |
| fragmentList.add( | |
| i, | |
| ChapterQuestionFragment(chapterContentList!![contentIndex].questions!![questionIndex]) | |
| ) | |
| tabTitles.add(i, "Q") | |
| } | |
| } | |
| fragmentList.add(ChapterCompletedFragment()) | |
| tabTitles.add("C") | |
| initialize() | |
| } | |
| } | |
| } | |
| Resource.Status.LOADING -> { | |
| Toast.makeText(context, "Loading", Toast.LENGTH_SHORT).show() | |
| hideProgressDialog() | |
| } | |
| Resource.Status.ERROR -> { | |
| LogUtils.d("$this prepareView") | |
| Toast.makeText(context, it.message, Toast.LENGTH_SHORT).show() | |
| hideProgressDialog() | |
| } | |
| } | |
| }) | |
| } | |
| // algorithm for get chapter content sequence with description and questions | |
| private fun getContentAndQuestionIndex( | |
| contents: List<ChapterContentModel>, | |
| designPosition: Int | |
| ): ArrayList<Int> { | |
| val myList = ArrayList<Int>() | |
| var sum = 0 | |
| var questionIndex = -1 | |
| val contentIndices = contents.indices | |
| var contentIndex = 0 | |
| for (i in contentIndices) { | |
| sum += (contents[i].questions!!.size + 1) | |
| if (designPosition >= sum) { | |
| contentIndex += 1 | |
| } | |
| } | |
| myList.add(contentIndex) | |
| if (designList!![designPosition].equals("question")) { | |
| val contentNumber = 0 until contentIndex | |
| var untilSumSize = 0 | |
| if (contentIndex == 0) { | |
| questionIndex = | |
| abs(designPosition - (contents[contentIndex].questions!!.size + 1)) - 1 | |
| } else { | |
| for (i in contentNumber) { | |
| untilSumSize += (contents[i].questions!!.size + 1) | |
| } | |
| questionIndex = abs(designPosition - untilSumSize) - 1 | |
| } | |
| } | |
| myList.add(questionIndex) | |
| return myList | |
| } | |
| private fun initialize() { | |
| val adapter = ViewPagerChapterAdapter( | |
| fragmentList, | |
| childFragmentManager, | |
| lifecycle | |
| ) | |
| binding.viewPager2.adapter = adapter | |
| binding.viewPager2.isUserInputEnabled = false | |
| TabLayoutMediator(binding.tabLayout, binding.viewPager2) { tab, position -> | |
| tab.text = tabTitles[position] | |
| binding.viewPager2.setCurrentItem(tab.position, true) | |
| }.attach() | |
| binding.tabLayout.touchables.forEach { it.isClickable = false } | |
| } | |
| override fun nextPage() { | |
| var page = binding.viewPager2.currentItem | |
| if (designList!![page].equals("question")) { | |
| showSnackBar(binding.root, "Correct Answer") | |
| } | |
| binding.viewPager2.currentItem = ++page | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment