Add beginning of viewmodel, update nav on back gesture
This commit is contained in:
		
							
								
								
									
										41
									
								
								.idea/inspectionProfiles/Project_Default.xml
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								.idea/inspectionProfiles/Project_Default.xml
									
									
									
										generated
									
									
									
										Normal file
									
								
							@@ -0,0 +1,41 @@
 | 
				
			|||||||
 | 
					<component name="InspectionProjectProfileManager">
 | 
				
			||||||
 | 
					  <profile version="1.0">
 | 
				
			||||||
 | 
					    <option name="myName" value="Project Default" />
 | 
				
			||||||
 | 
					    <inspection_tool class="PreviewAnnotationInFunctionWithParameters" enabled="true" level="ERROR" enabled_by_default="true">
 | 
				
			||||||
 | 
					      <option name="composableFile" value="true" />
 | 
				
			||||||
 | 
					      <option name="previewFile" value="true" />
 | 
				
			||||||
 | 
					    </inspection_tool>
 | 
				
			||||||
 | 
					    <inspection_tool class="PreviewApiLevelMustBeValid" enabled="true" level="ERROR" enabled_by_default="true">
 | 
				
			||||||
 | 
					      <option name="composableFile" value="true" />
 | 
				
			||||||
 | 
					      <option name="previewFile" value="true" />
 | 
				
			||||||
 | 
					    </inspection_tool>
 | 
				
			||||||
 | 
					    <inspection_tool class="PreviewDimensionRespectsLimit" enabled="true" level="WARNING" enabled_by_default="true">
 | 
				
			||||||
 | 
					      <option name="composableFile" value="true" />
 | 
				
			||||||
 | 
					      <option name="previewFile" value="true" />
 | 
				
			||||||
 | 
					    </inspection_tool>
 | 
				
			||||||
 | 
					    <inspection_tool class="PreviewFontScaleMustBeGreaterThanZero" enabled="true" level="ERROR" enabled_by_default="true">
 | 
				
			||||||
 | 
					      <option name="composableFile" value="true" />
 | 
				
			||||||
 | 
					      <option name="previewFile" value="true" />
 | 
				
			||||||
 | 
					    </inspection_tool>
 | 
				
			||||||
 | 
					    <inspection_tool class="PreviewMultipleParameterProviders" enabled="true" level="ERROR" enabled_by_default="true">
 | 
				
			||||||
 | 
					      <option name="composableFile" value="true" />
 | 
				
			||||||
 | 
					      <option name="previewFile" value="true" />
 | 
				
			||||||
 | 
					    </inspection_tool>
 | 
				
			||||||
 | 
					    <inspection_tool class="PreviewMustBeTopLevelFunction" enabled="true" level="ERROR" enabled_by_default="true">
 | 
				
			||||||
 | 
					      <option name="composableFile" value="true" />
 | 
				
			||||||
 | 
					      <option name="previewFile" value="true" />
 | 
				
			||||||
 | 
					    </inspection_tool>
 | 
				
			||||||
 | 
					    <inspection_tool class="PreviewNeedsComposableAnnotation" enabled="true" level="ERROR" enabled_by_default="true">
 | 
				
			||||||
 | 
					      <option name="composableFile" value="true" />
 | 
				
			||||||
 | 
					      <option name="previewFile" value="true" />
 | 
				
			||||||
 | 
					    </inspection_tool>
 | 
				
			||||||
 | 
					    <inspection_tool class="PreviewNotSupportedInUnitTestFiles" enabled="true" level="ERROR" enabled_by_default="true">
 | 
				
			||||||
 | 
					      <option name="composableFile" value="true" />
 | 
				
			||||||
 | 
					      <option name="previewFile" value="true" />
 | 
				
			||||||
 | 
					    </inspection_tool>
 | 
				
			||||||
 | 
					    <inspection_tool class="PreviewPickerAnnotation" enabled="true" level="ERROR" enabled_by_default="true">
 | 
				
			||||||
 | 
					      <option name="composableFile" value="true" />
 | 
				
			||||||
 | 
					      <option name="previewFile" value="true" />
 | 
				
			||||||
 | 
					    </inspection_tool>
 | 
				
			||||||
 | 
					  </profile>
 | 
				
			||||||
 | 
					</component>
 | 
				
			||||||
							
								
								
									
										1
									
								
								.idea/misc.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										1
									
								
								.idea/misc.xml
									
									
									
										generated
									
									
									
								
							@@ -1,4 +1,3 @@
 | 
				
			|||||||
<?xml version="1.0" encoding="UTF-8"?>
 | 
					 | 
				
			||||||
