Skip to content

Commit

Permalink
Merge pull request #169 from teogor/refactor/code-refactoring
Browse files Browse the repository at this point in the history
Refactor: Optimized Code Structure and Deprecated Redundant Blocks
  • Loading branch information
teogor authored Oct 29, 2023
2 parents a75a53e + 79be771 commit 01e61b0
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 40 deletions.
38 changes: 37 additions & 1 deletion app/src/main/kotlin/dev/teogor/ceres/feature/home/HomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,23 @@

package dev.teogor.ceres.feature.home

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.google.android.gms.ads.MobileAds
import dev.teogor.ceres.R
import dev.teogor.ceres.ads.HomeNativeAdBeta
import dev.teogor.ceres.core.foundation.compositions.LocalNetworkMonitor
import dev.teogor.ceres.core.foundation.extensions.createMediaPlayer
import dev.teogor.ceres.core.startup.ApplicationContextProvider.context
import dev.teogor.ceres.data.datastore.defaults.ceresPreferences
import dev.teogor.ceres.framework.core.app.BaseActions
import dev.teogor.ceres.framework.core.app.setScreenInfo
Expand All @@ -42,6 +52,7 @@ import dev.teogor.ceres.screen.builder.compose.SimpleView
import dev.teogor.ceres.screen.core.layout.ColumnLayoutBase
import dev.teogor.ceres.screen.ui.api.ExperimentalOnboardingScreenApi
import dev.teogor.ceres.screen.ui.onboarding.OnboardingRoute
import dev.teogor.ceres.ui.theme.MaterialTheme

// todo better way to configure this. perhaps use kotlin builder syntax
@Composable
Expand Down Expand Up @@ -150,5 +161,30 @@ private fun HomeScreen(
},
)

NativeAd<HomeNativeAdBeta>()
SimpleView(
title = "Open Ad Inspector",
clickable = {
MobileAds.openAdInspector(
context,
) {
// Error will be non-null if ad inspector closed due to an error.
}
},
)

