Skip to content

Commit

Permalink
Merge pull request #1113 from syarihu/fix_method_of_getting_version_n…
Browse files Browse the repository at this point in the history
…ames

[Android] Enable to get version name from BuildConfig on AboutScreen
  • Loading branch information
takahirom authored Sep 8, 2023
2 parents 761d195 + 4db99a1 commit ba2622f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.droidkaigi.confsched2023

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import io.github.droidkaigi.confsched2023.data.di.AppAndroidBuildConfig
import io.github.droidkaigi.confsched2023.model.BuildConfigProvider
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
class AppModule {
@Provides
@Singleton
@AppAndroidBuildConfig
fun provideBuildConfigProvider(): BuildConfigProvider = AppBuildConfigProvider()
}

class AppBuildConfigProvider(
override val versionName: String = BuildConfig.VERSION_NAME,
) : BuildConfigProvider
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.github.droidkaigi.confsched2023.data.di

import dagger.BindsOptionalOf
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import io.github.droidkaigi.confsched2023.model.BuildConfigProvider
import java.util.Optional
import javax.inject.Qualifier
import javax.inject.Singleton

@Qualifier
annotation class AppAndroidBuildConfig

@Module
@InstallIn(SingletonComponent::class)
class BuildConfigProviderModule {
@Provides
@Singleton
fun provideBuildConfigProvider(
@AppAndroidBuildConfig buildConfigOverride: Optional<BuildConfigProvider>,
): BuildConfigProvider = if (buildConfigOverride.isPresent) {
buildConfigOverride.get()
} else {
EmptyBuildConfigProvider
}
}

@InstallIn(SingletonComponent::class)
@Module
abstract class AppAndroidBuildConfigModule {
@BindsOptionalOf
@AppAndroidBuildConfig
abstract fun bindBuildConfigProvider(): BuildConfigProvider
}

private object EmptyBuildConfigProvider : BuildConfigProvider {
override val versionName: String = ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.github.droidkaigi.confsched2023.model

interface BuildConfigProvider {
val versionName: String
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package io.github.droidkaigi.confsched2023.about

import android.content.Context
import android.content.pm.PackageManager.PackageInfoFlags
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.lazy.LazyColumn
Expand All @@ -16,11 +12,12 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.text.font.FontWeight
Expand Down Expand Up @@ -62,38 +59,36 @@ const val AboutScreenTestTag = "AboutScreen"
@Composable
fun AboutScreen(
onAboutItemClick: (AboutItem) -> Unit,
viewModel: AboutScreenViewModel = hiltViewModel<AboutScreenViewModel>(),
viewModel: AboutScreenViewModel = hiltViewModel(),
contentPadding: PaddingValues = PaddingValues(),
onLinkClick: (url: String) -> Unit,
) {
// val uiState by viewModel.uiState.collectAsState()
val context = LocalContext.current
val uiState by viewModel.uiState.collectAsState()
val snackbarHostState = remember { SnackbarHostState() }
val versionName = remember { versionName(context) }

SnackbarMessageEffect(
snackbarHostState = snackbarHostState,
userMessageStateHolder = viewModel.userMessageStateHolder,
)
AboutScreen(
// uiState = uiState,
uiState = uiState,
snackbarHostState = snackbarHostState,
onAboutItemClick = onAboutItemClick,
versionName = versionName,
onLinkClick = onLinkClick,
contentPadding = contentPadding,
)
}

class AboutScreenUiState
class AboutScreenUiState(
val versionName: String,
)

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun AboutScreen(
// uiState: AboutScreenUiState,
uiState: AboutScreenUiState,
snackbarHostState: SnackbarHostState,
onAboutItemClick: (AboutItem) -> Unit,
versionName: String?,
onLinkClick: (url: String) -> Unit,
contentPadding: PaddingValues,
) {
Expand Down Expand Up @@ -163,7 +158,7 @@ private fun AboutScreen(
)
item {
AboutFooterLinks(
versionName = versionName,
versionName = uiState.versionName,
onYouTubeClick = {
onAboutItemClick(AboutItem.YouTube)
},
Expand All @@ -179,18 +174,3 @@ private fun AboutScreen(
},
)
}

private fun versionName(context: Context) = runCatching {
val info = if (VERSION.SDK_INT >= VERSION_CODES.TIRAMISU) {
context.packageManager.getPackageInfo(
context.packageName,
PackageInfoFlags.of(0),
)
} else {
context.packageManager.getPackageInfo(
context.packageName,
0,
)
}
info.versionName
}.getOrNull()
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ package io.github.droidkaigi.confsched2023.about

import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import io.github.droidkaigi.confsched2023.model.BuildConfigProvider
import io.github.droidkaigi.confsched2023.ui.UserMessageStateHolder
import io.github.droidkaigi.confsched2023.ui.buildUiState
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.StateFlow
import javax.inject.Inject

@HiltViewModel
class AboutScreenViewModel @Inject constructor(
val userMessageStateHolder: UserMessageStateHolder,
buildConfigProvider: BuildConfigProvider,
) : ViewModel(),
UserMessageStateHolder by userMessageStateHolder {
private val versionNameStateFlow = MutableStateFlow(buildConfigProvider.versionName)

val uiState = MutableStateFlow(
AboutScreenUiState(),
).asStateFlow()
val uiState: StateFlow<AboutScreenUiState> = buildUiState(versionNameStateFlow) {
AboutScreenUiState(buildConfigProvider.versionName)
}
}

0 comments on commit ba2622f

Please sign in to comment.