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

Wear app: Add a refresh button #257

Merged
merged 1 commit into from
Apr 11, 2024
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 @@ -150,6 +150,7 @@ fun MainScreen(
user = user,
onSignInClick = onSignInClick,
onSignOutClick = onSignOutClick,
onRefreshClick = viewModel::refresh
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ import fr.androidmakers.domain.repo.UserRepository
import fr.paug.androidmakers.wear.applicationContext
import fr.paug.androidmakers.wear.data.LocalPreferencesRepository
import fr.paug.androidmakers.wear.ui.session.UISession
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
Expand All @@ -40,21 +44,24 @@ private val DAY_1_DATE = LocalDate(year = 2024, month = Month.APRIL, dayOfMonth
private val DAY_2_DATE = DAY_1_DATE.plus(1, DateTimeUnit.DAY)

class MainViewModel(
application: Application,
private val userRepository: UserRepository,
localPreferencesRepository: LocalPreferencesRepository,
getAgendaUseCase: GetAgendaUseCase,
private val syncBookmarksUseCase: SyncBookmarksUseCase,
getFavoriteSessionsUseCase: GetFavoriteSessionsUseCase,
application: Application,
private val userRepository: UserRepository,
localPreferencesRepository: LocalPreferencesRepository,
getAgendaUseCase: GetAgendaUseCase,
private val syncBookmarksUseCase: SyncBookmarksUseCase,
getFavoriteSessionsUseCase: GetFavoriteSessionsUseCase,
) : AndroidViewModel(application) {
private val _user = MutableStateFlow<User?>(null)

val user: StateFlow<User?> = _user

private val refreshSignal = Channel<Unit>()

init {
viewModelScope.launch {
_user.emit(userRepository.getUser())
maybeSyncBookmarks()
refreshSignal.send(Unit)
}
}

Expand All @@ -67,24 +74,28 @@ class MainViewModel(
}
}

private val sessions: Flow<List<UISession>?> = getAgendaUseCase()
.filter { it.isSuccess }
.map { it.getOrThrow() }
.combine(getFavoriteSessionsUseCase()) { agenda, favoriteSessions ->
agenda.toUISessions(favoriteSessions)
}
.combine(localPreferencesRepository.showOnlyBookmarkedSessions) { sessions, showOnlyBookmarked ->
if (showOnlyBookmarked) {
sessions.filter { it.isBookmarked }
} else {
sessions
}
@OptIn(ExperimentalCoroutinesApi::class)
private val sessions: Flow<List<UISession>?> = refreshSignal.consumeAsFlow()
.flatMapLatest { getAgendaUseCase() }
.filter { it.isSuccess }
.map { it.getOrThrow() }
.combine(getFavoriteSessionsUseCase()) { agenda, favoriteSessions ->
agenda.toUISessions(favoriteSessions)
}
.combine(localPreferencesRepository.showOnlyBookmarkedSessions) { sessions, showOnlyBookmarked ->
if (showOnlyBookmarked) {
sessions.filter { it.isBookmarked }
} else {
sessions
}
.stateIn(viewModelScope, SharingStarted.Lazily, null)
}
.stateIn(viewModelScope, SharingStarted.Lazily, null)

val sessionsDay1 = sessions.map { sessions -> sessions?.filter { it.session.startsAt.date == DAY_1_DATE } }
val sessionsDay1 =
sessions.map { sessions -> sessions?.filter { it.session.startsAt.date == DAY_1_DATE } }

val sessionsDay2 = sessions.map { sessions -> sessions?.filter { it.session.startsAt.date == DAY_2_DATE } }
val sessionsDay2 =
sessions.map { sessions -> sessions?.filter { it.session.startsAt.date == DAY_2_DATE } }

/**
* Get the day of the conference, based on the current date.
Expand All @@ -107,33 +118,40 @@ class MainViewModel(

fun signOut() {
val googleSignInClient = GoogleSignIn.getClient(
applicationContext,
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken("985196411897-r7edbi9jgo3hfupekcmdrg66inonj0o5.apps.googleusercontent.com")
.build()
applicationContext,
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken("985196411897-r7edbi9jgo3hfupekcmdrg66inonj0o5.apps.googleusercontent.com")
.build()
)
googleSignInClient.signOut()
googleSignInClient.revokeAccess()
Firebase.auth.signOut()
_user.value = null
}

fun refresh() {
viewModelScope.launch {
maybeSyncBookmarks()
refreshSignal.send(Unit)
}
}
}

private fun Agenda.toUISessions(favoriteSessions: Set<String>): List<UISession> {
return sessions.values.mapNotNull { session ->
UISession(
session = session,
speakers = session.speakers.map {
speakers[it] ?: run {
Log.d(TAG, "Speaker $it not found")
return@mapNotNull null
}
},
room = rooms[session.roomId] ?: run {
Log.d(TAG, "Room ${session.roomId} not found")
session = session,
speakers = session.speakers.map {
speakers[it] ?: run {
Log.d(TAG, "Speaker $it not found")
return@mapNotNull null
},
isBookmarked = session.id in favoriteSessions,
}
},
room = rooms[session.roomId] ?: run {
Log.d(TAG, "Room ${session.roomId} not found")
return@mapNotNull null
},
isBookmarked = session.id in favoriteSessions,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ fun SettingsScreen(
user: User?,
onSignInClick: () -> Unit,
onSignOutClick: () -> Unit,
onRefreshClick: () -> Unit,
) {
var showSignOutConfirmDialog by remember { mutableStateOf(false) }
val columnState = rememberResponsiveColumnState(
Expand All @@ -48,6 +49,14 @@ fun SettingsScreen(
columnState = columnState,
modifier = Modifier.fillMaxSize()
) {
item {
Chip(
modifier = Modifier.fillMaxWidth(),
label = { Text(stringResource(R.string.settings_refresh)) },
onClick = onRefreshClick
)
}

item {
if (user == null) {
Chip(
Expand Down Expand Up @@ -133,5 +142,6 @@ private fun SettingsScreenPreview() {
user = null,
onSignInClick = {},
onSignOutClick = {},
onRefreshClick = {},
)
}
1 change: 1 addition & 0 deletions wearApp/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<string name="settings_signOut">Déconnexion</string>
<string name="settings_showBookmarksOnly">Sessions favorites seulement</string>
<string name="settings_signOutConfirm">Se déconnecter ?</string>
<string name="settings_refresh">Rafraîchir</string>
</resources>
1 change: 1 addition & 0 deletions wearApp/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
<string name="settings_signOut">Sign out</string>
<string name="settings_showBookmarksOnly">Show favorites only</string>
<string name="settings_signOutConfirm">Sign out?</string>
<string name="settings_refresh">Refresh</string>
</resources>
Loading