Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate delivery screen to compose #226

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,38 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.ui.platform.ComposeView
import androidx.core.os.bundleOf
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.hieuwu.groceriesstore.R
import com.hieuwu.groceriesstore.databinding.FragmentDeliveryBinding
import com.hieuwu.groceriesstore.utilities.KeyData

class DeliveryFragment : Fragment() {
lateinit var binding: FragmentDeliveryBinding

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

binding = DataBindingUtil.inflate<FragmentDeliveryBinding>(
inflater,
R.layout.fragment_delivery,
container,
false
)
binding.lifecycleOwner = this

binding.updateDeliveryBtn.setOnClickListener {
parentFragmentManager.setFragmentResult(
KeyData.RESULT_KEY, bundleOf(
KeyData.NAME_KEY to binding.receiverName.text.toString(),
KeyData.PHONE_KEY to binding.receiverPhone.text.toString(),
KeyData.STREET_KEY to binding.addressStreet.text.toString(),
KeyData.WARD_KEY to binding.addressWard.text.toString(),
KeyData.CITY_KEY to binding.addressCity.text.toString()
return ComposeView(requireContext()).apply {
setContent {
DeliveryScreen(
onNavigateBack = { findNavController().navigateUp() },
onUpdateClick = { delivery ->
parentFragmentManager.setFragmentResult(
KeyData.RESULT_KEY, bundleOf(
KeyData.NAME_KEY to delivery.name,
KeyData.PHONE_KEY to delivery.phoneNumber,
KeyData.STREET_KEY to delivery.street,
KeyData.WARD_KEY to delivery.district,
KeyData.CITY_KEY to delivery.province
)
)
findNavController().popBackStack()
}
)
)
findNavController().popBackStack()
}

binding.toolbar.setNavigationOnClickListener {
findNavController().navigateUp()
}
}
return binding.root
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.hieuwu.groceriesstore.presentation.delivery

data class DeliveryModel(
val name: String,
val phoneNumber: String,
val street: String,
val district: String,
val province: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package com.hieuwu.groceriesstore.presentation.delivery

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.NavigateBefore
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.hieuwu.groceriesstore.R
import com.hieuwu.groceriesstore.presentation.core.widgets.PrimaryButton
import com.hieuwu.groceriesstore.presentation.delivery.composables.Header
import com.hieuwu.groceriesstore.presentation.delivery.composables.Input

@Composable
fun DeliveryScreen(
onNavigateBack: () -> Unit,
onUpdateClick: (DeliveryModel) -> Unit
) {

var name by remember { mutableStateOf("") }
var phoneNumber by remember { mutableStateOf("") }
var street by remember { mutableStateOf("") }
var district by remember { mutableStateOf("") }
var province by remember { mutableStateOf("") }

Scaffold(
topBar = {
TopAppBar(
title = {
Text(stringResource(id = R.string.delivery))
},
backgroundColor = colorResource(id = R.color.colorPrimary),
contentColor = Color.White,
navigationIcon = {
IconButton(onClick = onNavigateBack) {
Icon(
imageVector = Icons.Filled.NavigateBefore,
contentDescription = null
)
}
}
)
}
) { paddingValues ->
Column(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues)
.padding(16.dp)
.verticalScroll(rememberScrollState())
) {

Header(title = "Receiver")

Input(
value = name,
placeholder = stringResource(id = R.string.name),
onValueChange = {
name = it
}
)

Input(
value = phoneNumber,
placeholder = stringResource(R.string.phone_number),
onValueChange = {
phoneNumber = it
}
)

Header(title = "Address")

Input(
value = street,
placeholder = "Street",
onValueChange = {
street = it
}
)

Input(
value = district,
placeholder = "Ward/District",
onValueChange = {
district = it
}
)

Input(
value = province,
placeholder = "Province/City",
onValueChange = {
province = it
}
)

Spacer(modifier = Modifier.weight(1f))

PrimaryButton(
onClick = {
onUpdateClick(
DeliveryModel(
name,
phoneNumber,
street,
district,
province
)
)
},
modifier = Modifier.fillMaxWidth()
) {
Text(text = stringResource(id = R.string.update), color = Color.White)
}
}
}

}

@Preview(
heightDp = 700
)
@Composable
fun DeliveryScreenPreview() {
DeliveryScreen(onNavigateBack = { /*TODO*/ }, onUpdateClick = {})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.hieuwu.groceriesstore.presentation.delivery.composables

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
fun Header(
modifier: Modifier = Modifier,
title: String
) {
Box(modifier = modifier.fillMaxWidth().padding(vertical = 8.dp)) {
Text(text = title, fontSize = 16.sp, fontWeight = FontWeight.Bold)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.hieuwu.groceriesstore.presentation.delivery.composables

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.TextField
import androidx.compose.material3.Text
import androidx.compose.material.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color

@Composable
fun Input(
modifier: Modifier = Modifier,
value: String,
placeholder: String,
onValueChange: (String) -> Unit
) {
TextField(
value = value,
onValueChange = onValueChange,
placeholder = {
Text(text = placeholder)
},
modifier = modifier.fillMaxWidth(),
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Color.Transparent
)
)
}
Loading
Loading