<project version="4">
 | 
					<project version="4">
 | 
				
			||||||
  <component name="ExternalStorageConfigurationManager" enabled="true" />
 | 
					  <component name="ExternalStorageConfigurationManager" enabled="true" />
 | 
				
			||||||
  <component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
 | 
					  <component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -65,4 +65,5 @@ dependencies {
 | 
				
			|||||||
    debugImplementation(libs.androidx.ui.tooling)
 | 
					    debugImplementation(libs.androidx.ui.tooling)
 | 
				
			||||||
    debugImplementation(libs.androidx.ui.test.manifest)
 | 
					    debugImplementation(libs.androidx.ui.test.manifest)
 | 
				
			||||||
    implementation("androidx.navigation:navigation-compose:$nav_version")
 | 
					    implementation("androidx.navigation:navigation-compose:$nav_version")
 | 
				
			||||||
 | 
					    implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -4,6 +4,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    <application
 | 
					    <application
 | 
				
			||||||
        android:allowBackup="true"
 | 
					        android:allowBackup="true"
 | 
				
			||||||
 | 
					        android:enableOnBackInvokedCallback="true"
 | 
				
			||||||
        android:dataExtractionRules="@xml/data_extraction_rules"
 | 
					        android:dataExtractionRules="@xml/data_extraction_rules"
 | 
				
			||||||
        android:fullBackupContent="@xml/backup_rules"
 | 
					        android:fullBackupContent="@xml/backup_rules"
 | 
				
			||||||
        android:icon="@mipmap/ic_launcher"
 | 
					        android:icon="@mipmap/ic_launcher"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,6 +4,7 @@ import android.annotation.SuppressLint
 | 
				
			|||||||
import android.os.Bundle
 | 
					import android.os.Bundle
 | 
				
			||||||
import androidx.activity.ComponentActivity
 | 
					import androidx.activity.ComponentActivity
 | 
				
			||||||
import androidx.activity.compose.setContent
 | 
					import androidx.activity.compose.setContent
 | 
				
			||||||
 | 
					import com.ti.mobpo.ui.Navigation
 | 
				
			||||||
import com.ti.mobpo.ui.theme.MobileSecurityTheme
 | 
					import com.ti.mobpo.ui.theme.MobileSecurityTheme
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class MainActivity : ComponentActivity() {
 | 
					class MainActivity : ComponentActivity() {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,4 @@
 | 
				
			|||||||
package com.ti.mobpo
 | 
					package com.ti.mobpo.ui
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import androidx.compose.foundation.layout.fillMaxWidth
 | 
					import androidx.compose.foundation.layout.fillMaxWidth
 | 
				
			||||||
import androidx.compose.foundation.text.KeyboardActions
 | 
					import androidx.compose.foundation.text.KeyboardActions
 | 
				
			||||||
@@ -1,4 +1,4 @@
 | 
				
			|||||||
package com.ti.mobpo
 | 
					package com.ti.mobpo.ui
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import androidx.compose.foundation.layout.Box
 | 
					import androidx.compose.foundation.layout.Box
 | 
				
			||||||
import androidx.compose.foundation.layout.fillMaxSize
 | 
					import androidx.compose.foundation.layout.fillMaxSize
 | 
				
			||||||
@@ -20,16 +20,19 @@ import androidx.compose.material3.Text
 | 
				
			|||||||
import androidx.compose.runtime.Composable
 | 
					import androidx.compose.runtime.Composable
 | 
				
			||||||
import androidx.compose.runtime.getValue
 | 
					import androidx.compose.runtime.getValue
 | 
				
			||||||
import androidx.compose.runtime.mutableStateOf
 | 
					import androidx.compose.runtime.mutableStateOf
 | 
				
			||||||
 | 
					import androidx.compose.runtime.rememberUpdatedState
 | 
				
			||||||
import androidx.compose.runtime.saveable.rememberSaveable
 | 
					import androidx.compose.runtime.saveable.rememberSaveable
 | 
				
			||||||
import androidx.compose.runtime.setValue
 | 
					import androidx.compose.runtime.setValue
 | 
				
			||||||
import androidx.compose.ui.Modifier
 | 
					import androidx.compose.ui.Modifier
 | 
				
			||||||
import androidx.compose.ui.tooling.preview.Preview
 | 
					import androidx.compose.ui.tooling.preview.Preview
 | 
				
			||||||
import androidx.navigation.compose.NavHost
 | 
					import androidx.navigation.compose.NavHost
 | 
				
			||||||
import androidx.navigation.compose.composable
 | 
					import androidx.navigation.compose.composable
 | 
				
			||||||
 | 
					import androidx.navigation.compose.currentBackStackEntryAsState
 | 
				
			||||||
import androidx.navigation.compose.rememberNavController
 | 
					import androidx.navigation.compose.rememberNavController
 | 
				
			||||||
import com.ti.mobpo.screens.Favourites
 | 
					import com.ti.mobpo.ui.screens.Favourites
 | 
				
			||||||
import com.ti.mobpo.screens.PokeSearch
 | 
					import com.ti.mobpo.ui.screens.PokeSearch
 | 
				
			||||||
import com.ti.mobpo.screens.Profile
 | 
					import com.ti.mobpo.ui.screens.PokeSearchScreen
 | 
				
			||||||
 | 
					import com.ti.mobpo.ui.screens.Profile
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@Composable
 | 
					@Composable
 | 
				
			||||||
@@ -54,10 +57,17 @@ fun Navigation() {
 | 
				
			|||||||
            route = Screen.Profile.route
 | 
					            route = Screen.Profile.route
 | 
				
			||||||
        ),
 | 
					        ),
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    var selectedItemIndex by rememberSaveable {
 | 
					    var selectedItemIndex by rememberSaveable {
 | 
				
			||||||
        mutableStateOf(0)
 | 
					        mutableStateOf(0)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    val navController = rememberNavController()
 | 
					    val navController = rememberNavController()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    val currentBackStackEntry by navController.currentBackStackEntryAsState()
 | 
				
			||||||
 | 
					    val currentRoute = currentBackStackEntry?.destination?.route
 | 
				
			||||||
 | 
					    val updatedSelectedIndex = items.indexOfFirst { it.route == currentRoute }
 | 
				
			||||||
 | 
					    selectedItemIndex = rememberUpdatedState(updatedSelectedIndex).value
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Scaffold (
 | 
					    Scaffold (
 | 
				
			||||||
        bottomBar = {
 | 
					        bottomBar = {
 | 
				
			||||||
            NavigationBar {
 | 
					            NavigationBar {
 | 
				
			||||||
@@ -113,6 +123,6 @@ fun Navigation() {
 | 
				
			|||||||
@Composable
 | 
					@Composable
 | 
				
			||||||
fun NavigationPreview() {
 | 
					fun NavigationPreview() {
 | 
				
			||||||
    Surface {
 | 
					    Surface {
 | 
				
			||||||
        PokeSearch()
 | 
					        PokeSearchScreen()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
							
								
								
									
										3
									
								
								app/src/main/java/com/ti/mobpo/ui/PokeSearchUiState.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								app/src/main/java/com/ti/mobpo/ui/PokeSearchUiState.kt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,3 @@
 | 
				
			|||||||
 | 
					package com.ti.mobpo.ui
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					data class PokeSearchUiState(val searchQuery: String = "")
 | 
				
			||||||
							
								
								
									
										11
									
								
								app/src/main/java/com/ti/mobpo/ui/PokeSearchViewModel.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								app/src/main/java/com/ti/mobpo/ui/PokeSearchViewModel.kt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
				
			|||||||
 | 
					package com.ti.mobpo.ui
 | 
				
			||||||
 | 
					import androidx.lifecycle.ViewModel
 | 
				
			||||||
 | 
					import kotlinx.coroutines.flow.MutableStateFlow
 | 
				
			||||||
 | 
					import kotlinx.coroutines.flow.StateFlow
 | 
				
			||||||
 | 
					import kotlinx.coroutines.flow.asStateFlow
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class PokeSearchViewModel : ViewModel() {
 | 
				
			||||||
 | 
					    private val _uiState = MutableStateFlow(PokeSearchUiState())
 | 
				
			||||||
 | 
					    val uiState: StateFlow<PokeSearchUiState> = _uiState.asStateFlow()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,4 +1,4 @@
 | 
				
			|||||||
package com.ti.mobpo
 | 
					package com.ti.mobpo.ui
 | 
				
			||||||
 | 
					
 | 
				
			||||||
sealed class Screen (val route: String) {
 | 
					sealed class Screen (val route: String) {
 | 
				
			||||||
    object PokeSearch : Screen("default_screen")
 | 
					    object PokeSearch : Screen("default_screen")
 | 
				
			||||||
@@ -1,4 +1,4 @@
 | 
				
			|||||||
package com.ti.mobpo.screens
 | 
					package com.ti.mobpo.ui.screens
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import androidx.compose.material3.Text
 | 
					import androidx.compose.material3.Text
 | 
				
			||||||
import androidx.compose.runtime.Composable
 | 
					import androidx.compose.runtime.Composable
 | 
				
			||||||
@@ -1,25 +1,36 @@
 | 
				
			|||||||
package com.ti.mobpo.screens
 | 
					package com.ti.mobpo.ui.screens
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import androidx.compose.foundation.layout.Arrangement
 | 
					import androidx.compose.foundation.layout.Arrangement
 | 
				
			||||||
import androidx.compose.foundation.layout.Column
 | 
					import androidx.compose.foundation.layout.Column
 | 
				
			||||||
import androidx.compose.foundation.layout.fillMaxSize
 | 
					import androidx.compose.foundation.layout.fillMaxSize
 | 
				
			||||||
import androidx.compose.foundation.layout.padding
 | 
					import androidx.compose.foundation.layout.padding
 | 
				
			||||||
import androidx.compose.runtime.Composable
 | 
					import androidx.compose.runtime.Composable
 | 
				
			||||||
 | 
					import androidx.compose.runtime.collectAsState
 | 
				
			||||||
 | 
					import androidx.compose.runtime.getValue
 | 
				
			||||||
import androidx.compose.ui.Alignment
 | 
					import androidx.compose.ui.Alignment
 | 
				
			||||||
import androidx.compose.ui.Modifier
 | 
					import androidx.compose.ui.Modifier
 | 
				
			||||||
import androidx.compose.ui.res.stringResource
 | 
					import androidx.compose.ui.res.stringResource
 | 
				
			||||||
import androidx.compose.ui.tooling.preview.Preview
 | 
					import androidx.compose.ui.tooling.preview.Preview
 | 
				
			||||||
import androidx.compose.ui.unit.dp
 | 
					import androidx.compose.ui.unit.dp
 | 
				
			||||||
import com.ti.mobpo.R
 | 
					import com.ti.mobpo.R
 | 
				
			||||||
import com.ti.mobpo.SearchBar
 | 
					import com.ti.mobpo.ui.SearchBar
 | 
				
			||||||
 | 
					import androidx.lifecycle.viewmodel.compose.viewModel
 | 
				
			||||||
 | 
					import com.ti.mobpo.ui.PokeSearchViewModel
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Composable
 | 
				
			||||||
 | 
					fun PokeSearchScreen(pokesearchViewModel: PokeSearchViewModel = viewModel()){
 | 
				
			||||||
 | 
					    val pokesearchUiState by pokesearchViewModel.uiState.collectAsState()
 | 
				
			||||||
 | 
					    PokeSearch()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@Composable
 | 
					@Composable
 | 
				
			||||||
fun PokeSearch() {
 | 
					fun PokeSearch() {
 | 
				
			||||||
    Column (
 | 
					    Column (
 | 
				
			||||||
        verticalArrangement = Arrangement.Top,
 | 
					        verticalArrangement = Arrangement.Top,
 | 
				
			||||||
        horizontalAlignment = Alignment.CenterHorizontally,
 | 
					        horizontalAlignment = Alignment.CenterHorizontally,
 | 
				
			||||||
        modifier = Modifier.fillMaxSize().padding(20.dp)
 | 
					        modifier = Modifier
 | 
				
			||||||
 | 
					            .fillMaxSize()
 | 
				
			||||||
 | 
					            .padding(20.dp)
 | 
				
			||||||
    ) {
 | 
					    ) {
 | 
				
			||||||
        SearchBar(stringResource(R.string.input_field_label)){ value ->
 | 
					        SearchBar(stringResource(R.string.input_field_label)){ value ->
 | 
				
			||||||
            run {
 | 
					            run {
 | 
				
			||||||
@@ -31,5 +42,5 @@ fun PokeSearch() {
 | 
				
			|||||||
@Preview(showBackground = true)
 | 
					@Preview(showBackground = true)
 | 
				
			||||||
@Composable
 | 
					@Composable
 | 
				
			||||||
fun PokeSearchApp(){
 | 
					fun PokeSearchApp(){
 | 
				
			||||||
    PokeSearch()
 | 
					    PokeSearchScreen()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -1,4 +1,4 @@
 | 
				
			|||||||
package com.ti.mobpo.screens
 | 
					package com.ti.mobpo.ui.screens
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import androidx.compose.material3.Text
 | 
					import androidx.compose.material3.Text
 | 
				
			||||||
import androidx.compose.runtime.Composable
 | 
					import androidx.compose.runtime.Composable
 | 
				
			||||||
		Reference in New Issue
	
	Block a user