Glance Tile for People in Space

This commit is contained in:
Yuri Schimke 2021-11-06 10:32:57 +00:00
parent 547bcb68a4
commit 19294f4b30
4 changed files with 101 additions and 1 deletions

View file

@ -8,7 +8,7 @@ android {
defaultConfig {
applicationId = "com.surrus.peopleinspace"
minSdk = 25
minSdk = 26
targetSdk = Versions.androidTargetSdk
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
@ -84,5 +84,7 @@ dependencies {
debugImplementation(composeUiTestManifest)
}
implementation("androidx.glance:glance-wear:1.0.0-SNAPSHOT")
implementation(project(":common"))
}

View file

@ -45,6 +45,21 @@
android:scheme="peopleinspace" />
</intent-filter>
</activity>
<service
android:name=".tile.PeopleInSpaceTile"
android:exported="true"
android:icon="@drawable/ic_iss"
android:label="@string/app_name"
android:permission="com.google.android.wearable.permission.BIND_TILE_PROVIDER">
<intent-filter>
<action android:name="androidx.wear.tiles.action.BIND_TILE_PROVIDER" />
</intent-filter>
<meta-data
android:name="androidx.wear.tiles.PREVIEW"
android:resource="@drawable/ic_american_astronaut" />
</service>
</application>
</manifest>

View file

@ -0,0 +1,57 @@
package com.surrus.peopleinspace.tile
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.ExperimentalUnitApi
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.dp
import androidx.glance.GlanceModifier
import androidx.glance.layout.Column
import androidx.glance.layout.Text
import androidx.glance.layout.padding
import androidx.glance.text.FontWeight
import androidx.glance.text.TextStyle
import androidx.glance.unit.ColorProvider
import com.surrus.common.remote.Assignment
import com.surrus.common.repository.PeopleInSpaceRepositoryInterface
import com.surrus.peopleinspace.tile.util.BaseGlanceTileService
import kotlinx.coroutines.flow.first
import org.koin.android.ext.android.inject
class PeopleInSpaceTile : BaseGlanceTileService<PeopleInSpaceTile.Data>() {
val repository: PeopleInSpaceRepositoryInterface by inject()
data class Data(val people: List<Assignment>)
override suspend fun loadData(): Data {
return Data(repository.fetchPeopleAsFlow().first())
}
@OptIn(ExperimentalUnitApi::class)
@Composable
override fun Content(data: Data) {
Column(
modifier = GlanceModifier.padding(horizontal = 8.dp)
) {
Text(
modifier = GlanceModifier.padding(bottom = 8.dp),
text = "People in Space",
style = TextStyle(
color = ColorProvider(Color.White),
fontSize = TextUnit(12f, TextUnitType.Sp),
fontWeight = FontWeight.Bold
)
)
data.people.forEach {
Text(
text = it.name,
style = TextStyle(
color = ColorProvider(Color.White),
fontSize = TextUnit(10f, TextUnitType.Sp)
)
)
}
}
}
}

View file

@ -0,0 +1,26 @@
package com.surrus.peopleinspace.tile.util
import androidx.compose.runtime.Composable
import androidx.glance.wear.GlanceTileService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
abstract class BaseGlanceTileService<T> : GlanceTileService() {
@Composable
override fun Content() {
// Terrible hack for lack of suspend load function
val data = runBlocking(Dispatchers.IO) {
try {
loadData()
} finally {
}
}
Content(data = data)
}
abstract suspend fun loadData(): T
@Composable
abstract fun Content(data: T)
}