Skip to content
This repository was archived by the owner on Dec 16, 2023. It is now read-only.

Commit dcdd613

Browse files
committed
Add prompt to create delivery address on checkout if none exists
1 parent 2cc10fd commit dcdd613

4 files changed

Lines changed: 92 additions & 11 deletions

File tree

app/src/main/java/com/marknkamau/justjava/di/Modules.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ val viewModelModule = module {
6666
viewModel { SignUpViewModel(get(), get(), get()) }
6767
viewModel { ProfileViewModel(get()) }
6868
viewModel { AddressBookViewModel(get()) }
69-
viewModel { CheckoutViewModel(get(), get(), get()) }
69+
viewModel { CheckoutViewModel(get(), get(), get(), get()) }
7070
viewModel { OrdersViewModel(get()) }
7171
viewModel { OrderDetailViewModel(get(), get()) }
7272
viewModel { PayMpesaViewModel(get(), get()) }

app/src/main/java/com/marknkamau/justjava/ui/checkout/CheckoutActivity.kt

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.marknkamau.justjava.ui.checkout
22

33
import android.annotation.SuppressLint
4+
import android.app.Activity
45
import android.content.Intent
56
import android.os.Bundle
67
import android.view.View
8+
import android.widget.Toast
79
import androidx.appcompat.app.AlertDialog
810
import androidx.appcompat.app.AppCompatActivity
911
import androidx.core.app.TaskStackBuilder
@@ -13,6 +15,7 @@ import com.marknjunge.core.data.model.PaymentMethod
1315
import com.marknjunge.core.data.model.Resource
1416
import com.marknjunge.core.data.model.User
1517
import com.marknkamau.justjava.R
18+
import com.marknkamau.justjava.ui.addAddress.AddAddressActivity
1619
import com.marknkamau.justjava.ui.login.SignInActivity
1720
import com.marknkamau.justjava.ui.main.MainActivity
1821
import com.marknkamau.justjava.ui.orderDetail.OrderDetailActivity
@@ -26,8 +29,9 @@ class CheckoutActivity : AppCompatActivity() {
2629

2730
private val checkoutViewModel: CheckoutViewModel by viewModel()
2831
private lateinit var paymentMethod: PaymentMethod
29-
private lateinit var deliveryAddress: Address
32+
private var deliveryAddress: Address? = null
3033
private lateinit var user: User
34+
private val ADD_ADDRESS_REQ = 99
3135

3236
@SuppressLint("DefaultLocale")
3337
override fun onCreate(savedInstanceState: Bundle?) {
@@ -50,10 +54,16 @@ class CheckoutActivity : AppCompatActivity() {
5054
checkoutViewModel.getCartItems()
5155
user = checkoutViewModel.getUser()
5256
if (user.address.isEmpty()) {
53-
// TODO Handle no address
57+
btnAddDeliveryAddress.visibility = View.VISIBLE
58+
btnChangeDeliveryAddress.visibility = View.GONE
59+
tvDeliveryAddress.visibility = View.GONE
5460
} else {
61+
btnAddDeliveryAddress.visibility = View.GONE
62+
btnChangeDeliveryAddress.visibility = View.VISIBLE
63+
tvDeliveryAddress.visibility = View.VISIBLE
64+
5565
deliveryAddress = user.address[0]
56-
tvDeliveryAddress.text = deliveryAddress.streetAddress
66+
tvDeliveryAddress.text = deliveryAddress!!.streetAddress
5767
}
5868

5969
btnChangeDeliveryAddress.setOnClickListener {
@@ -64,7 +74,34 @@ class CheckoutActivity : AppCompatActivity() {
6474
}
6575

6676
btnPlaceOrder.setOnClickListener {
67-
placeOrder()
77+
if (valid()) {
78+
placeOrder()
79+
}
80+
}
81+
btnAddDeliveryAddress.setOnClickListener {
82+
startActivityForResult(Intent(this, AddAddressActivity::class.java), ADD_ADDRESS_REQ)
83+
}
84+
}
85+
86+
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
87+
super.onActivityResult(requestCode, resultCode, data)
88+
89+
if (requestCode == ADD_ADDRESS_REQ && resultCode == Activity.RESULT_OK) {
90+
val address = data?.extras!![AddAddressActivity.ADDRESS_KEY] as Address
91+
92+
checkoutViewModel.addAddress(address).observe(this, Observer { resource ->
93+
when (resource) {
94+
is Resource.Success -> {
95+
btnAddDeliveryAddress.visibility = View.GONE
96+
btnChangeDeliveryAddress.visibility = View.VISIBLE
97+
tvDeliveryAddress.visibility = View.VISIBLE
98+
99+
deliveryAddress = address
100+
tvDeliveryAddress.text = deliveryAddress!!.streetAddress
101+
}
102+
is Resource.Failure -> toast(resource.message)
103+
}
104+
})
68105
}
69106
}
70107

@@ -91,7 +128,7 @@ class CheckoutActivity : AppCompatActivity() {
91128
.setTitle(R.string.delivery_address)
92129
.setItems(addresses) { _, which ->
93130
deliveryAddress = user.address[which]
94-
tvDeliveryAddress.text = deliveryAddress.streetAddress
131+
tvDeliveryAddress.text = deliveryAddress!!.streetAddress
95132
}
96133
.create()
97134
.show()
@@ -115,7 +152,7 @@ class CheckoutActivity : AppCompatActivity() {
115152
if (etAdditionalComments.trimmedText.isNotEmpty()) etAdditionalComments.trimmedText else null
116153

117154
btnPlaceOrder.isEnabled = false
118-
checkoutViewModel.placeOrder(paymentMethod, deliveryAddress, additionalComments)
155+
checkoutViewModel.placeOrder(paymentMethod, deliveryAddress!!, additionalComments)
119156
.observe(this, Observer { resource ->
120157
btnPlaceOrder.isEnabled = true
121158
when (resource) {
@@ -137,4 +174,15 @@ class CheckoutActivity : AppCompatActivity() {
137174
})
138175

139176
}
177+
178+
private fun valid(): Boolean {
179+
var isValid = true
180+
181+
if (deliveryAddress == null) {
182+
toast("A delivery address is required", Toast.LENGTH_LONG)
183+
isValid = false
184+
}
185+
186+
return isValid
187+
}
140188
}

app/src/main/java/com/marknkamau/justjava/ui/checkout/CheckoutViewModel.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ import androidx.lifecycle.viewModelScope
77
import com.marknjunge.core.data.local.PreferencesRepository
88
import com.marknjunge.core.data.model.*
99
import com.marknjunge.core.data.repository.OrdersRepository
10+
import com.marknjunge.core.data.repository.UsersRepository
1011
import com.marknkamau.justjava.data.db.DbRepository
1112
import com.marknkamau.justjava.data.models.CartItem
1213
import kotlinx.coroutines.launch
1314

1415
class CheckoutViewModel(
1516
private val preferencesRepository: PreferencesRepository,
1617
private val dbRepository: DbRepository,
17-
private val ordersRepository: OrdersRepository
18+
private val ordersRepository: OrdersRepository,
19+
private val usersRepository: UsersRepository
1820
) : ViewModel() {
1921
private val _items = MutableLiveData<List<CartItem>>()
2022
val items: LiveData<List<CartItem>> = _items
@@ -60,4 +62,16 @@ class CheckoutViewModel(
6062

6163
return liveData
6264
}
65+
66+
fun addAddress(address: Address): LiveData<Resource<Address>> {
67+
val liveData = MutableLiveData<Resource<Address>>()
68+
69+
viewModelScope.launch {
70+
_loading.value = true
71+
liveData.value = usersRepository.addAddress(address)
72+
_loading.value = false
73+
}
74+
75+
return liveData
76+
}
6377
}

app/src/main/res/layout/activity_checkout.xml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,12 @@
133133
android:layout_marginStart="16dp"
134134
android:layout_marginTop="8dp"
135135
android:layout_marginEnd="8dp"
136+
android:visibility="gone"
136137
app:layout_constraintEnd_toStartOf="@+id/btnChangeDeliveryAddress"
137138
app:layout_constraintStart_toStartOf="parent"
138139
app:layout_constraintTop_toBottomOf="@+id/textView36"
139-
tools:text="Unamed Road, Unamed Estate" />
140+
tools:text="Unamed Road, Unamed Estate"
141+
tools:visibility="visible" />
140142

141143
<com.google.android.material.button.MaterialButton
142144
android:id="@+id/btnChangeDeliveryAddress"
@@ -145,8 +147,25 @@
145147
android:layout_height="wrap_content"
146148
android:layout_marginEnd="16dp"
147149
android:text="Change"
150+
android:visibility="gone"
148151
app:layout_constraintBaseline_toBaselineOf="@+id/tvDeliveryAddress"
149-
app:layout_constraintEnd_toEndOf="parent" />
152+
app:layout_constraintEnd_toEndOf="parent"
153+
tools:visibility="visible" />
154+
155+
<com.google.android.material.button.MaterialButton
156+
android:id="@+id/btnAddDeliveryAddress"
157+
style="@style/AppTheme.Button.OutlinedAccent"
158+
android:layout_width="wrap_content"
159+
android:layout_height="56dp"
160+
android:layout_marginStart="16dp"
161+
android:layout_marginTop="8dp"
162+
android:layout_marginEnd="16dp"
163+
android:text="Add Delivery Address"
164+
android:visibility="gone"
165+
app:layout_constraintEnd_toEndOf="parent"
166+
app:layout_constraintStart_toStartOf="parent"
167+
app:layout_constraintTop_toBottomOf="@+id/tvDeliveryAddress"
168+
tools:visibility="visible" />
150169

151170
<View
152171
android:id="@+id/view12"
@@ -157,7 +176,7 @@
157176
android:background="#000"
158177
app:layout_constraintEnd_toEndOf="parent"
159178
app:layout_constraintStart_toStartOf="parent"
160-
app:layout_constraintTop_toBottomOf="@+id/tvDeliveryAddress" />
179+
app:layout_constraintTop_toBottomOf="@+id/btnAddDeliveryAddress" />
161180

162181
<TextView
163182
android:id="@+id/textView38"

0 commit comments

Comments
 (0)