Skip to content

Commit

Permalink
implement compose navigator & single activity principle
Browse files Browse the repository at this point in the history
  • Loading branch information
RocqJones committed Mar 5, 2024
1 parent d2d93dc commit 715d362
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 184 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ dependencies {
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation "androidx.compose.material3:material3:$material3_version"
// Compose navigation
implementation("androidx.navigation:navigation-compose:$navigation_version")

testImplementation "junit:junit:$junit_version"
androidTestImplementation "androidx.test.ext:junit:$test_junit"
Expand Down
98 changes: 23 additions & 75 deletions app/src/main/java/com/rocqjones/mesdk/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package com.rocqjones.mesdk

import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material3.Button
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.rocqjones.me_design.screens.ListActivity
import com.rocqjones.me_design.screens.bottomNavAdaptive.AdaptiveNavActivity
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.rocqjones.me_design.screens.BottomSheetDialogScreen
import com.rocqjones.me_design.screens.EndlessScreen
import com.rocqjones.me_design.ui.theme.MeSDKTheme
import com.rocqjones.me_logic.models.Screen
import com.rocqjones.mesdk.screens.HomeScreen

/**
* This is the first launcher screen of the SDK.
Expand All @@ -28,83 +28,34 @@ class MainActivity : ComponentActivity() {
super.onCreate(savedInstanceState)
setContent {
MeSDKTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
FirstScreen(
onMoveToListClicked = { navigateToList() },
onMoveToNavClicked = { navigateToAdaptiveUI() }
)
MyAppMain()
}
}
}
}

private fun navigateToAdaptiveUI() {
try {
val b = Intent(this, AdaptiveNavActivity::class.java)
b.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(b)
} catch (e: Exception) {
e.printStackTrace()
}
}

private fun navigateToList() {
try {
val a = Intent(this, ListActivity::class.java)
a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(a)
} catch (e: Exception) {
e.printStackTrace()
}
}
}

@Composable
fun FirstScreen(
onMoveToListClicked: () -> Unit,
onMoveToNavClicked: () -> Unit,
modifier: Modifier = Modifier
) {
/**
* When the button is clicked, should navigate to the respective UI
*/
Column(
modifier = modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
fun MyAppMain() {
val navController = rememberNavController()

NavHost(
navController, startDestination = Screen.HomeScreen.route
) {
val padding = 8.dp
Text(
"Welcome to the Me SDK!",
style = MaterialTheme.typography.headlineMedium,
color =MaterialTheme.colorScheme.secondary,
)
// list
Button(
modifier = Modifier.padding(padding).fillMaxWidth(),
shape = MaterialTheme.shapes.medium,
onClick = onMoveToListClicked // sets the sate to true
) {
Text(
"Endless List",
style = MaterialTheme.typography.bodyLarge
)
composable(Screen.HomeScreen.route) {
HomeScreen(navController)
}

composable(Screen.EndlessScreen.route) {
EndlessScreen(navController)
}

// Adaptive Navigation
Button(
modifier = Modifier.padding(padding).fillMaxWidth(),
shape = MaterialTheme.shapes.medium,
onClick = onMoveToNavClicked // sets the sate to true
) {
Text(
"Adaptive Navigation",
style = MaterialTheme.typography.bodyLarge
)
composable(Screen.BottomSheetDialogScreen.route) {
BottomSheetDialogScreen(navController)
}
}
}
Expand All @@ -123,9 +74,6 @@ fun FirstScreen(
@Composable
fun DefaultPreview() {
MeSDKTheme {
FirstScreen(
onMoveToListClicked = {},
onMoveToNavClicked = {}
) // empty lambda expression means "Do nothing" on click
MyAppMain()
}
}
67 changes: 67 additions & 0 deletions app/src/main/java/com/rocqjones/mesdk/screens/HomeScreen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.rocqjones.mesdk.screens

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.rocqjones.me_logic.models.Screen

@Composable
fun HomeScreen(navController: NavHostController) {

Box {
/**
* When the button is clicked, should navigate to the respective UI
*/
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
val padding = 8.dp
Text(
"Welcome to the Me SDK!",
style = MaterialTheme.typography.headlineMedium,
color = MaterialTheme.colorScheme.secondary,
)

// list
Button(
modifier = Modifier
.padding(padding)
.fillMaxWidth(),
shape = MaterialTheme.shapes.medium,
onClick = { navController.navigate(Screen.EndlessScreen.route) }
) {
Text(
"Vertical Endless List",
style = MaterialTheme.typography.bodyLarge
)
}

// Bottom Sheets
Button(
modifier = Modifier
.padding(padding)
.fillMaxWidth(),
shape = MaterialTheme.shapes.medium,
onClick = { navController.navigate(Screen.BottomSheetDialogScreen.route) }
) {
Text(
"Bottom Sheets Dialog",
style = MaterialTheme.typography.bodyLarge
)
}
}
}
}
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ buildscript {
ext.compose_version = '1.5.4' // compose.runtime
ext.compose_compiler = '1.4.4' // Release: https://developer.android.com/jetpack/androidx/releases/compose-kotlin
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.

plugins {
id 'com.android.application' version '8.1.4' apply false
id 'com.android.library' version '8.1.4' apply false
Expand All @@ -20,4 +20,5 @@ ext {
test_espresso = '3.5.1'
test_junit = '1.1.5'
junit_version = '4.13.2'
navigation_version = '2.7.7'
}
3 changes: 3 additions & 0 deletions me-design/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,7 @@ dependencies {

implementation "com.google.android.material:material:$material_version"
implementation "androidx.compose.material3:material3:$material3_version"

// Compose navigation
implementation("androidx.navigation:navigation-compose:$navigation_version")
}
10 changes: 0 additions & 10 deletions me-design/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application>
<activity
android:name=".screens.bottomNavAdaptive.AdaptiveNavActivity"
android:exported="false"
android:label="@string/title_activity_adaptive_nav"
android:theme="@style/Theme.MeSDK" />
<activity
android:name=".screens.ListActivity"
android:exported="false"
android:label="@string/title_activity_list"
android:theme="@style/Theme.MeSDK" />
<activity
android:name=".base.BaseActivity"
android:exported="false"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.rocqjones.me_design.screens

import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController

@Composable
fun BottomSheetDialogScreen(navController: NavHostController) {
Text(text = "BottomSheetDialogScreen")
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,33 @@
package com.rocqjones.me_design.screens

import android.content.res.Configuration
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.animateContentSize
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.spring
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.*
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.rocqjones.me_design.ui.theme.MeSDKTheme
import androidx.navigation.NavHostController
import com.rocqjones.me_design.R

class ListActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MeSDKTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize()
) {
GenerateLazyList()
}
}
}
}
@Composable
fun EndlessScreen(navController: NavHostController) {
GenerateLazyList()
}

@Composable
Expand Down Expand Up @@ -126,23 +114,4 @@ private fun CardContent(name: String) {
)
}
}
}

@Preview(
showBackground = true,
widthDp = 320,
name = "DefaultPreviewLight"
)
@Preview(
showBackground = true,
widthDp = 320,
uiMode = Configuration.UI_MODE_NIGHT_YES,
name = "DefaultPreviewDark"
)
@Preview(showBackground = true, widthDp = 320)
@Composable
fun DefaultPreview() {
MeSDKTheme {
GenerateLazyList()
}
}
Loading

0 comments on commit 715d362

Please sign in to comment.