Skip to content

feat: Add zip in multiple network calls #2010

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
Jun 28, 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 @@ -5,6 +5,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

import io.reactivex.disposables.CompositeDisposable
import io.reactivex.functions.BiFunction
import io.reactivex.rxkotlin.plusAssign
import org.fossasia.openevent.general.BuildConfig.MAPBOX_KEY
import org.fossasia.openevent.general.R
Expand Down Expand Up @@ -139,28 +140,17 @@ class EventDetailsViewModel(
fun fetchSimilarEvents(eventId: Long, topicId: Long, location: String?) {
if (eventId == -1L) return

var similarEventsFlowable = eventService.getEventsByLocation(location)
if (topicId != -1L) {
compositeDisposable += eventService.getSimilarEvents(topicId)
.withDefaultSchedulers()
.distinctUntilChanged()
.subscribe({ events ->
val list = events.filter { it.id != eventId }
val oldList = mutableSimilarEvents.value

val similarEventList = mutableSetOf<Event>()
similarEventList.addAll(list)
oldList?.let {
similarEventList.addAll(it)
}
mutableSimilarEvents.value = similarEventList
}, {
Timber.e(it, "Error fetching similar events")
mutablePopMessage.value = resource.getString(R.string.error_fetching_event_section_message,
resource.getString(R.string.similar_events))
similarEventsFlowable = similarEventsFlowable.zipWith(eventService.getSimilarEvents(topicId),
BiFunction { firstList: List<Event>, secondList: List<Event> ->
val similarList = mutableListOf<Event>()
similarList.addAll(firstList)
similarList.addAll(secondList)
similarList
})
}

compositeDisposable += eventService.getEventsByLocation(location)
compositeDisposable += similarEventsFlowable
.withDefaultSchedulers()
.distinctUntilChanged()
.subscribe({ events ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class OrderCompletedFragment : Fragment() {
loadEventDetails(it)
eventShare = it
val eventTopicId = it.eventTopic?.id ?: 0
orderCompletedViewModel.fetchSimilarEvents(safeArgs.eventId, eventTopicId)
val location = it.searchableLocationName ?: it.locationName
orderCompletedViewModel.fetchSimilarEvents(safeArgs.eventId, eventTopicId, location)
})

orderCompletedViewModel.similarEvents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.functions.BiFunction
import io.reactivex.rxkotlin.plusAssign
import org.fossasia.openevent.general.utils.extensions.withDefaultSchedulers
import org.fossasia.openevent.general.R
Expand Down Expand Up @@ -42,29 +43,37 @@ class OrderCompletedViewModel(private val eventService: EventService, private va
})
}

fun fetchSimilarEvents(eventId: Long, topicId: Long) {
fun fetchSimilarEvents(eventId: Long, topicId: Long, location: String?) {
if (eventId == -1L) return

if (topicId != -1L) {
compositeDisposable += eventService.getSimilarEvents(topicId)
.withDefaultSchedulers()
.doOnSubscribe {
mutableProgress.value = true
}.subscribe({ events ->
val list = events.filter { it.id != eventId }
val oldList = mutableSimilarEvents.value
var similarEventsFlowable = eventService.getEventsByLocation(location)

val similarEventList = mutableSetOf<Event>()
similarEventList.addAll(list)
oldList?.let {
similarEventList.addAll(it)
}
mutableProgress.value = false
mutableSimilarEvents.value = similarEventList
}, {
Timber.e(it, "Error fetching similar events")
if (topicId != -1L) {
similarEventsFlowable = similarEventsFlowable.zipWith(eventService.getSimilarEvents(topicId),
BiFunction { firstList: List<Event>, secondList: List<Event> ->
val similarList = mutableListOf<Event>()
similarList.addAll(firstList)
similarList.addAll(secondList)
similarList
})
}

compositeDisposable += similarEventsFlowable
.withDefaultSchedulers()
.distinctUntilChanged()
.subscribe({ events ->
val list = events.filter { it.id != eventId }
val oldList = mutableSimilarEvents.value
val similarEventList = mutableSetOf<Event>()
similarEventList.addAll(list)
oldList?.let {
similarEventList.addAll(it)
}
mutableProgress.value = false
mutableSimilarEvents.value = similarEventList
}, {
Timber.e(it, "Error fetching similar events")
})
}

fun setFavorite(eventId: Long, favorite: Boolean) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/item_card_similar_events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
android:layout_width="@dimen/fab_width"
android:layout_height="@dimen/fab_height"
android:layout_marginEnd="@dimen/fab_margin_80dp"
android:layout_marginRight="@dimen/fab_margin_80dp"
app:fabCustomSize="@dimen/fab_height"
android:background="@android:color/white"
android:scaleType="center"
Expand All @@ -70,6 +69,7 @@
android:layout_marginEnd="@dimen/fab_margin_right"
android:background="@android:color/white"
android:scaleType="center"
app:isFavorite="@{event.favorite}"
app:backgroundTint="@android:color/white"
app:tint="@color/colorAccent"
app:elevation="@dimen/fab_elevation"
Expand Down