Skip to content

Instantly share code, notes, and snippets.

@marcelpinto
Created July 6, 2019 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcelpinto/a1c533e843718f59de0c507cc39c14c1 to your computer and use it in GitHub Desktop.
Save marcelpinto/a1c533e843718f59de0c507cc39c14c1 to your computer and use it in GitHub Desktop.
abstract class BaseSlice(val context: Context, val sliceUri: Uri) {
/**
* @return the slice implementation to be used by SliceProvider
*/
abstract fun buildSlice(): Slice
/**
* Call refresh to notify the SliceProvider to load again.
*/
protected fun refresh() {
context.contentResolver.notifyChange(sliceUri, null)
}
}
class BaseSlicesProvider : SliceProvider() {
private val lastSlices = mutableMapOf<Uri, BaseSlice>()
override fun onBindSlice(sliceUri: Uri): Slice {
val slice = lastSlices.getOrPut(sliceUri) {
createNewSlice(sliceUri)
}
return slice.buildSlice()
}
override fun onCreateSliceProvider(): Boolean {
// Initialise your code
return true
}
open fun createNewSlice(sliceUri: Uri): BaseSlice
}
private fun createOrderSlice(data: Any): Slice {
return list(context = context, uri = sliceUri, ttl = ListBuilder.INFINITY) {
header {
title = "Your Order"
subtitle = "Status: ${data.order.status.name}"
primaryAction = createSliceAction {
appendPath(DeepLink.AwesomeSlices.PATH)
appendPath(DeepLink.AwesomeSlices.SHOW_ORDER)
}
}
data.items.forEach { item ->
row {
title = getItemTitle(data, item)
subtitle = "${item.price}$"
}
}
range {
title = "Estimated arrival"
subtitle = getEta(data)
value = getStep(data)
max = MAX_STEPS
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment