-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from makeevrserg/develop
wear integration
- Loading branch information
Showing
102 changed files
with
2,446 additions
and
406 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 68 additions & 4 deletions
72
androidApp/src/main/java/com/makeevrserg/empireprojekt/mobile/application/App.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,86 @@ | ||
package com.makeevrserg.empireprojekt.mobile.application | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import androidx.work.ExistingPeriodicWorkPolicy | ||
import androidx.work.PeriodicWorkRequest | ||
import androidx.work.WorkManager | ||
import com.google.android.horologist.annotations.ExperimentalHorologistApi | ||
import com.google.android.horologist.data.WearDataLayerRegistry | ||
import com.google.firebase.ktx.Firebase | ||
import com.google.firebase.ktx.initialize | ||
import com.makeevrserg.empireprojekt.mobile.features.root.di.RootModule | ||
import com.makeevrserg.empireprojekt.mobile.features.root.di.impl.RootModuleImpl | ||
import com.makeevrserg.empireprojekt.mobile.wear.messenger.di.WearMessengerModule | ||
import com.makeevrserg.empireprojekt.mobile.work.CheckStatusWork | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.isActive | ||
import kotlinx.coroutines.launch | ||
import ru.astrainteractive.klibs.kdi.getValue | ||
import ru.astrainteractive.klibs.mikro.platform.DefaultAndroidPlatformConfiguration | ||
import java.util.concurrent.TimeUnit | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
@OptIn(ExperimentalHorologistApi::class) | ||
class App : Application() { | ||
private val servicesModule by RootModule.servicesModule | ||
val rootModule by lazy { | ||
RootModuleImpl() | ||
} | ||
val wearMessengerModule by lazy { | ||
WearMessengerModule.Default( | ||
context = rootModule.servicesModule.platformConfiguration.value.applicationContext, | ||
coroutineScope = rootModule.servicesModule.mainScope.value, | ||
json = rootModule.servicesModule.jsonConfiguration.value | ||
) | ||
} | ||
private val wearDataLayerRegistry by lazy { | ||
WearDataLayerRegistry.fromContext( | ||
application = applicationContext, | ||
coroutineScope = rootModule.servicesModule.mainScope.value | ||
) | ||
} | ||
private val messageClient by lazy { | ||
wearDataLayerRegistry.messageClient | ||
} | ||
|
||
override fun onTerminate() { | ||
super.onTerminate() | ||
rootModule.servicesModule.mainScope.value.cancel() | ||
} | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
Firebase.initialize(this) | ||
servicesModule.platformConfiguration.initialize( | ||
rootModule.servicesModule.platformConfiguration.initialize { | ||
DefaultAndroidPlatformConfiguration( | ||
applicationContext | ||
) | ||
) | ||
} | ||
scheduleWork() | ||
} | ||
|
||
private fun scheduleWork() { | ||
val statusWork = PeriodicWorkRequest.Builder( | ||
CheckStatusWork::class.java, | ||
15, | ||
TimeUnit.MINUTES | ||
).build() | ||
val instanceWorkManager = WorkManager.getInstance(applicationContext) | ||
|
||
rootModule.servicesModule.mainScope.value.launch { | ||
while (isActive) { | ||
instanceWorkManager.enqueueUniquePeriodicWork( | ||
CheckStatusWork::class.java.simpleName, | ||
ExistingPeriodicWorkPolicy.CANCEL_AND_REENQUEUE, | ||
statusWork | ||
) | ||
delay(30.seconds) | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
fun Application.asEmpireApp(): App = (this as App) | ||
fun Context.asEmpireApp(): App = (applicationContext as App) | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
androidApp/src/main/java/com/makeevrserg/empireprojekt/mobile/work/CheckStatusWork.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.makeevrserg.empireprojekt.mobile.work | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import androidx.work.CoroutineWorker | ||
import androidx.work.WorkerParameters | ||
import com.makeevrserg.empireprojekt.mobile.application.App.Companion.asEmpireApp | ||
import com.makeevrserg.empireprojekt.mobile.features.status.StatusComponent | ||
import com.makeevrserg.empireprojekt.mobile.wear.messenger.api.app.model.StatusModel | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.awaitAll | ||
import kotlinx.coroutines.coroutineScope | ||
import ru.astrainteractive.klibs.kdi.Provider | ||
import ru.astrainteractive.klibs.kdi.getValue | ||
|
||
class CheckStatusWork( | ||
context: Context, | ||
params: WorkerParameters | ||
) : CoroutineWorker(context, params) { | ||
private val wearMessengerModule by lazy { | ||
applicationContext.asEmpireApp().wearMessengerModule | ||
} | ||
private val rootModule by lazy { | ||
applicationContext.asEmpireApp().rootModule | ||
} | ||
private val rootStatusComponent by Provider { | ||
rootModule.rootStatusComponent.value | ||
} | ||
|
||
override suspend fun doWork(): Result { | ||
Log.d(TAG, "doWork: ") | ||
sendStatus() | ||
return Result.success() | ||
} | ||
|
||
private suspend fun sendStatus() = coroutineScope { | ||
val messages = rootStatusComponent.statusComponents.map { | ||
async { | ||
it.checkStatus() | ||
val model = it.model.value | ||
StatusModel( | ||
title = model.title.toString(applicationContext), | ||
isLoading = model.isLoading, | ||
status = when (model.status) { | ||
StatusComponent.Model.LoadingStatus.LOADING -> StatusModel.LoadingStatus.LOADING | ||
StatusComponent.Model.LoadingStatus.SUCCESS -> StatusModel.LoadingStatus.SUCCESS | ||
StatusComponent.Model.LoadingStatus.ERROR -> StatusModel.LoadingStatus.ERROR | ||
} | ||
) | ||
} | ||
}.awaitAll() | ||
wearMessengerModule.wearMessageProducer.produce( | ||
message = wearMessengerModule.statusModelMessage, | ||
value = messages | ||
) | ||
} | ||
|
||
companion object { | ||
private const val TAG = "CheckStatusWork" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
-6.63 KB
(95%)
iosApp/iosApp.xcworkspace/xcuserdata/romanmakeev.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.