Skip to content

Commit

Permalink
Gsoc 2023 migrating active elections fragment to compose (#36)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
narendraanjana09 authored Aug 30, 2023
1 parent 7402bb9 commit 0d9e305
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -33,79 +28,49 @@ constructor(
private val displayElectionViewModel: DisplayElectionViewModel by viewModels {
viewModelFactory
}

lateinit var mElections: ArrayList<ElectionModel>
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<ElectionModel>) {
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("")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ constructor(
.time
private val date: String = formatter.format(currentDate)

val activeElections by lazyDeferred {
displayElectionsUseCases.getActiveElections(date)
}
val activeElections = MutableStateFlow<List<ElectionModel>>(emptyList())
val pendingElections by lazyDeferred {
displayElectionsUseCases.getPendingElections(date)
}
Expand Down Expand Up @@ -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)
Expand Down
81 changes: 0 additions & 81 deletions app/src/main/res/layout/fragment_active_elections.xml

This file was deleted.

3 changes: 1 addition & 2 deletions app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@
<fragment
android:id="@+id/activeElectionsFragment"
android:name="org.aossie.agoraandroid.ui.fragments.displayelections.ActiveElectionsFragment"
android:label="Active Elections"
tools:layout="@layout/fragment_active_elections">
android:label="Active Elections">
<action
android:id="@+id/action_activeElectionsFragment_to_electionDetailsFragment"
app:destination="@+id/electionDetailsFragment"
Expand Down

0 comments on commit 0d9e305

Please sign in to comment.