repeat(10) {
NativeAd<HomeNativeAdBeta>()
repeat(5) {
Box(
modifier = Modifier
.padding(horizontal = 10.dp, vertical = 4.dp)
.fillMaxWidth()
.height(40.dp)
.background(
color = MaterialTheme.colorScheme.background,
shape = RoundedCornerShape(20.dp),
),
) { }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fun OnLifecycleEvent(onEvent: (owner: LifecycleOwner, event: Lifecycle.Event) ->
@Composable
fun ObserveActivityLifecycle(
onEnterForeground: () -> Unit = {},
onExitForeground: () -> Unit = {}
onExitForeground: () -> Unit = {},
) {
val currentActivity = activity

Expand Down Expand Up @@ -104,7 +104,7 @@ fun ObserveActivityLifecycle(
@Composable
fun ObserveApplicationLifecycle(
onEnterForeground: () -> Unit = {},
onExitForeground: () -> Unit = {}
onExitForeground: () -> Unit = {},
) {
val context = LocalContext.current.applicationContext as Application

Expand Down Expand Up @@ -157,7 +157,7 @@ fun ObserveApplicationLifecycle(
@Composable
fun ObserveComposableLifecycle(
onEnterForeground: () -> Unit = {},
onExitForeground: () -> Unit = {}
onExitForeground: () -> Unit = {},
) {
val lifecycleOwner = LocalLifecycleOwner.current

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dev.teogor.ceres.core.runtime

import android.content.pm.ApplicationInfo
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.util.Base64
Expand All @@ -27,30 +28,13 @@ import java.io.FileInputStream
import java.security.MessageDigest
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZoneOffset

@Suppress("MemberVisibilityCanBePrivate", "unused")
object AppMetadataManager {
val packageName: String
get() = ApplicationContextProvider.context.packageName

val sanitizedPackageName: String
get() {
var sanitizedPackageName = when (flavor) {
"demo" -> packageName.removeSuffix(".demo")
else -> packageName
}
sanitizedPackageName = if (debug) {
sanitizedPackageName.removeSuffix(".debug")
} else {
sanitizedPackageName
}
sanitizedPackageName = when (flavor) {
"demo" -> sanitizedPackageName.removeSuffix(".demo")
else -> sanitizedPackageName
}
return sanitizedPackageName
}

val packageManager: PackageManager
get() = ApplicationContextProvider.context.packageManagerUtils().packageManager

Expand All @@ -63,29 +47,36 @@ object AppMetadataManager {
val versionCode: Long
get() = ApplicationContextProvider.context.packageManagerUtils().versionCode

val zoneOffset = ZoneId.systemDefault().rules.getOffset(LocalDateTime.now())
val buildDateTime = LocalDateTime.ofEpochSecond(
BuildConfig.BUILD_DATE_TIME.toLong(),
0,
zoneOffset,
)
val zoneOffset: ZoneOffset
get() = ZoneId.systemDefault().rules.getOffset(LocalDateTime.now())

val buildType = BuildConfig.BUILD_TYPE
val flavor = "demo" // todo BuildConfig.FLAVOR
val debug = BuildConfig.DEBUG
val gitHash = BuildConfig.GIT_HASH
val ceresFrameworkVersion = BuildConfig.CERES_FRAMEWORK_VERSION
val buildDateTime: LocalDateTime
get() = LocalDateTime.ofEpochSecond(
BuildConfig.BUILD_DATE_TIME.toLong(),
0,
zoneOffset,
)

val isDebuggable: Boolean
get() = BuildConfig.DEBUG
get() = packageInfo.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0

val buildType: String
get() = BuildConfig.BUILD_TYPE

// todo BuildConfig.FLAVOR
val flavor: String
get() = "demo"

val gitHash: String
get() = BuildConfig.GIT_HASH

val ceresFrameworkVersion: String
get() = BuildConfig.CERES_FRAMEWORK_VERSION

val apkSignature: String?
get() {
try {
val packageInfo = packageInfo
val sourceDir =
packageInfo.applicationInfo.sourceDir

val sourceDir = packageInfo.applicationInfo.sourceDir
val file = File(sourceDir)
val md = MessageDigest.getInstance("SHA-256")
val fis = FileInputStream(file)
Expand All @@ -102,4 +93,31 @@ object AppMetadataManager {
}
return null
}

// region Deprecated API
@Deprecated("use isDebuggable")
val debug = isDebuggable

@Deprecated(
message = "This property is deprecated. Please use packageName property instead.",
replaceWith = ReplaceWith("packageName"),
)
val sanitizedPackageName: String
get() {
var sanitizedPackageName = when (flavor) {
"demo" -> packageName.removeSuffix(".demo")
else -> packageName
}
sanitizedPackageName = if (isDebuggable) {
sanitizedPackageName.removeSuffix(".debug")
} else {
sanitizedPackageName
}
sanitizedPackageName = when (flavor) {
"demo" -> sanitizedPackageName.removeSuffix(".demo")
else -> sanitizedPackageName
}
return sanitizedPackageName
}
// endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import androidx.navigation.NavDestination
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavOptions
import dev.teogor.ceres.core.foundation.FoundationGlobal
import dev.teogor.ceres.core.foundation.NetworkMonitor
import dev.teogor.ceres.core.foundation.NetworkMonitorUtility
import dev.teogor.ceres.core.foundation.compositions.LocalNetworkMonitor
import dev.teogor.ceres.data.datastore.defaults.ceresPreferences
Expand Down Expand Up @@ -141,7 +142,7 @@ fun CeresApp(
// todo VMs
val bottomSheetVM: BottomSheetState = viewModel()
val toolbarState: ToolbarState = viewModel()
val networkMonitor = dev.teogor.ceres.core.foundation.NetworkMonitor()
val networkMonitor = NetworkMonitor()

CompositionLocalProvider(
LocalBottomSheetVM provides bottomSheetVM,
Expand Down
3 changes: 2 additions & 1 deletion screen/core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ android {
}

dependencies {
api(project(":core:analytics"))
api(project(":navigation:core"))
api(project(":ui:designsystem"))
api(project(":ui:theme"))
api(project(":ui:compose"))
api(project(":navigation:events"))

api(libs.androidx.compose.foundation)
}
Expand Down

0 comments on commit 01e61b0

Please sign in to comment.