23 May, 2024

Kotlin 2.0

 Migrated the current Android project to Kotlin 2.0 and it seems that Compose has strong skipping mode enabled by default as promised. Didn't see anything about it in the announcements though.

16 March, 2024

Start using adb from terminal on MacOS

 

Add the android sdk and platform-tools to your zsh

echo 'export ANDROID_HOME=/Users/$USER/Library/Android/sdk' >> ~/.zshrc
echo 'export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools' >> ~/.zshrc

 

04 March, 2024

Jetpack Compose remove unselected tab indicator

 

use 

divider = {},

like so:

    TabRow(
selectedTabIndex = selectedTabIndex,
modifier = modifier,
containerColor = Color.Transparent,
contentColor = MaterialTheme.colorScheme.onSurface,
indicator = { tabPositions ->
TabRowDefaults.Indicator(
modifier = Modifier.tabIndicatorOffset(tabPositions[selectedTabIndex]),
height = 2.dp,
// uncomment below to customize indicator color:
// color = MaterialTheme.colorScheme.onSurface,
)
},
divider = {},
tabs = tabs,
)

 

Disable Dark Theme alltoghether

 

@AndroidEntryPoint
class MainActivity : ComponentActivity() {


@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val config = resources.configuration
config.uiMode = Configuration.UI_MODE_NIGHT_NO
resources.updateConfiguration(config, resources.displayMetrics)

03 March, 2024

Download an image to local disk and show it with Compose.

 



class DownloadImageUseCase @Inject constructor(
@ApplicationContext private val context: Context,
private val dispatchersProvider: DispatchersProvider,
) {
suspend operator fun invoke(imageUrl: String): String? = withContext(dispatchersProvider.io()) {
try {
val input = URL(imageUrl).openStream()
val fileName = imageUrl.substringAfterLast("/")
val file = File(context.filesDir, fileName)
FileOutputStream(file).use { output ->
input.copyTo(output)
}
file.absolutePath
} catch (e: Exception) {
e.printStackTrace()
null
}
}
}
 
 
viewModelScope.launch {
state.update { oldState ->
oldState.copy(
imagePath =
downloadImageUseCase(
"https://apod.nasa.gov/apod/image/2403/IM_Odysseus_landing-1100x600.png",
),
)
}
}
 

@Composable
fun DisplayImageFromDisk(imagePath: String?) {
imagePath?.let {
Image(
painter = rememberAsyncImagePainter(File(it)),
contentDescription = "Cached Image",
modifier = Modifier.fillMaxWidth()
)
}
}