Skip to content

Commit

Permalink
Put LocalUseExpressiveIcon and NrkIconasPainter() in the library
Browse files Browse the repository at this point in the history
  • Loading branch information
hakonschia committed Jun 15, 2024
1 parent ae1b5ed commit 02a88a6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 22 deletions.
25 changes: 24 additions & 1 deletion .github/scripts/generate-android-vectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,37 @@ function generateKotlinValues() {
const lines = []
lines.push("package no.nrk.core.icons")

lines.push("")
lines.push("import androidx.compose.runtime.Composable")
lines.push("import androidx.compose.runtime.staticCompositionLocalOf")
lines.push("import androidx.compose.ui.graphics.painter.Painter")
lines.push("import androidx.compose.ui.res.painterResource")

lines.push("")
lines.push("// Warning: This is an autogenerated file. Please do not edit it manually")
lines.push("")

// It's unlikely that this will change on most screens, so it's probably a bit more efficient to use staticCompositionLocalOf here, right?
lines.push("val LocalUseExpressiveIcons = staticCompositionLocalOf<Boolean> {")
lines.push("\terror(\"LocalUseExpressiveIcons not found\")")
lines.push("}")
lines.push("")

lines.push("data class NrkIcon(")
lines.push("\tval normal: Int,")
lines.push("\tval expressive: Int")
lines.push(")")
lines.push(") {")
lines.push("\t@Composable")
lines.push("\tfun asPainter(): Painter {")
lines.push("\t\treturn painterResource(")
lines.push("\t\t\tid = if (LocalUseExpressiveIcons.current) {")
lines.push("\t\t\t\texpressive")
lines.push("\t\t\t} else {")
lines.push("\t\t\t\tnormal")
lines.push("\t\t\t}")
lines.push("\t\t)")
lines.push("\t}")
lines.push("}")
lines.push("")

lines.push("object NrkIcons {")
Expand Down
20 changes: 0 additions & 20 deletions android/app/src/main/kotlin/no/nrk/core/icons/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.Switch
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp

class MainActivity : AppCompatActivity() {
Expand Down Expand Up @@ -65,19 +61,3 @@ class MainActivity : AppCompatActivity() {
}
}
}

// It's unlikely that this changes on most screens, so it's probably a bit more efficient to use staticCompositionLocalOf here, right?
val LocalUseExpressiveIcons = staticCompositionLocalOf<Boolean> {
error("LocalUseExpressiveIcons not found")
}

@Composable
fun NrkIcon.asPainter(): Painter {
return painterResource(
id = if (LocalUseExpressiveIcons.current) {
expressive
} else {
normal
}
)
}
3 changes: 3 additions & 0 deletions android/icons/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
id 'org.jetbrains.kotlin.plugin.compose'
id 'maven-publish'
}

Expand Down Expand Up @@ -51,6 +52,8 @@ android {
}

dependencies {
implementation platform("androidx.compose:compose-bom:2024.06.00")
implementation "androidx.compose.ui:ui"
}

publishing {
Expand Down
22 changes: 21 additions & 1 deletion android/icons/src/main/kotlin/no/nrk/core/icons/NrkIcons.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
package no.nrk.core.icons

import androidx.compose.runtime.Composable
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.painterResource

// Warning: This is an autogenerated file. Please do not edit it manually

val LocalUseExpressiveIcons = staticCompositionLocalOf<Boolean> {
error("LocalUseExpressiveIcons not found")
}

data class NrkIcon(
val normal: Int,
val expressive: Int
)
) {
@Composable
fun asPainter(): Painter {
return painterResource(
id = if (LocalUseExpressiveIcons.current) {
expressive
} else {
normal
}
)
}
}

object NrkIcons {
val Nrk360 = NrkIcon(
Expand Down

0 comments on commit 02a88a6

Please sign in to comment.