Skip to content

Instantly share code, notes, and snippets.

View hieuwu's full-sized avatar
🌞
Free

Hieu Vu hieuwu

🌞
Free
View GitHub Profile
@OptIn(ExperimentalCoilApi::class)
@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
@Composable
fun ProductDetailsScreen(
modifier: Modifier = Modifier,
viewModel: ProductDetailsViewModel = hiltViewModel(),
navController: NavController,
productId: String?,
) {
val snackBarHostState = remember { SnackbarHostState() }
@HiltViewModel
class ProductDetailsViewModel @Inject constructor(
private val productRepository: ProductRepository,
savedStateHandle: SavedStateHandle,
) : ViewModel() {
private val _product = MutableStateFlow<Product?>(null)
val product: Flow<Product?> = _product
private val _name = MutableStateFlow("")
@OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterialApi::class)
@Composable
fun ProductListScreen(
modifier: Modifier = Modifier,
navController: NavController,
viewModel: ProductListViewModel = hiltViewModel(),
) {
val isLoading by viewModel.isLoading.collectAsState(initial = false)
val swipeRefreshState = rememberSwipeRefreshState(isRefreshing = isLoading)
SwipeRefresh(state = swipeRefreshState, onRefresh = { viewModel.getProducts() }) {
@HiltViewModel
class ProductListViewModel @Inject constructor(
private val productRepository: ProductRepository,
) : ViewModel() {
private val _productList = MutableStateFlow<List<Product>?>(listOf())
val productList: Flow<List<Product>?> = _productList
private val _isLoading = MutableStateFlow(false)
val isLoading: Flow<Boolean> = _isLoading
interface AuthenticationRepository {
suspend fun signIn(email: String, password: String): Boolean
suspend fun signUp(email: String, password: String): Boolean
suspend fun signInWithGoogle(): Boolean
}
class AuthenticationRepositoryImpl @Inject constructor(
private val goTrue: GoTrue
) : AuthenticationRepository {
override suspend fun signIn(email: String, password: String): Boolean {
interface ProductRepository {
suspend fun createProduct(product: Product): Boolean
suspend fun getProducts(): List<ProductDto>?
suspend fun getProduct(id: String): ProductDto
suspend fun deleteProduct(id: String)
suspend fun updateProduct(
id: String, name: String, price: Double, imageName: String, imageFile: ByteArray
)
}
data class Product(
val id: String,
val name: String,
val price: Double,
val image: String?
)
@Serializable
data class ProductDto(
@SerialName("name")
val name: String,
@SerialName("price")
val price: Double,
@SerialName("image")
@hieuwu
hieuwu / SupabaseModule.kt
Created August 1, 2023 14:24
SupabaseModule.kt
@InstallIn(SingletonComponent::class)
@Module
object SupabaseModule {
@Provides
@Singleton
fun provideSupabaseClient(): SupabaseClient {
return createSupabaseClient(
supabaseUrl = BuildConfig.SUPABASE_URL,
supabaseKey = BuildConfig.SUPABASE_ANON_KEY
@hieuwu
hieuwu / build.gradle (app)
Created August 1, 2023 14:23
build.gradle (app)
implementation "com.google.dagger:hilt-android:$hilt_version"
annotationProcessor "com.google.dagger:hilt-compiler:$hilt_version"
implementation "androidx.hilt:hilt-navigation-compose:1.0.0"