Sfoglia il codice sorgente

Created usecase, viewmodel, repository for splash screen

Khubaib 1 mese fa
parent
commit
0630c7bff9

+ 5 - 0
app/build.gradle.kts

@@ -63,11 +63,16 @@ dependencies {
     implementation(libs.androidx.material3)
     implementation(libs.hilt.android)
     kapt(libs.hilt.compiler)
+    kapt(libs.hilt.navigation)
+    implementation(libs.lifecycle.viewmodel.compose)
+    implementation(libs.navigation.compose)
 //    implementation(libs.hilt.lifecycle)
+    implementation(libs.hilt.navigation)
     kapt(libs.androidx.hilt.compiler)
     implementation(libs.retrofit2.squareup)
     implementation(libs.retrofit.converter)
     implementation(libs.okhttp3.logging.interceptor)
+    implementation(libs.runtime.livedata)
 
     testImplementation(libs.junit)
     androidTestImplementation(libs.androidx.junit)

+ 4 - 15
app/src/main/java/com/fastest/pass/MainActivity.kt

@@ -11,6 +11,8 @@ import androidx.compose.material3.Text
 import androidx.compose.runtime.Composable
 import androidx.compose.ui.Modifier
 import androidx.compose.ui.tooling.preview.Preview
+import androidx.navigation.compose.rememberNavController
+import com.fastest.pass.splash.presentation.ui.SplashScreen
 import com.fastest.pass.ui.theme.FastestPassTheme
 import dagger.hilt.android.AndroidEntryPoint
 
@@ -21,29 +23,16 @@ class MainActivity : ComponentActivity() {
         enableEdgeToEdge()
         setContent {
             FastestPassTheme {
-                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
-                    Greeting(
-                        name = "Android",
-                        modifier = Modifier.padding(innerPadding)
-                    )
-                }
+                val navHostController = rememberNavController()
+                SplashScreen(navHostController = navHostController)
             }
         }
     }
 }
 
-@Composable
-fun Greeting(name: String, modifier: Modifier = Modifier) {
-    Text(
-        text = "Hello $name!",
-        modifier = modifier
-    )
-}
-
 @Preview(showBackground = true)
 @Composable
 fun GreetingPreview() {
     FastestPassTheme {
-        Greeting("Android")
     }
 }

+ 25 - 11
app/src/main/java/com/fastest/pass/di/AppModule.kt

@@ -1,19 +1,33 @@
 package com.fastest.pass.di
 
+import com.fastest.pass.splash.data.repository.SplashRepositoryImpl
+import com.fastest.pass.splash.data.source.remote.SplashApiService
+import com.fastest.pass.splash.domain.repository.SplashRepository
 import dagger.Module
 import dagger.Provides
 import dagger.hilt.InstallIn
 import dagger.hilt.components.SingletonComponent
+import retrofit2.Retrofit
+import retrofit2.converter.gson.GsonConverterFactory
 import javax.inject.Singleton
 
-//@InstallIn(SingletonComponent::class)
-//@Module
-//object AppModule {
-//
-//    @Singleton
-//    @Provides
-//    fun provideApiService() {
-//
-//    }
-//
-//}
+@Module
+@InstallIn(SingletonComponent::class)
+object AppModule {
+
+    @Singleton
+    @Provides
+    fun provideApiService(): SplashApiService {
+        return Retrofit.Builder()
+            .baseUrl("")
+            .addConverterFactory(GsonConverterFactory.create())
+            .build()
+            .create(SplashApiService::class.java)
+    }
+
+    @Singleton
+    @Provides
+    fun provideSplashRepository(splashApiService: SplashApiService): SplashRepository {
+        return SplashRepositoryImpl(splashApiService)
+    }
+}

+ 12 - 0
app/src/main/java/com/fastest/pass/splash/data/repository/SplashRepositoryImpl.kt

@@ -0,0 +1,12 @@
+package com.fastest.pass.splash.data.repository
+
+import com.fastest.pass.splash.data.source.remote.SplashApiService
+import com.fastest.pass.splash.domain.repository.SplashRepository
+
+class SplashRepositoryImpl(
+    private val splashApiService: SplashApiService
+): SplashRepository {
+    override suspend fun splashRepository(){
+
+    }
+}

+ 0 - 15
app/src/main/java/com/fastest/pass/splash/data/source/remote/ApiService.kt

@@ -1,15 +0,0 @@
-package com.fastest.pass.splash.data.source.remote
-
-import retrofit2.http.FormUrlEncoded
-import retrofit2.http.POST
-import retrofit2.http.Query
-import retrofit2.Call
-
-interface ApiService {
-
-    @FormUrlEncoded
-    @POST("login")
-    suspend fun login(
-        @Query("email") email: String?, @Query("password") password: String?
-    ): Call<Any?>
-}

+ 12 - 0
app/src/main/java/com/fastest/pass/splash/data/source/remote/SplashApiService.kt

@@ -0,0 +1,12 @@
+package com.fastest.pass.splash.data.source.remote
+
+import retrofit2.Call
+import retrofit2.http.FormUrlEncoded
+import retrofit2.http.GET
+
+interface SplashApiService {
+
+    @FormUrlEncoded
+    @GET("splash")
+    suspend fun splash(): Call<Any?>
+}

