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

Send a signal to the watch to sync bookmarks #296

Merged
merged 1 commit into from
Apr 21, 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
5 changes: 5 additions & 0 deletions shared/data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ kotlin {
api(libs.androidx.datastore.preferences)

api(libs.firebase.auth)
}
}

androidMain {
dependencies {
implementation(libs.play.services.wearable)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fr.androidmakers.store.wear

import android.content.Context
import android.util.Log
import com.google.android.gms.wearable.Wearable
import kotlinx.coroutines.tasks.await

class WearMessagingImpl(context: Context) : WearMessaging {
companion object {
const val MESSAGE_SYNC_BOOKMARKS = "syncBookmarks"
}

private val nodeClient = Wearable.getNodeClient(context)
private val messageClient = Wearable.getMessageClient(context)

override suspend fun sendSyncBookmarksMessage() {
try {
val allNodeIds = nodeClient.connectedNodes.await().map { it.id }
for (nodeId in allNodeIds) {
messageClient.sendMessage(nodeId, MESSAGE_SYNC_BOOKMARKS, null).await()
}
} catch (t: Throwable) {
Log.w("WearMessagingImpl", "Error sending message", t)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package fr.androidmakers.store.wear

interface WearMessaging {
suspend fun sendSyncBookmarksMessage()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package fr.androidmakers.store.wear

import fr.androidmakers.domain.repo.MessagingRepository

class WearMessagingRepository(private val wearMessaging: WearMessaging) : MessagingRepository {
override suspend fun sendSyncBookmarksMessage() {
wearMessaging.sendSyncBookmarksMessage()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fr.androidmakers.store.wear

class WearMessagingImpl : WearMessaging {
override suspend fun sendSyncBookmarksMessage() {
// No-op on iOS
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import androidx.datastore.preferences.core.Preferences
import com.apollographql.apollo3.cache.normalized.sql.SqlNormalizedCacheFactory
import fr.androidmakers.store.graphql.ApolloClient
import fr.androidmakers.store.local.createDataStore
import fr.androidmakers.store.wear.WearMessaging
import fr.androidmakers.store.wear.WearMessagingImpl
import org.koin.android.ext.koin.androidContext
import org.koin.dsl.module

Expand All @@ -21,4 +23,8 @@ actual val dataPlatformModule = module {
androidContext().filesDir.resolve("bookmarks.preferences_pb").absolutePath
}
}

single<WearMessaging> {
WearMessagingImpl(get())
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.androidmakers.di

import fr.androidmakers.domain.repo.BookmarksRepository
import fr.androidmakers.domain.repo.MessagingRepository
import fr.androidmakers.domain.repo.PartnersRepository
import fr.androidmakers.domain.repo.RoomsRepository
import fr.androidmakers.domain.repo.SessionsRepository
Expand All @@ -14,6 +15,7 @@ import fr.androidmakers.store.graphql.SessionsGraphQLRepository
import fr.androidmakers.store.graphql.SpeakersGraphQLRepository
import fr.androidmakers.store.graphql.VenueGraphQLRepository
import fr.androidmakers.store.local.BookmarksDataStoreRepository
import fr.androidmakers.store.wear.WearMessagingRepository
import org.koin.core.module.Module
import org.koin.dsl.module

Expand All @@ -29,4 +31,5 @@ val dataModule = module {
single<VenueRepository> { VenueGraphQLRepository(get()) }

single<BookmarksRepository> { BookmarksDataStoreRepository(get()) }
single<MessagingRepository> { WearMessagingRepository(get()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import fr.androidmakers.domain.interactor.GetAgendaUseCase
import fr.androidmakers.domain.interactor.GetConferenceVenueUseCase
import fr.androidmakers.domain.interactor.GetFavoriteSessionsUseCase
import fr.androidmakers.domain.interactor.GetPartnersUseCase
import fr.androidmakers.domain.interactor.MergeBookmarksUseCase
import fr.androidmakers.domain.interactor.OpenCocUseCase
import fr.androidmakers.domain.interactor.OpenFaqUseCase
import fr.androidmakers.domain.interactor.OpenLinkUseCase
Expand All @@ -14,7 +15,6 @@ import fr.androidmakers.domain.interactor.OpenXAccountUseCase
import fr.androidmakers.domain.interactor.OpenXHashtagUseCase
import fr.androidmakers.domain.interactor.OpenYoutubeUseCase
import fr.androidmakers.domain.interactor.SetSessionBookmarkUseCase
import fr.androidmakers.domain.interactor.MergeBookmarksUseCase
import org.koin.core.module.Module
import org.koin.dsl.module

Expand All @@ -30,7 +30,7 @@ val domainModule = module {
factory { OpenYoutubeUseCase(get()) }
factory { OpenXHashtagUseCase(get()) }
factory { OpenXAccountUseCase(get()) }
factory { SetSessionBookmarkUseCase(get(), get(), get()) }
factory { SetSessionBookmarkUseCase(get(), get(), get(), get()) }
factory { GetPartnersUseCase(get()) }
factory { GetFavoriteSessionsUseCase(get()) }
factory { OpenPartnerLinkUseCase(get()) }
Expand Down
16 changes: 11 additions & 5 deletions shared/di/src/iosMain/kotlin/fr/androidmakers/di/DataModule.ios.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import androidx.datastore.preferences.core.Preferences
import com.apollographql.apollo3.cache.normalized.sql.SqlNormalizedCacheFactory
import fr.androidmakers.store.graphql.ApolloClient
import fr.androidmakers.store.local.createDataStore
import fr.androidmakers.store.wear.WearMessaging
import fr.androidmakers.store.wear.WearMessagingImpl
import kotlinx.cinterop.ExperimentalForeignApi
import org.koin.dsl.module
import platform.Foundation.NSDocumentDirectory
Expand All @@ -24,13 +26,17 @@ actual val dataPlatformModule = module {
single<DataStore<Preferences>> {
createDataStore {
val documentDirectory: NSURL? = NSFileManager.defaultManager.URLForDirectory(
directory = NSDocumentDirectory,
inDomain = NSUserDomainMask,
appropriateForURL = null,
create = false,
error = null,
directory = NSDocumentDirectory,
inDomain = NSUserDomainMask,
appropriateForURL = null,
create = false,
error = null,
)
requireNotNull(documentDirectory).path + "/bookmarks.preferences_pb"
}
}

single<WearMessaging> {
WearMessagingImpl()
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package fr.androidmakers.domain.interactor

import fr.androidmakers.domain.repo.BookmarksRepository
import fr.androidmakers.domain.repo.MessagingRepository
import fr.androidmakers.domain.repo.SessionsRepository
import fr.androidmakers.domain.repo.UserRepository

class SetSessionBookmarkUseCase(
private val userRepository: UserRepository,
private val sessionsRepository: SessionsRepository,
private val bookmarksRepository: BookmarksRepository
private val userRepository: UserRepository,
private val sessionsRepository: SessionsRepository,
private val bookmarksRepository: BookmarksRepository,
private val messagingRepository: MessagingRepository,
) {
suspend operator fun invoke(sessionId: String, isBookmark: Boolean) {
bookmarksRepository.setBookmarked(sessionId, isBookmark)
userRepository.getUser()?.id?.let { token ->
sessionsRepository.setBookmark(token, sessionId, isBookmark)
}
messagingRepository.sendSyncBookmarksMessage()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package fr.androidmakers.domain.repo

interface MessagingRepository {
suspend fun sendSyncBookmarksMessage()
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.wearable.Wearable
import dev.gitlive.firebase.Firebase
import dev.gitlive.firebase.auth.auth
import fr.androidmakers.domain.interactor.GetAgendaUseCase
import fr.androidmakers.domain.interactor.GetFavoriteSessionsUseCase
import fr.androidmakers.domain.interactor.MergeBookmarksUseCase
import fr.androidmakers.domain.model.Agenda
import fr.androidmakers.domain.model.User
import fr.androidmakers.domain.repo.BookmarksRepository
Expand Down Expand Up @@ -69,6 +69,14 @@ class MainViewModel(
maybeSyncBookmarks()
refreshSignal.send(Unit)
}

val messageClient = Wearable.getMessageClient(application)
messageClient.addListener {
Log.d(TAG, "Received syncBookmarks message")
viewModelScope.launch {
maybeSyncBookmarks()
}
}
}

private suspend fun maybeSyncBookmarks() {
Expand Down
Loading