generated from cortinico/kotlin-android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Andi-IM/dev
Dev
- Loading branch information
Showing
106 changed files
with
4,163 additions
and
305 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
app/src/main/java/com/github/andiim/plantscan/app/PlantScanActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.github.andiim.plantscan.app | ||
|
||
import android.os.Bundle | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import dagger.hilt.android.AndroidEntryPoint | ||
|
||
@AndroidEntryPoint | ||
class PlantScanActivity : ComponentActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContent { PlantScanApp() } | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
app/src/main/java/com/github/andiim/plantscan/app/PlantScanApp.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package com.github.andiim.plantscan.app | ||
|
||
import android.Manifest | ||
import android.content.Context | ||
import android.os.Build | ||
import androidx.annotation.RequiresApi | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.Snackbar | ||
import androidx.compose.material3.SnackbarHost | ||
import androidx.compose.material3.SnackbarHostState | ||
import androidx.compose.material3.Surface | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.ReadOnlyComposable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalConfiguration | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.unit.dp | ||
import androidx.navigation.NavHostController | ||
import androidx.navigation.compose.rememberNavController | ||
import com.github.andiim.plantscan.app.ui.common.composables.BottomBar | ||
import com.github.andiim.plantscan.app.ui.common.composables.PermissionDialog | ||
import com.github.andiim.plantscan.app.ui.common.composables.RationaleDialog | ||
import com.github.andiim.plantscan.app.ui.common.snackbar.SnackbarManager | ||
import com.github.andiim.plantscan.app.ui.navigation.SetupRootNavGraph | ||
import com.github.andiim.plantscan.app.ui.theme.PlantScanTheme | ||
import com.google.accompanist.permissions.ExperimentalPermissionsApi | ||
import com.google.accompanist.permissions.isGranted | ||
import com.google.accompanist.permissions.rememberPermissionState | ||
import com.google.accompanist.permissions.shouldShowRationale | ||
import kotlinx.coroutines.CoroutineScope | ||
|
||
@Composable | ||
fun PlantScanApp() { | ||
PlantScanTheme { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
RequestNotificationPermissionDialog() | ||
} | ||
|
||
Surface( | ||
modifier = Modifier.fillMaxSize(), | ||
color = MaterialTheme.colorScheme.background | ||
) { | ||
|
||
val appState = rememberAppState() | ||
Scaffold( | ||
snackbarHost = { | ||
SnackbarHost( | ||
hostState = appState.snackbarHostState, | ||
modifier = Modifier.padding(8.dp), | ||
snackbar = { snackbarData -> | ||
Snackbar( | ||
snackbarData, | ||
contentColor = MaterialTheme.colorScheme.onPrimary | ||
) | ||
}) | ||
}, | ||
bottomBar = { BottomBar(state = appState) } | ||
) { innerPadding -> | ||
SetupRootNavGraph(appState, modifier = Modifier.padding(innerPadding)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
@ReadOnlyComposable | ||
fun getContext(): Context { | ||
LocalConfiguration.current | ||
return LocalContext.current | ||
} | ||
|
||
@Composable | ||
fun rememberAppState( | ||
snackbarHostState: SnackbarHostState = SnackbarHostState(), | ||
navController: NavHostController = rememberNavController(), | ||
snackbarManager: SnackbarManager = SnackbarManager, | ||
getContext: Context = getContext(), | ||
coroutineScope: CoroutineScope = rememberCoroutineScope() | ||
) = | ||
remember( | ||
snackbarHostState, | ||
navController, | ||
snackbarManager, | ||
getContext, | ||
coroutineScope | ||
) { | ||
PlantScanAppState( | ||
snackbarHostState, | ||
navController, | ||
snackbarManager, | ||
getContext, | ||
coroutineScope | ||
) | ||
} | ||
|
||
@RequiresApi(Build.VERSION_CODES.TIRAMISU) | ||
@OptIn(ExperimentalPermissionsApi::class) | ||
@Composable | ||
fun RequestNotificationPermissionDialog() { | ||
val permissionState = | ||
rememberPermissionState(permission = Manifest.permission.POST_NOTIFICATIONS) | ||
|
||
if (!permissionState.status.isGranted) { | ||
if (permissionState.status.shouldShowRationale) RationaleDialog() | ||
else PermissionDialog { permissionState.launchPermissionRequest() } | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
app/src/main/java/com/github/andiim/plantscan/app/PlantScanAppState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.github.andiim.plantscan.app | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.net.Uri | ||
import androidx.compose.material3.SnackbarHostState | ||
import androidx.navigation.NavGraph.Companion.findStartDestination | ||
import androidx.navigation.NavHostController | ||
import com.github.andiim.plantscan.app.ui.common.snackbar.SnackbarManager | ||
import com.github.andiim.plantscan.app.ui.common.snackbar.SnackbarMessage.Companion.toMessage | ||
import com.github.andiim.plantscan.app.ui.navigation.Direction | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.filterNotNull | ||
import kotlinx.coroutines.launch | ||
|
||
class PlantScanAppState( | ||
val snackbarHostState: SnackbarHostState, | ||
val navController: NavHostController, | ||
private val snackbarManager: SnackbarManager, | ||
private val context: Context, | ||
coroutineScope: CoroutineScope | ||
) { | ||
init { | ||
coroutineScope.launch { | ||
snackbarManager.snackbarMessages.filterNotNull().collect { snackbarMessage -> | ||
val text = snackbarMessage.toMessage(context.resources) | ||
snackbarHostState.showSnackbar(text) | ||
} | ||
} | ||
} | ||
|
||
fun popUp() { | ||
navController.popBackStack() | ||
} | ||
|
||
fun navigate(route: String, singleTopLaunch: Boolean = true) { | ||
if (route == Direction.Detect.route) { | ||
toDetectActivity() | ||
} else { | ||
navController.navigate(route) { launchSingleTop = singleTopLaunch } | ||
} | ||
} | ||
|
||
private fun toDetectActivity() { | ||
val intent = Intent(Intent.ACTION_VIEW).apply { | ||
data = Uri.parse("plantscan://detection") | ||
`package` = context.packageName | ||
} | ||
context.startActivity(intent) | ||
} | ||
|
||
fun navigateAndPopUp(route: String, popUp: String) { | ||
navController.navigate(route) { | ||
launchSingleTop = true | ||
popUpTo(popUp) { inclusive = true } | ||
} | ||
} | ||
|
||
fun clearAndNavigate(route: String) { | ||
navController.navigate(route) { | ||
popUpTo(navController.graph.findStartDestination().id) { | ||
// saveState = true | ||
inclusive = true | ||
} | ||
launchSingleTop = true | ||
// restoreState = true | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
app/src/main/java/com/github/andiim/plantscan/app/PlantScanHiltApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.github.andiim.plantscan.app | ||
|
||
import android.app.Application | ||
import com.github.andiim.plantscan.app.di.DebugModule.provideTimberTree | ||
import dagger.hilt.android.HiltAndroidApp | ||
import timber.log.Timber | ||
|
||
@HiltAndroidApp | ||
class PlantScanHiltApplication : Application() { | ||
override fun onCreate() { | ||
super.onCreate() | ||
|
||
if (BuildConfig.DEBUG) { | ||
Timber.plant(provideTimberTree()) | ||
} | ||
} | ||
} |
Oops, something went wrong.