Skip to content

chore: Move flask filter logic from ViewModels to Services #1345

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

Closed
wants to merge 3 commits into from
Closed
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 @@ -27,19 +27,21 @@ class AttendeeService(
return attendeeApi.deleteAttendee(id)
}

fun getCustomFormsForAttendees(id: Long, filter: String): Single<List<CustomForm>> {
fun getCustomFormsForAttendees(id: Long): Single<List<CustomForm>> {
val formsSingle = attendeeDao.getCustomFormsForId(id)
return formsSingle.flatMap {
if (it.isNotEmpty())
formsSingle
else
else {
val filter = "[{\"name\":\"form\",\"op\":\"eq\",\"val\":\"order\"}]"
attendeeApi.getCustomFormsForAttendees(id, filter)
.map {
attendeeDao.insertCustomForms(it)
}
.flatMap {
formsSingle
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,7 @@ class AttendeeViewModel(
}

fun getCustomFormsForAttendees(eventId: Long) {
val filter = "[{\"name\":\"form\",\"op\":\"eq\",\"val\":\"order\"}]"
compositeDisposable.add(attendeeService.getCustomFormsForAttendees(eventId, filter)
compositeDisposable.add(attendeeService.getCustomFormsForAttendees(eventId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ class EventService(
}

fun getEventsByLocation(locationName: String): Single<List<Event>> {
return eventApi.searchEvents("name", locationName).flatMap { apiList ->
val query = "[{\"name\":\"location-name\",\"op\":\"ilike\",\"val\":\"%$locationName%\"}]"
return eventApi.searchEvents("name", query).flatMap { apiList ->
val eventIds = apiList.map { it.id }.toList()
eventTopicsDao.insertEventTopics(getEventTopicList(apiList))
eventDao.getFavoriteEventWithinIds(eventIds).flatMap { favIds ->
Expand All @@ -82,8 +83,9 @@ class EventService(
return eventApi.getEventFromApi(id)
}

fun getEventsUnderUser(eventId: String): Single<List<Event>> {
return eventApi.eventsUnderUser(eventId)
fun getEventsUnderUser(eventIds: List<Long>): Single<List<Event>> {
val query = buildQuery(eventIds)
return eventApi.eventsUnderUser(query)
}

fun setFavorite(eventId: Long, favorite: Boolean): Completable {
Expand All @@ -108,4 +110,33 @@ class EventService(
}
}
}

private fun buildQuery(eventIds: List<Long>): String {
var subQuery = ""

var eventId = -1L
val idList = ArrayList<Long>()
val eventIdAndTimes = mutableMapOf<Long, Int>()
eventIds.forEach { id ->
val times = eventIdAndTimes[id]
if (eventIdAndTimes.containsKey(id) && times != null) {
eventIdAndTimes[id] = times + 1
} else {
eventIdAndTimes[id] = 1
}
idList.add(id)
eventId = id
subQuery += ",{\"name\":\"id\",\"op\":\"eq\",\"val\":\"$eventId\"}"
}

val formattedSubQuery = if (subQuery != "")
subQuery.substring(1) // remove "," from the beginning
else
"" // if there are no orders

return if (idList.size == 1)
"[{\"name\":\"id\",\"op\":\"eq\",\"val\":\"$eventId\"}]"
else
"[{\"or\":[$formattedSubQuery]}]"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ class EventsViewModel(private val eventService: EventService, private val prefer
}

fun loadLocationEvents(loadingTag: Int) {
val query = "[{\"name\":\"location-name\",\"op\":\"ilike\",\"val\":\"%$savedLocation%\"}]"

if (loadingTag == RELOADING_EVENTS || (loadingTag == INITIAL_FETCHING_EVENTS &&
lastSearch != savedLocation)) {
compositeDisposable.add(eventService.getEventsByLocation(query)
compositeDisposable.add(eventService.getEventsByLocation(savedLocation.toString())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class OrdersUnderUserViewModel(
val attendeesNumber: LiveData<List<Int>> = mutableAttendeesNumber
private var eventIdMap = mutableMapOf<Long, Event>()
private val eventIdAndTimes = mutableMapOf<Long, Int>()
private var eventId: Long = -1
private val idList = ArrayList<Long>()
private val mutableMessage = SingleLiveEvent<String>()
val message: LiveData<String> = mutableMessage
private val mutableEventAndOrderIdentifier = MutableLiveData<List<Pair<Event, String>>>()
Expand All @@ -50,11 +48,11 @@ class OrdersUnderUserViewModel(
}.subscribe({
order = it
mutableAttendeesNumber.value = it.map { it.attendees.size }
val query = buildQuery(it)

if (idList.size != 0)
eventsUnderUser(query)
else {
val eventIds = it.mapNotNull { order -> order.event?.id }
if (eventIds.isNotEmpty()) {
eventsUnderUser(eventIds)
} else {
mutableProgress.value = false
mutableNoTickets.value = true
}
Expand All @@ -67,7 +65,7 @@ class OrdersUnderUserViewModel(
)
}

private fun eventsUnderUser(eventIds: String) {
private fun eventsUnderUser(eventIds: List<Long>) {
compositeDisposable.add(eventService.getEventsUnderUser(eventIds)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
Expand Down Expand Up @@ -98,35 +96,6 @@ class OrdersUnderUserViewModel(
)
}

private fun buildQuery(orderList: List<Order>): String {
var subQuery = ""

eventIdAndTimes.clear()
orderList.forEach {
it.event?.id?.let { it1 ->
val times = eventIdAndTimes[it1]
if (eventIdAndTimes.containsKey(it1) && times != null) {
eventIdAndTimes[it1] = times + 1
} else {
eventIdAndTimes[it1] = 1
}
idList.add(it1)
eventId = it1
subQuery += ",{\"name\":\"id\",\"op\":\"eq\",\"val\":\"$eventId\"}"
}
}

val formattedSubQuery = if (subQuery != "")
subQuery.substring(1) // remove "," from the beginning
else
"" // if there are no orders

return if (idList.size == 1)
"[{\"name\":\"id\",\"op\":\"eq\",\"val\":\"$eventId\"}]"
else
"[{\"or\":[$formattedSubQuery]}]"
}

override fun onCleared() {
super.onCleared()
compositeDisposable.clear()
Expand Down