Skip to content

fix: retain value of time and type of event #1571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import org.fossasia.openevent.general.paypal.Paypal
import org.fossasia.openevent.general.paypal.PaypalApi
import org.fossasia.openevent.general.search.GeoLocationViewModel
import org.fossasia.openevent.general.search.SearchLocationViewModel
import org.fossasia.openevent.general.search.SearchTimeViewModel
import org.fossasia.openevent.general.search.SearchViewModel
import org.fossasia.openevent.general.search.LocationService
import org.fossasia.openevent.general.search.SearchTypeViewModel
Expand Down Expand Up @@ -146,7 +147,8 @@ val viewModelModule = module {
viewModel { SearchViewModel(get(), get(), get(), get()) }
viewModel { AttendeeViewModel(get(), get(), get(), get(), get(), get(), get()) }
viewModel { SearchLocationViewModel(get()) }
viewModel { SearchTypeViewModel(get()) }
viewModel { SearchTimeViewModel(get()) }
viewModel { SearchTypeViewModel(get(), get()) }
viewModel { TicketsViewModel(get(), get(), get(), get()) }
viewModel { AboutEventViewModel(get(), get()) }
viewModel { SocialLinksViewModel(get(), get()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ import org.fossasia.openevent.general.utils.Utils.setToolbar

class SearchFragment : Fragment() {
private val searchViewModel by viewModel<SearchViewModel>()
private val safeArgs: SearchFragmentArgs? by lazy {
// When search fragment is opened using BottomNav, then fragment arguments are null
// navArgs delegate throws an IllegalStateException when arguments are null, so we construct SearchFragmentArgs
// from the arguments bundle
arguments?.let { SearchFragmentArgs.fromBundle(it) }
}
private lateinit var rootView: View
private lateinit var searchView: SearchView

Expand All @@ -59,8 +53,8 @@ class SearchFragment : Fragment() {
Navigation.findNavController(rootView).navigate(R.id.searchTimeFragment, bundle, getAnimSlide())
}
}

val time = safeArgs?.stringSavedDate
searchViewModel.loadSavedTime()
val time = searchViewModel.savedTime
if (time.isNullOrBlank()) rootView.timeTextView.text = getString(R.string.anytime)
else {
try {
Expand All @@ -74,7 +68,8 @@ class SearchFragment : Fragment() {
rootView.timeTextView.text = time
}
}
val type = safeArgs?.stringSavedType
searchViewModel.loadSavedType()
val type = searchViewModel.savedType
rootView.eventTypeTextView.text = if (type.isNullOrBlank()) getString(R.string.anything) else type

searchViewModel.loadSavedLocation()
Expand Down Expand Up @@ -129,8 +124,8 @@ class SearchFragment : Fragment() {
SearchResultsFragmentArgs.Builder()
.setQuery(query)
.setLocation(rootView.locationTextView.text.toString().nullToEmpty())
.setDate((safeArgs?.stringSavedDate ?: getString(R.string.anytime)).nullToEmpty())
.setType((safeArgs?.stringSavedType ?: getString(R.string.anything)).nullToEmpty())
.setDate((searchViewModel.savedTime ?: getString(R.string.anytime)).nullToEmpty())
.setType((searchViewModel.savedType ?: getString(R.string.anything)).nullToEmpty())
.build()
.toBundle()
.also { bundle ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.fossasia.openevent.general.R
import org.fossasia.openevent.general.event.EventUtils.getSimpleFormattedDate
import java.util.Calendar
import org.fossasia.openevent.general.utils.Utils.setToolbar
import org.koin.androidx.viewmodel.ext.android.viewModel

const val ANYTIME = "Anytime"
const val TODAY = "Today"
Expand All @@ -29,6 +30,8 @@ const val NEXT_MONTH = "In the next month"

class SearchTimeFragment : Fragment() {
private val safeArgs: SearchTimeFragmentArgs by navArgs()
private val searchTimeViewModel by viewModel<SearchTimeViewModel>()

private lateinit var rootView: View

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Expand Down Expand Up @@ -78,9 +81,9 @@ class SearchTimeFragment : Fragment() {
}

private fun redirectToSearch(time: String) {
val args = SearchFragmentArgs.Builder().setStringSavedDate(time).build().toBundle()
searchTimeViewModel.saveTime(time)
val navOptions = NavOptions.Builder().setPopUpTo(R.id.eventsFragment, false).build()
Navigation.findNavController(rootView).navigate(R.id.searchFragment, args, navOptions)
Navigation.findNavController(rootView).navigate(R.id.searchFragment, null, navOptions)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.fossasia.openevent.general.search

import androidx.lifecycle.ViewModel
import org.fossasia.openevent.general.data.Preference

const val SAVED_TIME = "TIME"

class SearchTimeViewModel(private val preference: Preference) : ViewModel() {

fun saveTime(query: String) {
preference.putString(SAVED_TIME, query)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class SearchTypeFragment : Fragment() {
val adapter = ArrayAdapter(context, R.layout.event_type_list, eventTypesList)
rootView.eventTypesLv.adapter = adapter

searchTypeViewModel.eventLocations
searchTypeViewModel.eventTypes
.nonNull()
.observe(this, Observer { list ->
list.forEach {
Expand All @@ -50,8 +50,8 @@ class SearchTypeFragment : Fragment() {
}

private fun redirectToSearch(type: String) {
val args = SearchFragmentArgs.Builder().setStringSavedType(type).build().toBundle()
searchTypeViewModel.saveType(type)
val navOptions = NavOptions.Builder().setPopUpTo(R.id.eventsFragment, false).build()
Navigation.findNavController(rootView).navigate(R.id.searchFragment, args, navOptions)
Navigation.findNavController(rootView).navigate(R.id.searchFragment, null, navOptions)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,38 @@ import androidx.lifecycle.ViewModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers
import org.fossasia.openevent.general.data.Preference
import org.fossasia.openevent.general.event.EventService
import org.fossasia.openevent.general.event.types.EventType
import timber.log.Timber

const val SAVED_TYPE = "TYPE"

class SearchTypeViewModel(
private val preference: Preference,
private val eventService: EventService
) : ViewModel() {

private val compositeDisposable = CompositeDisposable()
private val mutableEventLocations = MutableLiveData<List<EventType>>()
val eventLocations: LiveData<List<EventType>> = mutableEventLocations
private val mutableEventTypes = MutableLiveData<List<EventType>>()
val eventTypes: LiveData<List<EventType>> = mutableEventTypes

fun loadEventTypes() {
compositeDisposable.add(eventService.getEventTypes()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
mutableEventLocations.value = it
mutableEventTypes.value = it
}, {
Timber.e(it, "Error fetching events types")
})
)
}

fun saveType(query: String) {
preference.putString(SAVED_TYPE, query)
}

override fun onCleared() {
super.onCleared()
compositeDisposable.clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.fossasia.openevent.general.data.Preference
import org.fossasia.openevent.general.data.Resource
import org.fossasia.openevent.general.event.Event
import org.fossasia.openevent.general.event.EventService
import org.fossasia.openevent.general.event.types.EventType
import org.fossasia.openevent.general.utils.DateTimeUtils.getNextDate
import org.fossasia.openevent.general.utils.DateTimeUtils.getNextMonth
import org.fossasia.openevent.general.utils.DateTimeUtils.getNextToNextDate
Expand Down Expand Up @@ -43,16 +44,38 @@ class SearchViewModel(
val chipClickable: LiveData<Boolean> = mutableChipClickable
var searchEvent: String? = null
var savedLocation: String? = null
var savedType: String? = null
var savedTime: String? = null
private val savedNextDate = getNextDate()
private val savedNextToNextDate = getNextToNextDate()
private val savedWeekendDate = getWeekendDate()
private val savedWeekendNextDate = getNextToWeekendDate()
private val savedNextMonth = getNextMonth()
private val savedNextToNextMonth = getNextToNextMonth()
private val mutableEventTypes = MutableLiveData<List<EventType>>()
val eventTypes: LiveData<List<EventType>> = mutableEventTypes

fun loadEventTypes() {
compositeDisposable.add(eventService.getEventTypes()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
mutableEventTypes.value = it
}, {
Timber.e(it, "Error fetching events types")
})
)
}

fun loadSavedLocation() {
savedLocation = preference.getString(SAVED_LOCATION)
}
fun loadSavedType() {
savedType = preference.getString(SAVED_TYPE)
}
fun loadSavedTime() {
savedTime = preference.getString(SAVED_TIME)
}

fun loadEvents(location: String, time: String, type: String) {
if (mutableEvents.value != null) {
Expand Down
10 changes: 0 additions & 10 deletions app/src/main/res/navigation/navigation_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@
android:name="org.fossasia.openevent.general.search.SearchFragment"
android:label="SearchFragment"
tools:layout="@layout/fragment_search" >
<argument
android:name="@string/saved_date"
app:argType="string"
android:defaultValue="@null"
app:nullable="true"/>
<argument
android:name="@string/savedType"
app:argType="string"
android:defaultValue="@null"
app:nullable="true"/>
</fragment>
<fragment
android:id="@+id/searchLocationFragment"
Expand Down