Skip to content

fix: updated favourites on event navigation & continuous topic api calls #1707

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
May 20, 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 @@ -6,6 +6,7 @@ import androidx.room.OnConflictStrategy.REPLACE
import androidx.room.Query
import io.reactivex.Flowable
import io.reactivex.Single
import org.fossasia.openevent.general.event.topic.EventTopic

@Dao
interface EventDao {
Expand All @@ -28,7 +29,7 @@ interface EventDao {
fun getEventById(eventId: Long): Single<Event>

@Query("SELECT * from Event WHERE id in (:ids)")
fun getEventWithIds(ids: List<Long>): Single<List<Event>>
fun getEventWithIds(ids: List<Long>): Flowable<List<Event>>

@Query("UPDATE Event SET favorite = :favorite WHERE id = :eventId")
fun setFavorite(eventId: Long, favorite: Boolean)
Expand All @@ -39,6 +40,9 @@ interface EventDao {
@Query("SELECT id from Event WHERE favorite = 1 AND id in (:ids)")
fun getFavoriteEventWithinIds(ids: List<Long>): Single<List<Long>>

@Query("SELECT * from Event WHERE eventTopic = :topicId")
fun getAllSimilarEvents(topicId: Long): Flowable<List<Event>>
@Query("SELECT * from Event WHERE eventTopic = :eventTopic")
fun getAllSimilarEvents(eventTopic: EventTopic): Flowable<List<Event>>

@Query("SELECT * FROM EventTopic WHERE id=:topicId")
fun getEventTopic(topicId: Long): Single<EventTopic>
}
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ class EventDetailsFragment : Fragment() {
rootView.similarEventsRecycler.adapter = similarEventsAdapter

eventViewModel.similarEvents.observe(viewLifecycleOwner, Observer { similarEvents ->
similarEventsAdapter.submitList(similarEvents)
similarEventsAdapter.submitList(similarEvents.toList())
rootView.similarEventsContainer.visibility = if (similarEvents.isNotEmpty()) View.VISIBLE else View.GONE
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class EventDetailsViewModel(
val eventSponsors: LiveData<List<Sponsor>> = mutableEventSponsors
private val mutableSocialLinks = MutableLiveData<List<SocialLink>>()
val socialLinks: LiveData<List<SocialLink>> = mutableSocialLinks
private val mutableSimilarEvents = MutableLiveData<List<Event>>()
val similarEvents: LiveData<List<Event>> = mutableSimilarEvents
private val mutableSimilarEvents = MutableLiveData<Set<Event>>()
val similarEvents: LiveData<Set<Event>> = mutableSimilarEvents

fun isLoggedIn() = authHolder.isLoggedIn()

Expand Down Expand Up @@ -127,11 +127,16 @@ class EventDetailsViewModel(
if (topicId != -1L) {
compositeDisposable += eventService.getSimilarEvents(topicId)
.withDefaultSchedulers()
.subscribe({
val similarEventList = mutableListOf<Event>()
mutableSimilarEvents.value?.let { currentEvents -> similarEventList.addAll(currentEvents) }
val list = it.filter { it.id != eventId }
.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")
Expand All @@ -142,11 +147,15 @@ class EventDetailsViewModel(

compositeDisposable += eventService.getEventsByLocation(location)
.withDefaultSchedulers()
.subscribe({
val similarEventList = mutableListOf<Event>()
mutableSimilarEvents.value?.let { currentEvents -> similarEventList.addAll(currentEvents) }
val list = it.filter { it.id != eventId }
.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")
Expand Down Expand Up @@ -176,6 +185,7 @@ class EventDetailsViewModel(
}
compositeDisposable += eventService.getEvent(id)
.withDefaultSchedulers()
.distinctUntilChanged()
.doOnSubscribe {
mutableProgress.value = true
}.doFinally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ class EventService(
eventsFlowable
else
eventApi.getEvents()
.map {
eventDao.insertEvents(it)
eventTopicsDao.insertEventTopics(getEventTopicList(it))
}
.toFlowable()
.flatMap {
eventsFlowable
}
.map {
eventDao.insertEvents(it)
eventTopicsDao.insertEventTopics(getEventTopicList(it))
}
.toFlowable()
.flatMap {
eventsFlowable
}
}
}

Expand All @@ -58,9 +58,9 @@ class EventService(

private fun getEventTopicList(eventsList: List<Event>): List<EventTopic?> {
return eventsList
.filter { it.eventTopic != null }
.map { it.eventTopic }
.toList()
.filter { it.eventTopic != null }
.map { it.eventTopic }
.toList()
}

fun getEventTopics(): Flowable<List<EventTopic>> {
Expand All @@ -74,39 +74,40 @@ class EventService(
fun getEventFeedback(id: Long): Single<List<Feedback>> {
return eventFeedbackApi.getEventFeedback(id)
}

fun submitFeedback(feedback: Feedback): Single<Feedback> {
return eventFeedbackApi.postfeedback(feedback)
}
fun getSearchEvents(eventName: String, sortBy: String): Single<List<Event>> {
return eventApi.searchEvents(sortBy, eventName).flatMap { apiList ->
val eventIds = apiList.map { it.id }.toList()
eventDao.getFavoriteEventWithinIds(eventIds).flatMap { favIds ->
updateFavorites(apiList, favIds)
}

fun getSearchEvents(eventName: String, sortBy: String): Flowable<List<Event>> {
return eventApi.searchEvents(sortBy, eventName).flatMapPublisher { apiList ->
updateFavorites(apiList)
}
}

fun getFavoriteEvents(): Flowable<List<Event>> {
return eventDao.getFavoriteEvents()
}

fun getEventsByLocation(locationName: String?): Single<List<Event>> {
fun getEventsByLocation(locationName: String?): Flowable<List<Event>> {
val query = "[{\"name\":\"location-name\",\"op\":\"ilike\",\"val\":\"%$locationName%\"}," +
"{\"name\":\"ends-at\",\"op\":\"ge\",\"val\":\"%${EventUtils.getTimeInISO8601(Date())}%\"}]"
return eventApi.searchEvents("name", query).flatMap { apiList ->
val eventIds = apiList.map { it.id }.toList()
eventTopicsDao.insertEventTopics(getEventTopicList(apiList))
eventDao.getFavoriteEventWithinIds(eventIds).flatMap { favIds ->
updateFavorites(apiList, favIds)
}
return eventApi.searchEvents("name", query).flatMapPublisher { apiList ->
updateFavorites(apiList)
}
}

fun updateFavorites(apiEvents: List<Event>, favEventIds: List<Long>): Single<List<Event>> {
apiEvents.map { if (favEventIds.contains(it.id)) it.favorite = true }
eventDao.insertEvents(apiEvents)
val eventIds = apiEvents.map { it.id }.toList()
return eventDao.getEventWithIds(eventIds)
fun updateFavorites(apiList: List<Event>): Flowable<List<Event>> {

val ids = apiList.map { it.id }.toList()
eventTopicsDao.insertEventTopics(getEventTopicList(apiList))
return eventDao.getFavoriteEventWithinIds(ids)
.flatMapPublisher { favIds ->
apiList.map { if (favIds.contains(it.id)) it.favorite = true }
eventDao.insertEvents(apiList)
val eventIds = apiList.map { it.id }.toList()
eventDao.getEventWithIds(eventIds)
}
}

fun getEvent(id: Long): Flowable<Event> {
Expand All @@ -123,15 +124,14 @@ class EventService(
}
}

fun getEventsUnderUser(eventIds: List<Long>): Single<List<Event>> {
fun getEventsUnderUser(eventIds: List<Long>): Flowable<List<Event>> {
val query = buildQuery(eventIds)
return eventApi.eventsUnderUser(query)
.map {
.flatMapPublisher {
eventDao.insertEvents(it)
it
}.onErrorResumeNext {
eventDao.getEventWithIds(eventIds).map { it }
eventDao.getEventWithIds(eventIds)
}
.onErrorResumeNext(eventDao.getEventWithIds(eventIds))
}

fun setFavorite(eventId: Long, favorite: Boolean): Completable {
Expand All @@ -141,20 +141,10 @@ class EventService(
}

fun getSimilarEvents(id: Long): Flowable<List<Event>> {
val eventsFlowable = eventDao.getAllSimilarEvents(id)
return eventsFlowable.switchMap {
if (it.isNotEmpty())
eventsFlowable
else
eventTopicApi.getEventsUnderTopicId(id)
.toFlowable()
.map {
eventDao.insertEvents(it)
}
.flatMap {
eventsFlowable
}
}
return eventTopicApi.getEventsUnderTopicId(id)
.flatMapPublisher {
updateFavorites(it)
}
}

fun getSpeakerCall(id: Long): Single<SpeakersCall> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,17 @@ class EventsViewModel(
if (lastSearch != savedLocation.value) {
compositeDisposable += eventService.getEventsByLocation(mutableSavedLocation.value)
.withDefaultSchedulers()
.distinctUntilChanged()
.doOnSubscribe {
mutableShowShimmerEvents.value = true
}
.doFinally {
mutableProgress.value = false
mutableShowShimmerEvents.value = false
lastSearch = mutableSavedLocation.value ?: ""
stopLoaders()
}.subscribe({
stopLoaders()
mutableEvents.value = it
}, {
stopLoaders()
Timber.e(it, "Error fetching events")
mutableError.value = resource.getString(R.string.error_fetching_events_message)
})
Expand All @@ -62,6 +63,11 @@ class EventsViewModel(
}
}

private fun stopLoaders() {
mutableProgress.value = false
mutableShowShimmerEvents.value = false
lastSearch = mutableSavedLocation.value ?: ""
}
fun isConnected(): Boolean = mutableConnectionLiveData.value ?: false

fun clearEvents() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ class OrdersUnderUserViewModel(
private fun eventsUnderUser(eventIds: List<Long>, showExpired: Boolean) {
compositeDisposable += eventService.getEventsUnderUser(eventIds)
.withDefaultSchedulers()
.distinctUntilChanged()
.doFinally {
mutableShowShimmerResults.value = false
}.subscribe({
mutableShowShimmerResults.value = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WHY?

Copy link
Contributor Author

@iamanbansal iamanbansal May 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calls the doFInally action after this Flowable signals onError or onCompleted or gets canceled by the downstream.

Means it'll keep showing the shimmer even after the success.

val events = ArrayList<Event>()
it.map {
val times = eventIdAndTimes[it.id]
Expand All @@ -93,6 +95,7 @@ class OrdersUnderUserViewModel(
if (finalList.isEmpty()) mutableNoTickets.value = true
mutableEventAndOrder.value = finalList
}, {
mutableShowShimmerResults.value = false
mutableMessage.value = resource.getString(R.string.list_events_fail_message)
Timber.d(it, "Failed to list events under a user ")
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,20 +313,27 @@ class SearchViewModel(
}
compositeDisposable += eventService.getSearchEvents(query, sortBy)
.withDefaultSchedulers()
.distinctUntilChanged()
.doOnSubscribe {
mutableShowShimmerResults.value = true
mutableChipClickable.value = false
}.doFinally {
mutableShowShimmerResults.value = false
mutableChipClickable.value = true
stopLoaders()
}.subscribe({
stopLoaders()
mutableEvents.value = it
}, {
stopLoaders()
Timber.e(it, "Error fetching events")
mutableError.value = resource.getString(R.string.error_fetching_events_message)
})
}

private fun stopLoaders() {
mutableShowShimmerResults.value = false
mutableChipClickable.value = true
}

fun setFavorite(eventId: Long, favorite: Boolean) {
compositeDisposable += eventService.setFavorite(eventId, favorite)
.withDefaultSchedulers()
Expand Down