Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

docs: update example

Hieu Vu84f9b71a1548
diff --git a/examples/product-sample-supabase-kt/app/.gitignore b/examples/product-sample-supabase-kt/app/.gitignore
deleted file mode 100644
index 42afabfd2ab..00000000000
--- a/examples/product-sample-supabase-kt/app/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-/build
\ No newline at end of file
diff --git a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/data/repository/AuthenticationRepository.kt b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/data/repository/AuthenticationRepository.kt
new file mode 100644
index 00000000000..edac2c23a24
--- /dev/null
+++ b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/data/repository/AuthenticationRepository.kt
@@ -0,0 +1,6 @@
+package com.example.manageproducts.data.repository
+
+interface AuthenticationRepository {
+ suspend fun signIn(email: String, password: String): Boolean
+ suspend fun signUp(email: String, password: String): Boolean
+}
\ No newline at end of file
diff --git a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/data/repository/impl/AuthenticationRepositoryImpl.kt b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/data/repository/impl/AuthenticationRepositoryImpl.kt
new file mode 100644
index 00000000000..68535d45eda
--- /dev/null
+++ b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/data/repository/impl/AuthenticationRepositoryImpl.kt
@@ -0,0 +1,34 @@
+package com.example.manageproducts.data.repository.impl
+
+import com.example.manageproducts.data.repository.AuthenticationRepository
+import io.github.jan.supabase.gotrue.GoTrue
+import io.github.jan.supabase.gotrue.providers.builtin.Email
+import javax.inject.Inject
+
+class AuthenticationRepositoryImpl @Inject constructor(
+ private val goTrue: GoTrue
+) : AuthenticationRepository {
+ override suspend fun signIn(email: String, password: String): Boolean {
+ return try {
+ goTrue.loginWith(Email) {
+ this.email = email
+ this.password = password
+ }
+ true
+ } catch (e: Exception) {
+ false
+ }
+ }
+
+ override suspend fun signUp(email: String, password: String): Boolean {
+ return try {
+ goTrue.signUpWith(Email) {
+ this.email = email
+ this.password = password
+ }
+ true
+ } catch (e: Exception) {
+ false
+ }
+ }
+}
\ No newline at end of file
diff --git a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/data/repository/impl/ProductRepositoryImpl.kt b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/data/repository/impl/ProductRepositoryImpl.kt
index 058ea96d636..7e052507057 100644
--- a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/data/repository/impl/ProductRepositoryImpl.kt
+++ b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/data/repository/impl/ProductRepositoryImpl.kt
@@ -6,8 +6,6 @@ import com.example.manageproducts.data.repository.ProductRepository
import com.example.manageproducts.domain.model.Product
import io.github.jan.supabase.postgrest.Postgrest
import io.github.jan.supabase.storage.Storage
-import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.withContext
import javax.inject.Inject
class ProductRepositoryImpl @Inject constructor(
@@ -16,14 +14,11 @@ class ProductRepositoryImpl @Inject constructor(
) : ProductRepository {
override suspend fun createProduct(product: Product): Boolean {
return try {
- withContext(Dispatchers.IO) {
- val productDto = ProductDto(
- name = product.name,
- price = product.price,
- )
- postgrest["products"].insert(productDto)
- true
- }
+ val productDto = ProductDto(
+ name = product.name,
+ price = product.price,
+ )
+ postgrest["products"].insert(productDto)
true
} catch (e: java.lang.Exception) {
throw e
@@ -31,27 +26,21 @@ class ProductRepositoryImpl @Inject constructor(
}
override suspend fun getProducts(): List<ProductDto>? {
- return withContext(Dispatchers.IO) {
- val result = postgrest["products"]
- .select().decodeList<ProductDto>()
- result
- }
+ val result = postgrest["products"]
+ .select().decodeList<ProductDto>()
+ return result
}
override suspend fun getProduct(id: String): ProductDto {
- return withContext(Dispatchers.IO) {
- postgrest["products"].select {
- eq("id", id)
- }.decodeSingle<ProductDto>()
- }
+ return postgrest["products"].select {
+ eq("id", id)
+ }.decodeSingle<ProductDto>()
}
override suspend fun deleteProduct(id: String) {
- return withContext(Dispatchers.IO) {
- postgrest["products"].delete {
- eq("id", id)
- }
+ postgrest["products"].delete {
+ eq("id", id)
}
}
@@ -62,29 +51,26 @@ class ProductRepositoryImpl @Inject constructor(
imageName: String,
imageFile: ByteArray
) {
- withContext(Dispatchers.IO) {
-
- if (imageFile.isNotEmpty()) {
- val imageUrl =
- storage["Product%20Image"].upload(
- path = "$imageName.png",
- data = imageFile,
- upsert = true
- )
- postgrest["products"].update({
- set("name", name)
- set("price", price)
- set("image", buildImageUrl(imageFileName = imageUrl))
- }) {
- eq("id", id)
- }
- } else {
- postgrest["products"].update({
- set("name", name)
- set("price", price)
- }) {
- eq("id", id)
- }
+ if (imageFile.isNotEmpty()) {
+ val imageUrl =
+ storage["Product%20Image"].upload(
+ path = "$imageName.png",
+ data = imageFile,
+ upsert = true
+ )
+ postgrest["products"].update({
+ set("name", name)
+ set("price", price)
+ set("image", buildImageUrl(imageFileName = imageUrl))
+ }) {
+ eq("id", id)
+ }
+ } else {
+ postgrest["products"].update({
+ set("name", name)
+ set("price", price)
+ }) {
+ eq("id", id)
}
}
}
diff --git a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/di/RepositoryModule.kt b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/di/RepositoryModule.kt
index 25f416f383f..fac3c1deea3 100644
--- a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/di/RepositoryModule.kt
+++ b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/di/RepositoryModule.kt
@@ -1,7 +1,9 @@
package com.example.manageproducts.di
+import com.example.manageproducts.data.repository.AuthenticationRepository
import com.example.manageproducts.data.repository.ImageRepository
import com.example.manageproducts.data.repository.ProductRepository
+import com.example.manageproducts.data.repository.impl.AuthenticationRepositoryImpl
import com.example.manageproducts.data.repository.impl.ImageRepositoryImpl
import com.example.manageproducts.data.repository.impl.ProductRepositoryImpl
import dagger.Binds
@@ -19,4 +21,7 @@ abstract class RepositoryModule {
@Binds
abstract fun bindImageRepository(impl: ImageRepositoryImpl): ImageRepository
+ @Binds
+ abstract fun bindAuthenticateRepository(impl: AuthenticationRepositoryImpl): AuthenticationRepository
+
}
\ No newline at end of file
diff --git a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/di/UseCaseModule.kt b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/di/UseCaseModule.kt
index fdc1af1de4d..05a246c50d8 100644
--- a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/di/UseCaseModule.kt
+++ b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/di/UseCaseModule.kt
@@ -28,4 +28,10 @@ abstract class UseCaseModule {
@Binds
abstract fun bindUploadImageUseCaseImpl(impl: UploadImageUseCaseImpl): UploadImageUseCase
+
+ @Binds
+ abstract fun bindAuthenticateUseCase(impl: SignInUseCaseImpl): SignInUseCase
+
+ @Binds
+ abstract fun bindSignUpUseCase(impl: SignUpUseCaseImpl): SignUpUseCase
}
\ No newline at end of file
diff --git a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/domain/usecase/SignInUseCase.kt b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/domain/usecase/SignInUseCase.kt
new file mode 100644
index 00000000000..aab4986f405
--- /dev/null
+++ b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/domain/usecase/SignInUseCase.kt
@@ -0,0 +1,9 @@
+package com.example.manageproducts.domain.usecase
+
+interface SignInUseCase : UseCase<SignInUseCase.Input, SignInUseCase.Output> {
+ class Input(val email: String, val password: String)
+ sealed class Output() {
+ object Success : Output()
+ object Failure : Output()
+ }
+}
\ No newline at end of file
diff --git a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/domain/usecase/SignUpUseCase.kt b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/domain/usecase/SignUpUseCase.kt
new file mode 100644
index 00000000000..05d89b4e15a
--- /dev/null
+++ b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/domain/usecase/SignUpUseCase.kt
@@ -0,0 +1,9 @@
+package com.example.manageproducts.domain.usecase
+
+interface SignUpUseCase: UseCase<SignUpUseCase.Input, SignUpUseCase.Output> {
+ class Input(val email: String, val password: String)
+ sealed class Output {
+ object Success: Output()
+ object Failure: Output()
+ }
+}
\ No newline at end of file
diff --git a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/domain/usecase/impl/GetProductsUseCaseImpl.kt b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/domain/usecase/impl/GetProductsUseCaseImpl.kt
index bf36d7bbb07..bea6ea0d52f 100644
--- a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/domain/usecase/impl/GetProductsUseCaseImpl.kt
+++ b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/domain/usecase/impl/GetProductsUseCaseImpl.kt
@@ -1,6 +1,5 @@
package com.example.manageproducts.domain.usecase.impl
-import com.example.manageproducts.data.network.dto.ProductDto
import com.example.manageproducts.data.repository.ProductRepository
import com.example.manageproducts.domain.model.Product
import com.example.manageproducts.domain.usecase.GetProductsUseCase
@@ -25,5 +24,4 @@ class GetProductsUseCaseImpl @Inject constructor(
})
} ?: GetProductsUseCase.Output.Failure
}
-}
-
+}
\ No newline at end of file
diff --git a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/domain/usecase/impl/SignInUseCaseImpl.kt b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/domain/usecase/impl/SignInUseCaseImpl.kt
new file mode 100644
index 00000000000..ac94b37200d
--- /dev/null
+++ b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/domain/usecase/impl/SignInUseCaseImpl.kt
@@ -0,0 +1,22 @@
+package com.example.manageproducts.domain.usecase.impl
+
+import com.example.manageproducts.data.repository.AuthenticationRepository
+import com.example.manageproducts.domain.usecase.SignInUseCase
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
+import javax.inject.Inject
+
+class SignInUseCaseImpl @Inject constructor(
+ private val authenticationRepository: AuthenticationRepository
+) : SignInUseCase {
+ override suspend fun execute(input: SignInUseCase.Input): SignInUseCase.Output {
+ return withContext(Dispatchers.IO) {
+ val result = authenticationRepository.signIn(input.email, input.password)
+ if (result) {
+ SignInUseCase.Output.Success
+ } else {
+ SignInUseCase.Output.Failure
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/domain/usecase/impl/SignUpUseCaseImpl.kt b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/domain/usecase/impl/SignUpUseCaseImpl.kt
new file mode 100644
index 00000000000..fc60fcb9195
--- /dev/null
+++ b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/domain/usecase/impl/SignUpUseCaseImpl.kt
@@ -0,0 +1,21 @@
+package com.example.manageproducts.domain.usecase.impl
+
+import com.example.manageproducts.data.repository.AuthenticationRepository
+import com.example.manageproducts.domain.usecase.SignUpUseCase
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
+import javax.inject.Inject
+
+class SignUpUseCaseImpl @Inject constructor(
+ private val authenticationRepository: AuthenticationRepository
+) : SignUpUseCase {
+ override suspend fun execute(input: SignUpUseCase.Input): SignUpUseCase.Output =
+ withContext(Dispatchers.IO) {
+ val result = authenticationRepository.signUp(input.email, input.password)
+ if (result) {
+ SignUpUseCase.Output.Success
+ } else {
+ SignUpUseCase.Output.Failure
+ }
+ }
+}
\ No newline at end of file
diff --git a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/addproduct/AddProductScreen.kt b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/addproduct/AddProductScreen.kt
index 7e4584956fa..68b6abdd10b 100644
--- a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/addproduct/AddProductScreen.kt
+++ b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/addproduct/AddProductScreen.kt
@@ -60,70 +60,130 @@ fun AddProductScreen(
)
}
) { padding ->
- Column(
- modifier = modifier
- .padding(padding)
- .padding(16.dp)
- .fillMaxSize()
- ) {
- val name = rememberSaveable { mutableStateOf("") }
- val price = rememberSaveable { mutableStateOf("") }
- OutlinedTextField(
- label = {
- Text(
- text = "Product name",
- color = MaterialTheme.colorScheme.primary,
- style = MaterialTheme.typography.titleMedium
- )
- },
- maxLines = 2,
- shape = RoundedCornerShape(32),
- modifier = modifier.fillMaxWidth(),
- value = name.value,
- onValueChange = {
- name.value = it
- },
- )
- Spacer(modifier = modifier.height(12.dp))
- OutlinedTextField(
- label = {
- Text(
- text = "Product price",
- color = MaterialTheme.colorScheme.primary,
- style = MaterialTheme.typography.titleMedium
- )
- },
- maxLines = 2,
- shape = RoundedCornerShape(32),
- modifier = modifier.fillMaxWidth(),
- value = price.value,
- keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
- onValueChange = {
- price.value = it
- },
- )
- Spacer(modifier = modifier.height(12.dp))
- Spacer(modifier = modifier.weight(1f))
- OutlinedButton(
- modifier = modifier
- .fillMaxWidth(),
- onClick = {
+ val navigateAddProductSuccess =
+ viewModel.navigateAddProductSuccess.collectAsState(initial = null).value
+ val isLoading =
+ viewModel.isLoading.collectAsState(initial = null).value
+ if (isLoading == true) {
+ LoadingScreen(message = "Adding Product",
+ onCancelSelected = {
navController.navigateUp()
- }) {
- Text(text = "Cancel")
- }
- Spacer(modifier = modifier.height(12.dp))
- Button(
- modifier = modifier.fillMaxWidth(),
- onClick = {
- viewModel.onCreateProduct(
- name = name.value,
- price = if (price.value.isEmpty()) 0.0 else price.value.trim()
- .toDouble(),
+ })
+ } else {
+ when (navigateAddProductSuccess) {
+ null -> {
+ Column(
+ modifier = modifier
+ .padding(padding)
+ .padding(16.dp)
+ .fillMaxSize()
+ ) {
+ val name = rememberSaveable { mutableStateOf("") }
+ val price = rememberSaveable { mutableStateOf("") }
+ OutlinedTextField(
+ label = {
+ Text(
+ text = "Product name",
+ color = MaterialTheme.colorScheme.primary,
+ style = MaterialTheme.typography.titleMedium
+ )
+ },
+ maxLines = 2,
+ shape = RoundedCornerShape(32),
+ modifier = modifier.fillMaxWidth(),
+ value = name.value,
+ onValueChange = {
+ name.value = it
+ },
+ )
+ Spacer(modifier = modifier.height(12.dp))
+ OutlinedTextField(
+ label = {
+ Text(
+ text = "Product price",
+ color = MaterialTheme.colorScheme.primary,
+ style = MaterialTheme.typography.titleMedium
+ )
+ },
+ maxLines = 2,
+ shape = RoundedCornerShape(32),
+ modifier = modifier.fillMaxWidth(),
+ value = price.value,
+ keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
+ onValueChange = {
+ price.value = it
+ },
+ )
+ Spacer(modifier = modifier.height(12.dp))
+ Spacer(modifier = modifier.weight(1f))
+ OutlinedButton(
+ modifier = modifier
+ .fillMaxWidth(),
+ onClick = {
+ navController.navigateUp()
+ }) {
+ Text(text = "Cancel")
+ }
+ Spacer(modifier = modifier.height(12.dp))
+ Button(
+ modifier = modifier.fillMaxWidth(),
+ onClick = {
+ viewModel.onCreateProduct(
+ name = name.value,
+ price = if (price.value.isEmpty()) 0.0 else price.value.trim()
+ .toDouble(),
+ )
+ }) {
+ Text(text = "Add Product")
+ }
+ }
+ }
+ is CreateProductUseCase.Output.Success -> {
+ SuccessScreen(
+ message = "Product added",
+ onMoreAction = {
+ viewModel.onAddMoreProductSelected()
+ },
+ onNavigateBack = {
+ navController.navigateUp()
+ })
+ }
+ is CreateProductUseCase.Output.Failure -> {
+ val resonMessage = rememberSaveable {
+ mutableStateOf(handleError(navigateAddProductSuccess))
+ }
+ FailScreen(modifier = modifier.padding(16.dp),
+ message = "Fail to Add Product",
+ reason = resonMessage.value,
+ onRetrySelected = {
+ viewModel.onRetrySelected()
+ },
+ onNavigateBack = { navController.navigateUp() }
)
- }) {
- Text(text = "Add Product")
+ }
+
}
}
+
}
}
+
+private fun handleError(failResult: CreateProductUseCase.Output.Failure): String {
+ return when (failResult) {
+ is CreateProductUseCase.Output.Failure.InternalError -> {
+ "Internal Error"
+ }
+ is CreateProductUseCase.Output.Failure.BadRequest -> {
+ "Bad request"
+ }
+ is CreateProductUseCase.Output.Failure.Conflict -> {
+ "Conflict"
+ }
+ is CreateProductUseCase.Output.Failure.Unauthorized -> {
+ "Unauthorized"
+ }
+ else -> {
+ "Internal Error"
+ }
+ }
+}
\ No newline at end of file
diff --git a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/addproduct/AddProductViewModel.kt b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/addproduct/AddProductViewModel.kt
index aa0ff925d8b..e78d57fb38c 100644
--- a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/addproduct/AddProductViewModel.kt
+++ b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/addproduct/AddProductViewModel.kt
@@ -2,7 +2,6 @@ package com.example.manageproducts.presentation.feature.addproduct
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
-import com.example.manageproducts.data.repository.ProductRepository
import com.example.manageproducts.domain.model.Product
import com.example.manageproducts.domain.usecase.CreateProductUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
@@ -14,13 +13,19 @@ import javax.inject.Inject
@HiltViewModel
class AddProductViewModel @Inject constructor(
- private val productRepository: ProductRepository,
-) : ViewModel() {
+ private val createProductUseCase: CreateProductUseCase,
+) : ViewModel(), AddProductContract {
+
+ private val _navigateAddProductSuccess = MutableStateFlow<CreateProductUseCase.Output?>(null)
+ override val navigateAddProductSuccess: Flow<CreateProductUseCase.Output?> =
+ _navigateAddProductSuccess
private val _isLoading = MutableStateFlow(false)
- val isLoading: Flow<Boolean> = _isLoading
+ override val isLoading: Flow<Boolean> = _isLoading
- fun onCreateProduct(name: String, price: Double) {
+ private val _showSuccessMessage = MutableStateFlow(false)
+ override val showSuccessMessage: Flow<Boolean> = _showSuccessMessage
+ override fun onCreateProduct(name: String, price: Double) {
if (name.isEmpty() || price <= 0) return
viewModelScope.launch {
_isLoading.value = true
@@ -29,8 +34,27 @@ class AddProductViewModel @Inject constructor(
name = name,
price = price,
)
- productRepository.createProduct(product = product)
- _isLoading.value = false
+ when (val result =
+ createProductUseCase.execute(CreateProductUseCase.Input(product = product))) {
+ is CreateProductUseCase.Output.Success -> {
+ _isLoading.value = false
+ _showSuccessMessage.emit(true)
+ _navigateAddProductSuccess.value = result
+ }
+ is CreateProductUseCase.Output.Failure -> {
+ _isLoading.value = false
+ _navigateAddProductSuccess.value = result
+ }
+ }
+
}
}
+
+ override fun onAddMoreProductSelected() {
+ _navigateAddProductSuccess.value = null
+ }
+
+ override fun onRetrySelected() {
+ _navigateAddProductSuccess.value = null
+ }
}
\ No newline at end of file
diff --git a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/productdetails/ProductDetailsViewModel.kt b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/productdetails/ProductDetailsViewModel.kt
index bafa82a7cb8..3347cf9b00d 100644
--- a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/productdetails/ProductDetailsViewModel.kt
+++ b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/productdetails/ProductDetailsViewModel.kt
@@ -3,8 +3,6 @@ package com.example.manageproducts.presentation.feature.productdetails
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
-import com.example.manageproducts.data.network.dto.ProductDto
-import com.example.manageproducts.data.repository.ProductRepository
import com.example.manageproducts.domain.model.Product
import com.example.manageproducts.domain.usecase.GetProductDetailsUseCase
import com.example.manageproducts.domain.usecase.UpdateProductUseCase
@@ -18,7 +16,9 @@ import javax.inject.Inject
@HiltViewModel
class ProductDetailsViewModel @Inject constructor(
- private val productRepository: ProductRepository,
+ private val getProductDetailsUseCase: GetProductDetailsUseCase,
+ private val updateProductUseCase: UpdateProductUseCase,
+ private val uploadImageUseCase: UploadImageUseCase,
savedStateHandle: SavedStateHandle,
) : ViewModel(), ProductDetailsContract {
@@ -43,10 +43,22 @@ class ProductDetailsViewModel @Inject constructor(
private fun getProduct(productId: String) {
viewModelScope.launch {
- val result = productRepository.getProduct(productId).asDomainModel()
- _product.emit(result)
- _name.emit(result.name)
- _price.emit(result.price)
+ val result = getProductDetailsUseCase.execute(
+ GetProductDetailsUseCase.Input(
+ id = productId
+ )
+ )
+ when (result) {
+ is GetProductDetailsUseCase.Output.Success -> {
+ _product.emit(result.data)
+ _name.emit(result.data.name)
+ _price.emit(result.data.price)
+ _imageUrl.emit(result.data.image)
+ }
+ is GetProductDetailsUseCase.Output.Failure -> {
+
+ }
+ }
}
}
@@ -60,26 +72,27 @@ class ProductDetailsViewModel @Inject constructor(
override fun onSaveProduct(image: ByteArray) {
viewModelScope.launch {
- productRepository.updateProduct(
- id = _product.value?.id ?: "",
- price = _price.value,
- name = _name.value,
- imageFile = image,
- imageName = "image_${_product.value?.id}",
+ updateProductUseCase.execute(
+ UpdateProductUseCase.Input(
+ id = _product.value?.id ?: "",
+ price = _price.value,
+ name = _name.value,
+ imageFile = image,
+ imageName = "image_${_product.value?.id}",
+ )
)
+ if (image.isNotEmpty()) {
+// uploadImageUseCase.execute(
+// UploadImageUseCase.Input(
+// "image_${_product.value?.id}",
+// imageByteArray = image
+// )
+// )
+ }
}
}
override fun onImageChange(url: String) {
_imageUrl.value = url
}
-
- private fun ProductDto.asDomainModel(): Product {
- return Product(
- id = this.id ?: "",
- name = this.name,
- price = this.price,
- image = this.image ?: ""
- )
- }
}
\ No newline at end of file
diff --git a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/productlist/ProductListScreen.kt b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/productlist/ProductListScreen.kt
index c09813df30d..aab33646ed2 100644
--- a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/productlist/ProductListScreen.kt
+++ b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/productlist/ProductListScreen.kt
@@ -3,10 +3,7 @@ package com.example.manageproducts.presentation.feature.productlist
import android.annotation.SuppressLint
import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.background
-import androidx.compose.foundation.layout.Box
-import androidx.compose.foundation.layout.PaddingValues
-import androidx.compose.foundation.layout.fillMaxSize
-import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material.*
@@ -18,7 +15,6 @@ import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
-import com.example.manageproducts.R
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
@@ -32,11 +28,14 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.navigation.NavController
-import com.google.accompanist.swiperefresh.SwipeRefresh
-import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
+import com.example.manageproducts.R
import com.example.manageproducts.presentation.navigation.AddProductDestination
+import com.example.manageproducts.presentation.navigation.AuthenticationDestination
import com.example.manageproducts.presentation.navigation.ProductDetailsDestination
+import com.google.accompanist.swiperefresh.SwipeRefresh
+import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
+@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterialApi::class)
@Composable
fun ProductListScreen(
@@ -46,6 +45,24 @@ fun ProductListScreen(
) {
val isLoading by viewModel.isLoading.collectAsState(initial = false)
val swipeRefreshState = rememberSwipeRefreshState(isRefreshing = isLoading)
+ val lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current
+ // If `lifecycleOwner` changes, dispose and reset the effect
+ DisposableEffect(lifecycleOwner) {
+ // Create an observer that triggers our remembered callbacks
+ // for sending analytics events
+ val observer = LifecycleEventObserver { _, event ->
+ if (event == Lifecycle.Event.ON_START) {
+ viewModel.getProducts()
+ }
+ }
+ // Add the observer to the lifecycle
+ lifecycleOwner.lifecycle.addObserver(observer)
+
+ // When the effect leaves the Composition, remove the observer
+ onDispose {
+ lifecycleOwner.lifecycle.removeObserver(observer)
+ }
+ }
SwipeRefresh(state = swipeRefreshState, onRefresh = { viewModel.getProducts() }) {
Scaffold(
topBar = {
@@ -63,72 +80,81 @@ fun ProductListScreen(
AddProductButton(onClick = { navController.navigate(AddProductDestination.route) })
}
) { padding ->
- val productList = viewModel.productList.collectAsState(initial = listOf()).value
- if (!productList.isNullOrEmpty()) {
- LazyColumn(
- modifier = modifier.padding(padding),
- contentPadding = PaddingValues(5.dp)
- ) {
- itemsIndexed(
- items = productList,
- key = { _, product -> product.name }) { _, item ->
- val state = rememberDismissState(
- confirmStateChange = {
- if (it == DismissValue.DismissedToStart) {
- // Handle item removed
- viewModel.removeItem(item)
+ Column(modifier = modifier.padding(paddingValues = padding)) {
+ androidx.compose.material3.Button(modifier = modifier.fillMaxWidth().padding(20.dp), onClick = {
+ navController.navigate(AuthenticationDestination.route)
+ }) {
+ Text("Authentication feature")
+ }
+
+ val productList = viewModel.productList.collectAsState(initial = listOf()).value
+ if (!productList.isNullOrEmpty()) {
+ LazyColumn(
+ modifier = modifier.padding(padding),
+ contentPadding = PaddingValues(5.dp)
+ ) {
+ itemsIndexed(
+ items = productList,
+ key = { _, product -> product.name }) { _, item ->
+ val state = rememberDismissState(
+ confirmStateChange = {
+ if (it == DismissValue.DismissedToStart) {
+ // Handle item removed
+ viewModel.removeItem(item)
+ }
+ true
}
- true
- }
- )
- SwipeToDismiss(
- state = state,
- background = {
- val color by animateColorAsState(
- targetValue = when (state.dismissDirection) {
- DismissDirection.StartToEnd -> MaterialTheme.colorScheme.primary
- DismissDirection.EndToStart -> MaterialTheme.colorScheme.primary.copy(
- alpha = 0.2f
+ )
+ SwipeToDismiss(
+ state = state,
+ background = {
+ val color by animateColorAsState(
+ targetValue = when (state.dismissDirection) {
+ DismissDirection.StartToEnd -> MaterialTheme.colorScheme.primary
+ DismissDirection.EndToStart -> MaterialTheme.colorScheme.primary.copy(
+ alpha = 0.2f
+ )
+ null -> Color.Transparent
+ }
+ )
+ Box(
+ modifier = modifier
+ .fillMaxSize()
+ .background(color = color)
+ .padding(16.dp),
+ ) {
+ Icon(
+ imageVector = Icons.Filled.Delete,
+ contentDescription = null,
+ tint = MaterialTheme.colorScheme.primary,
+ modifier = modifier.align(Alignment.CenterEnd)
)
- null -> Color.Transparent
}
- )
- Box(
- modifier = modifier
- .fillMaxSize()
- .background(color = color)
- .padding(16.dp),
- ) {
- Icon(
- imageVector = Icons.Filled.Delete,
- contentDescription = null,
- tint = MaterialTheme.colorScheme.primary,
- modifier = modifier.align(Alignment.CenterEnd)
- )
- }
- },
- dismissContent = {
- ProductListItem(
- product = item,
- modifier = modifier,
- onClick = {
- navController.navigate(
- ProductDetailsDestination.createRouteWithParam(
- item.id
+ },
+ dismissContent = {
+ ProductListItem(
+ product = item,
+ modifier = modifier,
+ onClick = {
+ navController.navigate(
+ ProductDetailsDestination.createRouteWithParam(
+ item.id
+ )
)
- )
- },
- )
- },
- directions = setOf(DismissDirection.EndToStart),
- )
+ },
+ )
+ },
+ directions = setOf(DismissDirection.EndToStart),
+ )
+ }
}
+ } else {
+ Text("Product list is empty!")
}
- } else {
- Text("Product list is empty!")
}
+
}
}
}
diff --git a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/productlist/ProductListViewModel.kt b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/productlist/ProductListViewModel.kt
index bcb9d4f1b4c..a360fe811c5 100644
--- a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/productlist/ProductListViewModel.kt
+++ b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/productlist/ProductListViewModel.kt
@@ -2,9 +2,9 @@ package com.example.manageproducts.presentation.feature.productlist
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
-import com.example.manageproducts.data.network.dto.ProductDto
-import com.example.manageproducts.data.repository.ProductRepository
import com.example.manageproducts.domain.model.Product
+import com.example.manageproducts.domain.usecase.DeleteProductUseCase
+import com.example.manageproducts.domain.usecase.GetProductsUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
@@ -13,7 +13,8 @@ import javax.inject.Inject
@HiltViewModel
class ProductListViewModel @Inject constructor(
- private val productRepository: ProductRepository,
+ private val getProductsUseCase: GetProductsUseCase,
+ private val deleteProductUseCase: DeleteProductUseCase,
) : ViewModel(), ProductListContract {
private val _productList = MutableStateFlow<List<Product>?>(listOf())
@@ -29,8 +30,14 @@ class ProductListViewModel @Inject constructor(
override fun getProducts() {
viewModelScope.launch {
- val products = productRepository.getProducts()
- _productList.emit(products?.map { it -> it.asDomainModel() })
+ when (val result = getProductsUseCase.execute(input = Unit)) {
+ is GetProductsUseCase.Output.Success -> {
+ _productList.emit(result.data)
+ }
+ is GetProductsUseCase.Output.Failure -> {
+
+ }
+ }
}
}
@@ -39,19 +46,11 @@ class ProductListViewModel @Inject constructor(
val newList = mutableListOf<Product>().apply { _productList.value?.let { addAll(it) } }
newList.remove(product)
_productList.emit(newList.toList())
+
// Call api to remove
- productRepository.deleteProduct(id = product.id)
+ deleteProductUseCase.execute(DeleteProductUseCase.Input(productId = product.id))
// Then fetch again
getProducts()
}
}
-
- private fun ProductDto.asDomainModel(): Product {
- return Product(
- id = this.id ?: "",
- name = this.name,
- price = this.price,
- image = this.image ?: ""
- )
- }
}
\ No newline at end of file
diff --git a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/signin/SignInScreen.kt b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/signin/SignInScreen.kt
new file mode 100644
index 00000000000..20843fb0979
--- /dev/null
+++ b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/signin/SignInScreen.kt
@@ -0,0 +1,125 @@
+package com.example.manageproducts.presentation.feature.signin
+
+import androidx.compose.foundation.layout.*
+import androidx.compose.foundation.shape.RoundedCornerShape
+import androidx.compose.material.SnackbarDuration
+import androidx.compose.material.SnackbarHostState
+import androidx.compose.material.TopAppBar
+import androidx.compose.material.icons.Icons
+import androidx.compose.material.icons.filled.ArrowBack
+import androidx.compose.material3.*
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.collectAsState
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.rememberCoroutineScope
+import androidx.compose.ui.ExperimentalComposeUiApi
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.platform.LocalSoftwareKeyboardController
+import androidx.compose.ui.unit.dp
+import androidx.hilt.navigation.compose.hiltViewModel
+import androidx.navigation.NavController
+import com.example.manageproducts.presentation.navigation.SignUpDestination
+import kotlinx.coroutines.launch
+
+@OptIn(ExperimentalMaterial3Api::class, ExperimentalComposeUiApi::class)
+@Composable
+fun SignInScreen(
+ modifier: Modifier = Modifier,
+ navController: NavController,
+ viewModel: SignInViewModel = hiltViewModel()
+) {
+
+ val snackBarHostState = remember { SnackbarHostState() }
+ val coroutineScope = rememberCoroutineScope()
+ Scaffold(
+ snackbarHost = { androidx.compose.material.SnackbarHost(snackBarHostState) },
+ topBar = {
+ TopAppBar(
+ navigationIcon = {
+ IconButton(onClick = {
+ navController.navigateUp()
+ }) {
+ Icon(
+ imageVector = Icons.Filled.ArrowBack,
+ contentDescription = null,
+ tint = MaterialTheme.colorScheme.onPrimary
+ )
+ }
+ },
+ backgroundColor = MaterialTheme.colorScheme.primary,
+ title = {
+ Text(
+ text = "Login",
+ color = MaterialTheme.colorScheme.onPrimary,
+ )
+ },
+ )
+ }
+ ) { paddingValues ->
+ Column(
+ modifier = modifier
+ .padding(paddingValues)
+ .padding(20.dp)
+ ) {
+ val email = viewModel.email.collectAsState(initial = "")
+ val password = viewModel.password.collectAsState()
+ androidx.compose.material.OutlinedTextField(
+ label = {
+ Text(
+ text = "Email",
+ color = MaterialTheme.colorScheme.primary,
+ style = MaterialTheme.typography.titleMedium
+ )
+ },
+ maxLines = 1,
+ shape = RoundedCornerShape(32),
+ modifier = modifier.fillMaxWidth(),
+ value = email.value,
+ onValueChange = {
+ viewModel.onEmailChange(it)
+ },
+ )
+ androidx.compose.material.OutlinedTextField(
+ label = {
+ Text(
+ text = "Password",
+ color = MaterialTheme.colorScheme.primary,
+ style = MaterialTheme.typography.titleMedium
+ )
+ },
+ maxLines = 1,
+ shape = RoundedCornerShape(32),
+ modifier = modifier
+ .fillMaxWidth()
+ .padding(top = 12.dp),
+ value = password.value,
+ onValueChange = {
+ viewModel.onPasswordChange(it)
+ },
+ )
+ val localSoftwareKeyboardController = LocalSoftwareKeyboardController.current
+ Button(modifier = modifier
+ .fillMaxWidth()
+ .padding(top = 12.dp),
+ onClick = {
+ localSoftwareKeyboardController?.hide()
+ viewModel.onSignIn()
+ coroutineScope.launch {
+ snackBarHostState.showSnackbar(
+ message = "Sign in successfully !",
+ duration = SnackbarDuration.Long
+ )
+ }
+ }) {
+ Text("Sign in")
+ }
+ OutlinedButton(modifier = modifier
+ .fillMaxWidth()
+ .padding(top = 12.dp), onClick = {
+ navController.navigate(SignUpDestination.route)
+ }) {
+ Text("Sign up")
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/signin/SignInViewModel.kt b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/signin/SignInViewModel.kt
new file mode 100644
index 00000000000..1d5141af532
--- /dev/null
+++ b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/signin/SignInViewModel.kt
@@ -0,0 +1,52 @@
+package com.example.manageproducts.presentation.feature.signin
+
+import androidx.lifecycle.ViewModel
+import androidx.lifecycle.viewModelScope
+import com.example.manageproducts.domain.usecase.SignInUseCase
+import dagger.hilt.android.lifecycle.HiltViewModel
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.launch
+import javax.inject.Inject
+
+@HiltViewModel
+class SignInViewModel @Inject constructor(
+ private val signInUseCase: SignInUseCase
+) : ViewModel() {
+
+ private val _email = MutableStateFlow<String>("")
+ val email: Flow<String> = _email
+
+ private val _password = MutableStateFlow("")
+ val password = _password
+
+ private val _message = MutableStateFlow("")
+ val message = _message
+
+ fun onEmailChange(email: String) {
+ _email.value = email
+ }
+
+ fun onPasswordChange(password: String) {
+ _password.value = password
+ }
+
+ fun onSignIn() {
+ viewModelScope.launch {
+ val result = signInUseCase.execute(
+ SignInUseCase.Input(
+ email = _email.value,
+ password = _password.value
+ )
+ )
+ when (result) {
+ is SignInUseCase.Output.Success -> {
+ message.emit("Login successfully !")
+ }
+ else -> {
+ message.emit("Login failed !")
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/signup/SignUpScreen.kt b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/signup/SignUpScreen.kt
new file mode 100644
index 00000000000..9790c9bad96
--- /dev/null
+++ b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/signup/SignUpScreen.kt
@@ -0,0 +1,119 @@
+package com.example.manageproducts.presentation.feature.signup
+
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.shape.RoundedCornerShape
+import androidx.compose.material.OutlinedTextField
+import androidx.compose.material.SnackbarDuration
+import androidx.compose.material.SnackbarHostState
+import androidx.compose.material.TopAppBar
+import androidx.compose.material.icons.Icons
+import androidx.compose.material.icons.filled.ArrowBack
+import androidx.compose.material3.*
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.collectAsState
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.rememberCoroutineScope
+import androidx.compose.ui.ExperimentalComposeUiApi
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.platform.LocalSoftwareKeyboardController
+import androidx.compose.ui.unit.dp
+import androidx.hilt.navigation.compose.hiltViewModel
+import androidx.navigation.NavController
+import kotlinx.coroutines.launch
+
+@OptIn(ExperimentalMaterial3Api::class, ExperimentalComposeUiApi::class)
+@Composable
+fun SignUpScreen(
+ modifier: Modifier = Modifier,
+ navController: NavController,
+ viewModel: SignUpViewModel = hiltViewModel()
+) {
+ val snackBarHostState = remember { SnackbarHostState() }
+ val coroutineScope = rememberCoroutineScope()
+ Scaffold(
+ snackbarHost = { androidx.compose.material.SnackbarHost(snackBarHostState) },
+ topBar = {
+ TopAppBar(
+ navigationIcon = {
+ IconButton(onClick = {
+ navController.navigateUp()
+ }) {
+ Icon(
+ imageVector = Icons.Filled.ArrowBack,
+ contentDescription = null,
+ tint = MaterialTheme.colorScheme.onPrimary
+ )
+ }
+ },
+ backgroundColor = MaterialTheme.colorScheme.primary,
+ title = {
+ Text(
+ text = "Sign Up",
+ color = MaterialTheme.colorScheme.onPrimary,
+ )
+ },
+ )
+ }
+ ) { paddingValues ->
+ Column(
+ modifier = modifier
+ .padding(paddingValues)
+ .padding(20.dp)
+ ) {
+ val email = viewModel.email.collectAsState(initial = "")
+ val password = viewModel.password.collectAsState()
+ OutlinedTextField(
+ label = {
+ Text(
+ text = "Email",
+ color = MaterialTheme.colorScheme.primary,
+ style = MaterialTheme.typography.titleMedium
+ )
+ },
+ maxLines = 1,
+ shape = RoundedCornerShape(32),
+ modifier = modifier.fillMaxWidth(),
+ value = email.value,
+ onValueChange = {
+ viewModel.onEmailChange(it)
+ },
+ )
+ OutlinedTextField(
+ label = {
+ Text(
+ text = "Password",
+ color = MaterialTheme.colorScheme.primary,
+ style = MaterialTheme.typography.titleMedium
+ )
+ },
+ maxLines = 1,
+ shape = RoundedCornerShape(32),
+ modifier = modifier
+ .fillMaxWidth()
+ .padding(top = 12.dp),
+ value = password.value,
+ onValueChange = {
+ viewModel.onPasswordChange(it)
+ },
+ )
+ val localSoftwareKeyboardController = LocalSoftwareKeyboardController.current
+ Button(modifier = modifier
+ .fillMaxWidth()
+ .padding(top = 12.dp),
+ onClick = {
+ localSoftwareKeyboardController?.hide()
+ viewModel.onSignUp()
+ coroutineScope.launch {
+ snackBarHostState.showSnackbar(
+ message = "Create account successfully. Sign in now!",
+ duration = SnackbarDuration.Long
+ )
+ }
+ }) {
+ Text("Sign up")
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/signup/SignUpViewModel.kt b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/signup/SignUpViewModel.kt
new file mode 100644
index 00000000000..b845c5d75ac
--- /dev/null
+++ b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/feature/signup/SignUpViewModel.kt
@@ -0,0 +1,54 @@
+package com.example.manageproducts.presentation.feature.signup
+
+import androidx.lifecycle.ViewModel
+import androidx.lifecycle.viewModelScope
+import com.example.manageproducts.domain.usecase.SignUpUseCase
+import dagger.hilt.android.lifecycle.HiltViewModel
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.launch
+import javax.inject.Inject
+
+@HiltViewModel
+class SignUpViewModel @Inject constructor(
+ private val signUpUseCase: SignUpUseCase
+) : ViewModel() {
+
+ private val _email = MutableStateFlow<String>("")
+ val email: Flow<String> = _email
+
+ private val _password = MutableStateFlow("")
+ val password = _password
+
+ private val _message = MutableStateFlow("")
+ val message = _message
+
+ fun onEmailChange(email: String) {
+ _email.value = email
+ }
+
+ fun onPasswordChange(password: String) {
+ _password.value = password
+ }
+
+ fun onSignUp() {
+ viewModelScope.launch {
+ val result = signUpUseCase.execute(
+ SignUpUseCase.Input(
+ email = _email.value,
+ password = _password.value
+ )
+ )
+ when (result) {
+ is SignUpUseCase.Output.Success -> {
+ _message.emit("Account created successfully!")
+ }
+ else -> {
+ _message.emit("Create account failed !")
+
+ }
+ }
+ }
+
+ }
+}
\ No newline at end of file
diff --git a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/navigation/Destination.kt b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/navigation/Destination.kt
index e65c32e75d4..8b15ed02d1d 100644
--- a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/navigation/Destination.kt
+++ b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/navigation/Destination.kt
@@ -29,3 +29,13 @@ object AddProductDestination : Destination {
override val route = "add_product"
override val title = "Add Product"
}
+
+object AuthenticationDestination: Destination {
+ override val route = "authentication"
+ override val title = "Authentication"
+}
+
+object SignUpDestination: Destination {
+ override val route = "signup"
+ override val title = "Sign Up"
+}
\ No newline at end of file
diff --git a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/navigation/NavRegister.kt b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/navigation/NavRegister.kt
index a4518dc8df8..69f937374e3 100644
--- a/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/navigation/NavRegister.kt
+++ b/examples/product-sample-supabase-kt/app/src/main/java/com/example/manageproducts/presentation/navigation/NavRegister.kt
@@ -4,8 +4,10 @@ import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import com.example.manageproducts.presentation.feature.addproduct.AddProductScreen
+import com.example.manageproducts.presentation.feature.signin.SignInScreen
import com.example.manageproducts.presentation.feature.productdetails.ProductDetailsScreen
import com.example.manageproducts.presentation.feature.productlist.ProductListScreen
+import com.example.manageproducts.presentation.feature.signup.SignUpScreen
fun NavGraphBuilder.navRegistration(navController: NavController) {
composable(ProductListDestination.route) {
@@ -14,11 +16,25 @@ fun NavGraphBuilder.navRegistration(navController: NavController) {
)
}
+ composable(AuthenticationDestination.route) {
+ SignInScreen(
+ navController = navController
+ )
+ }
+
+ composable(SignUpDestination.route) {
+ SignUpScreen(
+ navController = navController
+ )
+ }
+
composable(AddProductDestination.route) {
AddProductScreen(
navController = navController
)
}
+
+
composable(route = "${ProductDetailsDestination.route}/{${ProductDetailsDestination.productId}}",
arguments = ProductDetailsDestination.arguments) { navBackStackEntry ->
val productName =
diff --git a/examples/product-sample-supabase-kt/local.properties b/examples/product-sample-supabase-kt/local.properties
new file mode 100644
index 00000000000..e735b7260b8
--- /dev/null
+++ b/examples/product-sample-supabase-kt/local.properties
@@ -0,0 +1,13 @@
+## This file is automatically generated by Android Studio.
+# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
+#
+# This file should *NOT* be checked into Version Control Systems,
+# as it contains information specific to your local configuration.
+#
+# Location of the SDK. This is only used by Gradle.
+# For customization when using a Version Control System, please read the
+# header note.
+sdk.dir=/Users/hieuvu/Library/Android/sdk
+API_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InR5anh6b2RmcmFoaWJka2hncXV5Iiwicm9sZSI6ImFub24iLCJpYXQiOjE2ODUxMTM2MDcsImV4cCI6MjAwMDY4OTYwN30.rXN-_rGEHKjOlo75xoDYOPkshrno9rK7h1H-CXFb2_g
+SECRET=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InR5anh6b2RmcmFoaWJka2hncXV5Iiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTY4NTExMzYwNywiZXhwIjoyMDAwNjg5NjA3fQ.lWsaScQutgWFsikOFvs17jHdZEe95HYOVjwE8Dm_n1U
+SUPABASE_URL=https://tyjxzodfrahibdkhgquy.supabase.co
\ No newline at end of file