Skip to content
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

fix filters rules #118

Merged
merged 2 commits into from
Apr 22, 2022
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 @@ -19,8 +19,8 @@ import com.google.accompanist.pager.rememberPagerState
import fr.paug.androidmakers.ui.adapter.DaySchedule
import fr.paug.androidmakers.ui.adapter.ScheduleSession
import fr.paug.androidmakers.ui.model.UISession
import fr.paug.androidmakers.util.SessionFilter
import fr.paug.androidmakers.util.BookmarksStore
import fr.paug.androidmakers.util.SessionFilter
import fr.paug.androidmakers.util.TimeUtils
import kotlinx.coroutines.launch
import java.time.OffsetDateTime
Expand Down Expand Up @@ -87,11 +87,14 @@ fun AgendaPager(
},
)
}

AgendaColumn(
sessionsPerStartTime = addSeparators(LocalContext.current, items),
onSessionClicked = onSessionClicked
)
if (items.isEmpty()) {
EmptyLayout()
martinbonnin marked this conversation as resolved.
Show resolved Hide resolved
} else {
AgendaColumn(
sessionsPerStartTime = addSeparators(LocalContext.current, items),
onSessionClicked = onSessionClicked
)
}
}
}
}
Expand All @@ -108,37 +111,50 @@ private fun addSeparators(context: Context, sessions: List<UISession>): Map<Stri
}
}


// algorithm to filter sessions by applying filters, if the filters is same category we keep
// the combined logic (OR) otherwise it's AND with category filters
// example: Language English && (Rooms Moebius || Rooms A...)
// the algorithm is inspired by Inverted index
// time complexity is O(n * m) where n is the number of sessions and m is the number of filters
private fun List<ScheduleSession>.filterSessions(
filterList: List<SessionFilter>
): List<ScheduleSession> {
val filteredSessions = mutableListOf<ScheduleSession>()

if (filterList.isEmpty()) {
filteredSessions.addAll(this)
} else {
for (item in this) {
for (sessionFilter in filterList) {
val matched = when (sessionFilter.type) {
SessionFilter.FilterType.BOOKMARK -> {
BookmarksStore.isBookmarked(item.sessionId)
}
SessionFilter.FilterType.LANGUAGE -> {
sessionFilter.value == item.language
}
SessionFilter.FilterType.ROOM -> {
sessionFilter.value == item.roomId
}
): Set<ScheduleSession> {
val filteredSessions = hashSetOf<ScheduleSession>()
if (filterList.isEmpty()) {
filteredSessions.addAll(this)
return filteredSessions
}
val sessionsByFilterType = mutableMapOf<SessionFilter.FilterType, MutableList<ScheduleSession>>()
for (filter in filterList) {
if (!sessionsByFilterType.containsKey(filter.type)) {
sessionsByFilterType[filter.type] = mutableListOf()
}

if (matched) {
filteredSessions.add(item)
}
for (session in this) {
for (filter in filterList) {
when (filter.type) {
SessionFilter.FilterType.BOOKMARK -> {
if (BookmarksStore.isBookmarked(session.sessionId)) {
sessionsByFilterType[filter.type]?.add(session)
}
}
SessionFilter.FilterType.LANGUAGE -> {
if (filter.value == session.language) {
sessionsByFilterType[filter.type]?.add(session)
}
}
SessionFilter.FilterType.ROOM -> {
if (filter.value == session.roomId) {
sessionsByFilterType[filter.type]?.add(session)
}
}
}
}
}
}
}

return filteredSessions
//get union join of all ScheduleSessions
val origin = sessionsByFilterType.values.flatten().toMutableSet()
sessionsByFilterType.values.forEach { origin.retainAll(it) }
return origin
}

private fun getRoomTitle(scheduleSession: ScheduleSession, daySchedule: DaySchedule): String {
Expand Down
18 changes: 16 additions & 2 deletions app/src/main/java/fr/paug/androidmakers/ui/components/LceLayout.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fr.paug.androidmakers.ui.components

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Button
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.Text
Expand All @@ -14,7 +13,6 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.google.accompanist.swiperefresh.SwipeRefresh
import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
import com.google.android.datatransport.runtime.retries.Retries.retry
import fr.paug.androidmakers.R
import fr.paug.androidmakers.ui.viewmodel.Lce
import fr.paug.androidmakers.ui.viewmodel.LceViewModel
Expand Down Expand Up @@ -111,4 +109,20 @@ fun ErrorLayout(
}
}
}
}

@Composable
fun EmptyLayout(modifier: Modifier = Modifier) {
Box(
modifier = modifier
.fillMaxWidth()
.fillMaxHeight()
.padding(16.dp),
contentAlignment = Alignment.Center
) {
Text(
text = stringResource(id = R.string.empty_events),
textAlign = TextAlign.Center
)
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<string name="language">Langue</string>
<string name="room">Salle</string>
<string name="rooms">Salles</string>
<string name="empty_events">Désolé, aucune conférence n\'est disponible pour les filtres actuellement sélectionnés.</string>

<!--Venue-->
<string name="locate_on_map">Localiser sur la carte</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<string name="language">Language</string>
<string name="room">Room</string>
<string name="rooms">Rooms</string>
<string name="empty_events">Sorry, there is nothing conference available for currently selected filters.</string>

<!--Venue-->
<string name="locate_on_map">Locate on map</string>
Expand Down