Created
September 28, 2021 12:06
-
-
Save pankajjangid/0dce3c860f48f34a6cb0d681ceac478e 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
| package com.pankajjangid.meditqrando.modules.main.prayer | |
| import android.os.Bundle | |
| import android.view.View | |
| import androidx.lifecycle.ViewModelProvider | |
| import androidx.navigation.fragment.findNavController | |
| import androidx.recyclerview.widget.LinearLayoutManager | |
| import com.google.gson.Gson | |
| import com.pankajjangid.meditqrando.R | |
| import com.pankajjangid.meditqrando.base.BaseAdapterCallback | |
| import com.pankajjangid.meditqrando.base.BaseCallback | |
| import com.pankajjangid.meditqrando.base.BaseFragment | |
| import com.pankajjangid.meditqrando.databinding.FragmentPrayerBinding | |
| import com.pankajjangid.meditqrando.models.AudioData | |
| import com.pankajjangid.meditqrando.models.VideoData | |
| import com.pankajjangid.meditqrando.modules.main.MainActivity | |
| import com.pankajjangid.meditqrando.modules.main.prayer.adapter.PrayerAdapter | |
| import com.pankajjangid.meditqrando.modules.player.VideoPlayerActivity | |
| import com.pankajjangid.meditqrando.networking.ApiParams | |
| import com.pankajjangid.meditqrando.networking.response.TimelineResponse | |
| import com.pankajjangid.meditqrando.utils.EndlessOnScrollListener | |
| import com.pankajjangid.meditqrando.utils.Utils | |
| import com.pankajjangid.meditqrando.utils.view_binding.BindingAdapter | |
| import org.kodein.di.Kodein | |
| import org.kodein.di.KodeinAware | |
| import org.kodein.di.android.x.kodein | |
| import org.kodein.di.generic.instance | |
| class PrayerFragment : BaseFragment<FragmentPrayerBinding, PrayerViewModel>(), | |
| KodeinAware, PrayerListener { | |
| companion object { | |
| @JvmStatic | |
| fun newInstance(param1: String, param2: String) = | |
| PrayerFragment().apply { | |
| } | |
| } | |
| /* | |
| private val adapter: PrayerRVAdapter by lazy { | |
| PrayerRVAdapter() | |
| } | |
| */ | |
| /** | |
| * Initialize the necessary variables | |
| */ | |
| var isLoadingProgress = false | |
| var isFinalPage = false | |
| var TOTAL_PAGES = 50 | |
| private val LIMIT = 10 | |
| private var currentPage = 1 | |
| var paginationAdapter: PrayerAdapter? = null | |
| private val factory: PrayerViewModelFactory by instance() | |
| override fun getFragmentView() = R.layout.fragment_prayer | |
| override fun getViewModelFactory(): ViewModelProvider.Factory? = factory | |
| override fun getViewModel(): Class<PrayerViewModel> = PrayerViewModel::class.java | |
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
| super.onViewCreated(view, savedInstanceState) | |
| if (!hasInitializedRootView) { | |
| hasInitializedRootView = true | |
| viewModel.listener = this | |
| binding.viewModel = viewModel | |
| initAdapter() | |
| viewModel.init(requireActivity(), this) | |
| val request = HashMap<String, String>() | |
| request[ApiParams.PAGE] = currentPage.toString() | |
| request[ApiParams.LIMIT] = LIMIT.toString() | |
| viewModel.loadTimeline(request, true) | |
| } | |
| (activity as MainActivity).setViews("", true) | |
| (activity as MainActivity).setTopView(MainActivity.CurrentTopView.PRAYER) | |
| } | |
| private fun initAdapter() { | |
| val linearLayoutManager = LinearLayoutManager( | |
| requireContext(), | |
| LinearLayoutManager.VERTICAL, | |
| false | |
| ) | |
| paginationAdapter = PrayerAdapter(requireContext(),callback = object :BaseAdapterCallback{ | |
| override fun onItemClick(position: Int, tag: Int, item: Any?) { | |
| if (item is TimelineResponse.Data){ | |
| when (item.content_type_id) { | |
| PrayerAdapter.ContentViewType.VIDEO.ordinal -> { | |
| val data = VideoData( | |
| id = item.id, | |
| duration = item.content_media?.duration ?: "", | |
| extension = item.content_media?.extension ?: "", | |
| originalName = item.content_media?.original_name ?: "", | |
| url = item.content_media?.url ?: "" | |
| ) | |
| Utils.startNewActivity(requireActivity(),VideoPlayerActivity::class.java,VideoPlayerActivity.KEY_DATA,Gson().toJson(data),false) | |
| } | |
| PrayerAdapter.ContentViewType.AUDIO.ordinal -> { | |
| val data = AudioData( | |
| id = item.id, | |
| duration = item.content_media?.duration ?: "", | |
| extension = item.content_media?.extension ?: "", | |
| originalName = item.content_media?.original_name ?: "", | |
| url = item.content_media?.url ?: "", | |
| imageUrl = item.content_image_media?.url ?: "", | |
| ) | |
| val action = PrayerFragmentDirections.actionPrayerFragmentToMusicDetailsFragment(data,null) | |
| findNavController().navigate(action) | |
| } | |
| PrayerAdapter.ContentViewType.IMAGE.ordinal -> { | |
| } | |
| PrayerAdapter.ContentViewType.POST.ordinal -> { | |
| } | |
| } | |
| } | |
| } | |
| }) | |
| binding.rvPrayer.layoutManager = linearLayoutManager | |
| binding.rvPrayer.adapter = paginationAdapter | |
| binding.rvPrayer.addOnScrollListener(object : EndlessOnScrollListener(linearLayoutManager) { | |
| override fun onScrolledToEnd() { | |
| //Check loading not progress and is not a final page | |
| if (!isLoadingProgress && !isFinalPage) { | |
| isLoadingProgress = true | |
| // add 10 by 10 to tempList then notify changing in data | |
| currentPage += 1 | |
| val request = HashMap<String, String>() | |
| request[ApiParams.PAGE] = currentPage.toString() | |
| request[ApiParams.LIMIT] = LIMIT.toString() | |
| viewModel.loadNextPage(request) | |
| } | |
| isLoadingProgress = false | |
| } | |
| }) | |
| } | |
| override val kodein: Kodein by kodein() | |
| override fun onLoading() { | |
| showProgress() | |
| } | |
| override fun onSuccess(string: String) { | |
| } | |
| override fun onFailure(message: String) { | |
| hideProgress() | |
| } | |
| fun palishData(response: TimelineResponse) { | |
| hideProgress() | |
| TOTAL_PAGES = response.total | |
| paginationAdapter!!.addAll(response.data); | |
| if (currentPage <= TOTAL_PAGES) paginationAdapter!!.addLoadingFooter(); | |
| else isFinalPage = true; | |
| } | |
| fun palishNextData(finalResponse: TimelineResponse) { | |
| hideProgress() | |
| //Check if data list is empty than it is final page | |
| if (finalResponse.data.isEmpty()) { | |
| isFinalPage = true | |
| } | |
| isLoadingProgress = false | |
| val results: List<TimelineResponse.Data> = finalResponse.data | |
| paginationAdapter!!.addAll(results) | |
| if (currentPage != TOTAL_PAGES) paginationAdapter!!.addLoadingFooter() else isFinalPage = | |
| true | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment