From 0d9e30589a5bf50e1a76608149a8fd9d4351c4f0 Mon Sep 17 00:00:00 2001 From: Narendra Singh Anjana Date: Wed, 30 Aug 2023 14:50:03 +0530 Subject: [PATCH] Gsoc 2023 migrating active elections fragment to compose (#36) * Update for some issue In Deploy to Appetize * removed Github Token from ci file * Migrated fragment_elections.xml.xml to ElectionsScreen.kt in compose * Migrated fragment_finished_elections.xmle.xml.xml to ElectionsScreen.kt in compose * Migrated fragment_active_elections.xml.xmle.xml.xml to ElectionsScreen.kt in compose --- .../ActiveElectionsFragment.kt | 93 ++++++------------- .../DisplayElectionViewModel.kt | 21 ++++- .../res/layout/fragment_active_elections.xml | 81 ---------------- app/src/main/res/navigation/nav_graph.xml | 3 +- 4 files changed, 48 insertions(+), 150 deletions(-) delete mode 100644 app/src/main/res/layout/fragment_active_elections.xml diff --git a/app/src/main/java/org/aossie/agoraandroid/ui/fragments/displayelections/ActiveElectionsFragment.kt b/app/src/main/java/org/aossie/agoraandroid/ui/fragments/displayelections/ActiveElectionsFragment.kt index 18f626ab..8ffdeab0 100644 --- a/app/src/main/java/org/aossie/agoraandroid/ui/fragments/displayelections/ActiveElectionsFragment.kt +++ b/app/src/main/java/org/aossie/agoraandroid/ui/fragments/displayelections/ActiveElectionsFragment.kt @@ -4,21 +4,16 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import androidx.core.widget.doAfterTextChanged +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.ui.platform.ComposeView import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.lifecycle.ViewModelProvider -import androidx.lifecycle.lifecycleScope -import androidx.navigation.Navigation -import androidx.recyclerview.widget.LinearLayoutManager -import androidx.recyclerview.widget.RecyclerView -import kotlinx.coroutines.launch -import org.aossie.agoraandroid.data.adapters.ElectionsAdapter -import org.aossie.agoraandroid.databinding.FragmentActiveElectionsBinding -import org.aossie.agoraandroid.domain.model.ElectionModel +import androidx.navigation.fragment.findNavController import org.aossie.agoraandroid.ui.fragments.BaseFragment -import org.aossie.agoraandroid.utilities.hide -import org.aossie.agoraandroid.utilities.show +import org.aossie.agoraandroid.ui.screens.elections.ElectionsScreen +import org.aossie.agoraandroid.ui.theme.AgoraTheme import javax.inject.Inject /** @@ -33,79 +28,49 @@ constructor( private val displayElectionViewModel: DisplayElectionViewModel by viewModels { viewModelFactory } - - lateinit var mElections: ArrayList - private lateinit var electionsAdapter: ElectionsAdapter private val onItemClicked = { _id: String -> val action = ActiveElectionsFragmentDirections.actionActiveElectionsFragmentToElectionDetailsFragment(_id) - Navigation.findNavController(binding.root) - .navigate(action) + findNavController().navigate(action) } - private lateinit var binding: FragmentActiveElectionsBinding + private lateinit var composeView: ComposeView override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { - binding = FragmentActiveElectionsBinding.inflate(inflater) - return binding.root + return ComposeView(requireContext()).also { + composeView = it + } } override fun onFragmentInitiated() { - - mElections = ArrayList() - electionsAdapter = ElectionsAdapter(onItemClicked) - binding.rvActiveElections.apply { - layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false) - adapter = electionsAdapter - } - binding.searchView.doAfterTextChanged { - filter(it.toString()) + bindUI() + composeView.setContent { + val elections by displayElectionViewModel.activeElections.collectAsState() + val progressErrorState by displayElectionViewModel.progressAndErrorState.collectAsState() + val searchText by displayElectionViewModel.search + AgoraTheme { + ElectionsScreen( + screenState = progressErrorState, + elections = elections, + searchText = searchText, + onSearch = { + displayElectionViewModel.getActiveElectionsState(it) + }, + onItemClicked = onItemClicked + ) + } } - } - override fun onNetworkConnected() { - bindUI() } - override fun onActivityCreated(savedInstanceState: Bundle?) { - super.onActivityCreated(savedInstanceState) + override fun onNetworkConnected() { bindUI() } private fun bindUI() { - lifecycleScope.launch { - try { - val elections = displayElectionViewModel.activeElections.await() - elections.collect { - if (it != null) { - addElections(it) - } - } - } catch (e: IllegalStateException) { - binding.tvSomethingWentWrong.show() - } - } - } - - private fun addElections(elections: List) { - if (elections.isNotEmpty()) { - mElections.addAll(elections) - electionsAdapter.submitList(elections) - } else { - binding.tvEmptyElection.show() - } - } - - private fun filter(query: String) { - val updatedList = displayElectionViewModel.filter(mElections, query) - electionsAdapter.submitList(updatedList) - if (updatedList.isEmpty()) { - binding.tvEmptyElection.show() - } else { - binding.tvEmptyElection.hide() - } + displayElectionViewModel.getActiveElectionsState("") } } diff --git a/app/src/main/java/org/aossie/agoraandroid/ui/fragments/displayelections/DisplayElectionViewModel.kt b/app/src/main/java/org/aossie/agoraandroid/ui/fragments/displayelections/DisplayElectionViewModel.kt index 6f1dfe4b..ca2ca034 100644 --- a/app/src/main/java/org/aossie/agoraandroid/ui/fragments/displayelections/DisplayElectionViewModel.kt +++ b/app/src/main/java/org/aossie/agoraandroid/ui/fragments/displayelections/DisplayElectionViewModel.kt @@ -31,9 +31,7 @@ constructor( .time private val date: String = formatter.format(currentDate) - val activeElections by lazyDeferred { - displayElectionsUseCases.getActiveElections(date) - } + val activeElections = MutableStateFlow>(emptyList()) val pendingElections by lazyDeferred { displayElectionsUseCases.getPendingElections(date) } @@ -61,6 +59,23 @@ constructor( } } + fun getActiveElectionsState(query:String) { + viewModelScope.launch { + try { + displayElectionsUseCases.getActiveElections(date).collectLatest { list -> + search.value = query + if(query.isEmpty()) { + activeElections.emit(list) + }else{ + activeElections.emit(filter(list, query)) + } + } + } catch (e: IllegalStateException) { + showMessage(R.string.something_went_wrong_please_try_again_later) + } + } + } + private fun showLoading(message: Any) { _progressAndErrorState.value = progressAndErrorState.value.copy( loading = Pair(message,true) diff --git a/app/src/main/res/layout/fragment_active_elections.xml b/app/src/main/res/layout/fragment_active_elections.xml deleted file mode 100644 index 7a485d9a..00000000 --- a/app/src/main/res/layout/fragment_active_elections.xml +++ /dev/null @@ -1,81 +0,0 @@ - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/navigation/nav_graph.xml b/app/src/main/res/navigation/nav_graph.xml index eee6bd8e..221cbdfd 100644 --- a/app/src/main/res/navigation/nav_graph.xml +++ b/app/src/main/res/navigation/nav_graph.xml @@ -188,8 +188,7 @@ + android:label="Active Elections">