Skip to content

Instantly share code, notes, and snippets.

View apkelly's full-sized avatar

Andrew Kelly apkelly

View GitHub Profile
@apkelly
apkelly / Results.md
Last active August 6, 2024 06:58
Annotated String resources in Kotlin and for Jetpack Compose UI

The code below renders the following text in both Android Views and in Jetpack Compose UI.

Contact our team on 555 555 555 Opt 3 to activate.

import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
@apkelly
apkelly / MatrixCompose.kt
Last active September 16, 2021 23:11
Matrix Animation
import android.graphics.Typeface
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
private lateinit var mViewModel: CloudAutoMLViewModel
override fun onCreate(icicle: Bundle?) {
super.onCreate(icicle)
mViewModel = ViewModelProviders.of(this).get(CloudAutoMLViewModel::class.java)
mViewModel.subscribeClassifications()
.observe(this, Observer<Resource<Pair<Int, String>, Throwable>> { resource ->
when (resource) {
is LoadingResource -> {
@apkelly
apkelly / GoogleVisionActivity.kt
Created February 24, 2019 13:59
GoogleVisionActivity.kt
override fun onNewItem(faceId: Int, item: Face) {
mFaceGraphic = FaceGraphic(faceId, mGraphicOverlay)
mDetector.lastFrame?.let { frame ->
// Lets try and find out who this face belongs to
mViewModel.classify(faceId, frame.convertToByteArray())
}
}
@apkelly
apkelly / CloudAutoMLViewModel.kt
Last active February 24, 2019 13:24
CloudAutoMLViewModel.kt
companion object {
private const val PROJECT = "devnibbles"
private const val LOCATION = "us-central1"
private const val MODEL = "ICN3704829353327390855"
private const val SERVICE_ACCOUNT_JSON = "<insert json here>"
}
private val mServiceCredentials = ServiceAccountCredentials
.fromStream(ByteArrayInputStream(SERVICE_ACCOUNT_JSON.toByteArray(Charset.defaultCharset())))
.createScoped(mutableListOf("https://www.googleapis.com/auth/cloud-platform"))
companion object {
private const val PROJECT = "devnibbles"
private const val LOCATION = "us-central1"
private const val MODEL = "ICN3704829353327390855"
private const val ACCESS_TOKEN = "<insert token here>"
}
fun classifyUsingRetrofit(faceId: Int, imageBytes: ByteArray) {
launch(errorHandler) {
// Show loading indicator while we wait for the request.
@apkelly
apkelly / CloudAutoMLViewModel.kt
Last active February 1, 2019 01:12
CloudAutoMLViewModel.kt
private fun getRESTService(): CloudAutoMLService {
val gsonFactory = GsonConverterFactory
.create(GsonBuilder().create())
val networkClient = OkHttpClient.Builder()
.addInterceptor(HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
})
.build()
@apkelly
apkelly / CloudAutoMLModel.kt
Created February 1, 2019 01:05
CloudAutoMLModel.kt
// Expected json payload for webservice.
// {
// "payload": {
// "image": {
// "imageBytes": "YOUR_IMAGE_BYTE"
// }
// }
// }
data class CloudAutoMLModel(val payload: Payload)
@apkelly
apkelly / CloudAutoMLService.kt
Last active February 14, 2019 11:08
CloudAutoMLService.kt
// Expected json response from webservice
//{
// "payload": [
// {
// "classification": {
// "score": 0.87991875
// },
// "displayName": "Andy"
// }
// ]