Merge pull request #91 from yschimke/tile

Tile for People in Space
This commit is contained in:
John O'Reilly 2021-11-06 12:22:31 +00:00 committed by GitHub
commit 07bfe06bf7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 101 additions and 13 deletions

View file

@ -54,16 +54,10 @@ class ISSMapWidget : BaseGlanceAppWidget<ISSMapWidget.Data>() {
val bitmap = withContext(Dispatchers.Main) {
suspendCoroutine<Bitmap> { cont ->
println("mapSnapshot suspendCoroutine")
// val cache = TileWriter()
val mapSnapshot = MapSnapshot(
{
println("mapSnapshot status " + it.status)
if (it.status == MapSnapshot.Status.CANVAS_OK) {
val bitmap = Bitmap.createBitmap(it.bitmap)
println("mapSnapshot returning " + bitmap.width)
cont.resume(bitmap)
}
},
@ -74,14 +68,11 @@ class ISSMapWidget : BaseGlanceAppWidget<ISSMapWidget.Data>() {
)
launch(Dispatchers.IO) {
println("mapSnapshot launch")
mapSnapshot.run()
}
}
}
println("mapSnapshot returning data " + bitmap.width)
return Data(bitmap)
}

View file

@ -35,8 +35,6 @@ abstract class BaseGlanceAppWidget<T>(initialData: T? = null) : GlanceAppWidget(
val currentGlanceId = snapshotFlow { glanceId }.filterNotNull().firstOrNull()
println("initiateLoad $currentGlanceId")
if (currentGlanceId != null) {
update(context, currentGlanceId)
}

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

@ -64,7 +64,6 @@ fun IssMap() {
.testTag(ISSPositionMapTag)
.semantics { observedIssPosition = issPosition },
update = { map ->
println(issPosition)
val issPositionPoint = GeoPoint(issPosition.latitude, issPosition.longitude)
map.controller.setCenter(issPositionPoint)

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)
}