Skip to content

Commit

Permalink
Remove onTerminate in TilesApplication (#403)
Browse files Browse the repository at this point in the history
* Remove unused update mechanism

* Remove onTerminate

- it's not called on prod devices
- the app container will be GC'd anyway because the app is being destroyed

* Add singleEntryTimeline convenience function to minimize boilerplate

* Add missing license header

* Fix incorrect syntax
  • Loading branch information
ataulm authored Jun 8, 2022
1 parent ea4bbf3 commit 87bcca5
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 103 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.wear.tiles

import androidx.wear.tiles.LayoutElementBuilders
import androidx.wear.tiles.LayoutElementBuilders.LayoutElement
import androidx.wear.tiles.TimelineBuilders

/**
* Provides a [TimelineBuilders.Timeline] which has only one entry.
*/
internal fun singleEntryTimeline(rootLayout: LayoutElement): TimelineBuilders.Timeline {
return TimelineBuilders.Timeline.Builder()
.addTimelineEntry(
TimelineBuilders.TimelineEntry.Builder()
.setLayout(
LayoutElementBuilders.Layout.Builder()
.setRoot(rootLayout)
.build()
)
.build()
)
.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ class TilesApplication : Application(), ImageLoaderFactory {
appContainer = AppContainer(this)
}

override fun onTerminate() {
appContainer.close()

super.onTerminate()
}

// Allows Coil ImageLoader singleton to work from anywhere
override fun newImageLoader(): ImageLoader = appContainer.imageLoader
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import android.content.Context
import coil.ImageLoader
import com.example.wear.tiles.messaging.MessagingRepo
import com.example.wear.tiles.messaging.MessagingTileRenderer
import com.example.wear.tiles.messaging.Updates
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
Expand All @@ -34,7 +33,6 @@ class AppContainer(application: Context) : Closeable {
val repo: MessagingRepo = MessagingRepo(application)
val renderer: MessagingTileRenderer = MessagingTileRenderer(application)
val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
val updates: Updates = Updates(application = application, scope = scope, repo = repo)

override fun close() {
scope.cancel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ import androidx.wear.tiles.LayoutElementBuilders.ArcLine
import androidx.wear.tiles.LayoutElementBuilders.Box
import androidx.wear.tiles.LayoutElementBuilders.Column
import androidx.wear.tiles.LayoutElementBuilders.FontStyles
import androidx.wear.tiles.LayoutElementBuilders.Layout
import androidx.wear.tiles.LayoutElementBuilders.Text
import androidx.wear.tiles.ModifiersBuilders.Modifiers
import androidx.wear.tiles.ModifiersBuilders.Semantics
import androidx.wear.tiles.RequestBuilders.ResourcesRequest
import androidx.wear.tiles.RequestBuilders.TileRequest
import androidx.wear.tiles.ResourceBuilders.Resources
import androidx.wear.tiles.TileBuilders.Tile
import androidx.wear.tiles.TimelineBuilders.Timeline
import androidx.wear.tiles.TimelineBuilders.TimelineEntry
import com.example.wear.tiles.R
import com.example.wear.tiles.singleEntryTimeline
import com.google.android.horologist.tiles.CoroutinesTileService

// Updating this version triggers a new call to onResourcesRequest(). This is useful for dynamic
Expand Down Expand Up @@ -66,19 +64,9 @@ class FitnessTileService : CoroutinesTileService() {
.setResourcesVersion(RESOURCES_VERSION)
// Creates a timeline to hold one or more tile entries for a specific time periods.
.setTimeline(
Timeline.Builder()
.addTimelineEntry(
TimelineEntry.Builder()
.setLayout(
Layout.Builder()
.setRoot(
layout(goalProgress, requestParams.deviceParameters!!)
)
.build()
)
.build()
)
.build()
singleEntryTimeline(
layout(goalProgress, requestParams.deviceParameters!!)
)
)
.build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import androidx.wear.tiles.DimensionBuilders.dp
import androidx.wear.tiles.LayoutElementBuilders.Column
import androidx.wear.tiles.LayoutElementBuilders.FontStyles
import androidx.wear.tiles.LayoutElementBuilders.Image
import androidx.wear.tiles.LayoutElementBuilders.Layout
import androidx.wear.tiles.LayoutElementBuilders.Row
import androidx.wear.tiles.LayoutElementBuilders.Spacer
import androidx.wear.tiles.LayoutElementBuilders.Text
Expand All @@ -36,10 +35,9 @@ import androidx.wear.tiles.ResourceBuilders.AndroidImageResourceByResId
import androidx.wear.tiles.ResourceBuilders.ImageResource
import androidx.wear.tiles.ResourceBuilders.Resources
import androidx.wear.tiles.TileBuilders.Tile
import androidx.wear.tiles.TimelineBuilders.Timeline
import androidx.wear.tiles.TimelineBuilders.TimelineEntry
import com.example.wear.tiles.R
import com.example.wear.tiles.components.IconButton
import com.example.wear.tiles.singleEntryTimeline
import com.google.android.horologist.tiles.CoroutinesTileService

// Updating this version triggers a new call to onResourcesRequest(). This is useful for dynamic
Expand Down Expand Up @@ -73,18 +71,10 @@ class PlayNextSongTileService : CoroutinesTileService() {
override suspend fun tileRequest(requestParams: TileRequest): Tile {
return Tile.Builder().apply {
setResourcesVersion(RESOURCES_VERSION)
// Creates a timeline to hold one or more tile entries for a specific time periods.
setTimeline(
Timeline.Builder()
.addTimelineEntry(
TimelineEntry.Builder()
.setLayout(
Layout.Builder()
.setRoot(tileLayout(requestParams.deviceParameters!!)).build()
)
.build()
)
.build()
singleEntryTimeline(
tileLayout(requestParams.deviceParameters!!)
)
)
}.build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import androidx.wear.tiles.LayoutElementBuilders.Box
import androidx.wear.tiles.LayoutElementBuilders.Column
import androidx.wear.tiles.LayoutElementBuilders.FontStyles
import androidx.wear.tiles.LayoutElementBuilders.HORIZONTAL_ALIGN_CENTER
import androidx.wear.tiles.LayoutElementBuilders.Layout
import androidx.wear.tiles.LayoutElementBuilders.LayoutElement
import androidx.wear.tiles.LayoutElementBuilders.Row
import androidx.wear.tiles.LayoutElementBuilders.Spacer
Expand All @@ -41,11 +40,10 @@ import androidx.wear.tiles.ResourceBuilders
import androidx.wear.tiles.ResourceBuilders.ImageResource
import androidx.wear.tiles.ResourceBuilders.Resources
import androidx.wear.tiles.TileBuilders.Tile
import androidx.wear.tiles.TimelineBuilders
import androidx.wear.tiles.TimelineBuilders.TimelineEntry
import androidx.wear.tiles.material.Button
import androidx.wear.tiles.material.ButtonColors
import com.example.wear.tiles.R
import com.example.wear.tiles.singleEntryTimeline
import com.example.wear.tiles.util.TileRenderer

class MessagingTileRenderer(context: Context) :
Expand All @@ -54,30 +52,13 @@ class MessagingTileRenderer(context: Context) :
tileState: MessagingTileState,
requestParams: RequestBuilders.TileRequest,
): Tile {
val tileLayout = tileLayout(
tileState,
requestParams.deviceParameters!!
)

val timelineEntry = TimelineEntry.Builder()
.setLayout(
Layout.Builder()
.setRoot(
tileLayout
)
.build()
)
.build()

return Tile.Builder()
.setResourcesVersion(PERMANENT_RESOURCES_VERSION)
// Creates a timeline to hold one or more tile entries for a specific time periods.
.setTimeline(
TimelineBuilders.Timeline.Builder()
.addTimelineEntry(
timelineEntry
)
.build()
singleEntryTimeline(
tileLayout(tileState, requestParams.deviceParameters!!)
)
).build()
}

Expand Down Expand Up @@ -113,9 +94,8 @@ class MessagingTileRenderer(context: Context) :
return resourcesBuilder.build()
}

private fun RequestBuilders.ResourcesRequest.resourceRequested(
searchElement: String
) = resourceIds.isEmpty() || resourceIds.contains(searchElement)
private fun RequestBuilders.ResourcesRequest.resourceRequested(resourceId: String) =
resourceIds.isEmpty() || resourceIds.contains(resourceId)

internal fun tileLayout(
state: MessagingTileState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class MessagingTileService : CoroutinesTileService() {
private lateinit var renderer: MessagingTileRenderer
private lateinit var repo: MessagingRepo
private lateinit var imageLoader: ImageLoader
private lateinit var updates: Updates

override fun onCreate() {
super.onCreate()
Expand All @@ -58,7 +57,6 @@ class MessagingTileService : CoroutinesTileService() {
renderer = appContainer.renderer
repo = appContainer.repo
imageLoader = ImageLoader(this)
updates = appContainer.updates

tileStateFlow = repo.getFavoriteContacts()
.map {
Expand Down

This file was deleted.

0 comments on commit 87bcca5

Please sign in to comment.