+ 8 - 0
app/src/main/java/com/fastest/pass/splash/domain/SplashUseCase.kt

@@ -0,0 +1,8 @@
+package com.fastest.pass.splash.domain
+
+import com.fastest.pass.splash.domain.repository.SplashRepository
+
+class SplashUseCase(
+    private val splashRepository: SplashRepository
+) {
+}

+ 5 - 0
app/src/main/java/com/fastest/pass/splash/domain/repository/SplashRepository.kt

@@ -0,0 +1,5 @@
+package com.fastest.pass.splash.domain.repository
+
+interface SplashRepository {
+    suspend fun splashRepository()
+}

+ 31 - 0
app/src/main/java/com/fastest/pass/splash/presentation/ui/SplashScreen.kt

@@ -0,0 +1,31 @@
+package com.fastest.pass.splash.presentation.ui
+
+import android.widget.Toast
+import android.window.SplashScreen
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.livedata.observeAsState
+import androidx.compose.ui.platform.LocalContext
+import androidx.hilt.navigation.compose.hiltViewModel
+import androidx.navigation.NavHostController
+import com.fastest.pass.splash.presentation.viewmodels.SplashViewModel
+import dagger.hilt.android.lifecycle.HiltViewModel
+
+@Composable
+fun SplashScreen(navHostController: NavHostController, splashViewModel: SplashViewModel = hiltViewModel()) {
+    val navigateToLogin by splashViewModel.navigateToLogin.observeAsState()
+    val context = LocalContext.current
+
+    LaunchedEffect(key1 = true) {
+        splashViewModel.checkAuthentication()
+    }
+
+    if (navigateToLogin == true) {
+        Toast.makeText(context, "Login", Toast.LENGTH_SHORT).show()
+    } else {
+        Text(text = "Splash")
+    }
+
+}

+ 23 - 0
app/src/main/java/com/fastest/pass/splash/presentation/viewmodels/SplashViewModel.kt

@@ -0,0 +1,23 @@
+package com.fastest.pass.splash.presentation.viewmodels
+
+import androidx.lifecycle.LiveData
+import androidx.lifecycle.MutableLiveData
+import androidx.lifecycle.ViewModel
+import androidx.lifecycle.viewModelScope
+import dagger.hilt.android.lifecycle.HiltViewModel
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.launch
+import javax.inject.Inject
+
+@HiltViewModel
+class SplashViewModel @Inject constructor() : ViewModel() {
+    private val _navigateToLogin = MutableLiveData<Boolean>(false)
+    val navigateToLogin: LiveData<Boolean> = _navigateToLogin
+
+    fun checkAuthentication() {
+        viewModelScope.launch {
+            delay(2000)
+            _navigateToLogin.value = true
+        }
+    }
+}

+ 11 - 4
gradle/libs.versions.toml

@@ -1,6 +1,6 @@
 [versions]
-agp = "8.5.0"
-hiltAndroid = "2.44"
+agp = "8.5.2"
+hiltAndroid = "2.45"
 kotlin = "1.9.0"
 coreKtx = "1.15.0"
 junit = "4.13.2"
@@ -11,16 +11,20 @@ activityCompose = "1.9.3"
 composeBom = "2024.04.01"
 org-jetbrains-kotlin-android = "1.9.0"
 hilt-lifecycle-viewmodel = "1.0.0-alpha03"
-androidx-hilt-lifecycle = "1.0.0"
+androidx-hilt-lifecycle = "1.2.0"
 retrofit-version = "2.9.0"
 okhttp3-interceptor = "4.9.0"
+lifecycle-viewmodel = "2.8.7"
+navigation-compose = "2.7.7"
+runtime-livedata = "1.7.5"
 
 [libraries]
 androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
 hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hiltAndroid" }
 hilt-compiler = { module = "com.google.dagger:hilt-compiler", version.ref = "hiltAndroid" }
 hilt-lifecycle = { module = "androidx.hilt:hilt-lifecycle-viewmodel", version.ref = "hilt-lifecycle-viewmodel" }
-androidx-hilt-compiler = {module = "androidx.hilt:hilt-compiler", version.ref = "androidx-hilt-lifecycle"}
+hilt-navigation = { module = "androidx.hilt:hilt-navigation-compose", version.ref = "androidx-hilt-lifecycle" }
+androidx-hilt-compiler = {module = "androidx.hilt:hilt-compiler", version.ref = "androidx-hilt-lifecycle" }
 junit = { group = "junit", name = "junit", version.ref = "junit" }
 androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
 androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
@@ -37,6 +41,9 @@ androidx-material3 = { group = "androidx.compose.material3", name = "material3"
 retrofit2-squareup = { module = "com.squareup.retrofit2:retrofit", version.ref = "retrofit-version" }
 retrofit-converter = { module = "com.squareup.retrofit2:converter-gson", version.ref = "retrofit-version" }
 okhttp3-logging-interceptor = { module = "com.squareup.okhttp3:logging-interceptor", version.ref = "okhttp3-interceptor"}
+lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "lifecycle-viewmodel" }
+navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "navigation-compose" }
+runtime-livedata = { module = "androidx.compose.runtime:runtime-livedata", version.ref = "runtime-livedata"}
 
 [plugins]
 android-application = { id = "com.android.application", version.ref = "agp" }