-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
391 additions
and
15 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
app/src/main/java/com/nqmgaming/shoseshop/data/model/order/Order.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.nqmgaming.shoseshop.data.model.order | ||
|
||
import com.google.gson.annotations.SerializedName | ||
|
||
data class Order( | ||
@SerializedName("_id") | ||
val id: String, | ||
@SerializedName("user") | ||
val userId: String, | ||
@SerializedName("address") | ||
val address: String, | ||
@SerializedName("email") | ||
val email: String, | ||
@SerializedName("phoneNumber") | ||
val phoneNumber: String, | ||
@SerializedName("paymentMethod") | ||
val paymentMethod: String, | ||
@SerializedName("status") | ||
val status: String, | ||
@SerializedName("total") | ||
val total: Int, | ||
@SerializedName("products") | ||
val products: List<OrderProduct>, | ||
@SerializedName("createdAt") | ||
val createdAt: String, | ||
@SerializedName("updatedAt") | ||
val updatedAt: String, | ||
) | ||
|
||
data class OrderProduct( | ||
@SerializedName("_id") | ||
val id: String, | ||
@SerializedName("product") | ||
val productId: String, | ||
@SerializedName("quantity") | ||
val quantity: Int, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
app/src/main/java/com/nqmgaming/shoseshop/ui/activities/checkout/CheckoutViewModel.kt
This file was deleted.
Oops, something went wrong.
126 changes: 125 additions & 1 deletion
126
app/src/main/java/com/nqmgaming/shoseshop/ui/activities/order/OrderActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,145 @@ | ||
package com.nqmgaming.shoseshop.ui.activities.order | ||
|
||
import android.app.Dialog | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.util.Log | ||
import android.widget.Toast | ||
import androidx.activity.enableEdgeToEdge | ||
import androidx.activity.viewModels | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.core.view.ViewCompat | ||
import androidx.core.view.WindowInsetsCompat | ||
import com.nqmgaming.shoseshop.R | ||
import com.nqmgaming.shoseshop.data.model.order.Order | ||
import com.nqmgaming.shoseshop.data.model.order.OrderProduct | ||
import com.nqmgaming.shoseshop.databinding.ActivityOrderBinding | ||
import com.nqmgaming.shoseshop.ui.activities.MainActivity | ||
import com.nqmgaming.shoseshop.ui.fragments.cart.CartViewModel | ||
import com.nqmgaming.shoseshop.util.SharedPrefUtils | ||
import com.saadahmedsoft.popupdialog.PopupDialog | ||
import com.saadahmedsoft.popupdialog.Styles | ||
import com.saadahmedsoft.popupdialog.listener.OnDialogButtonClickListener | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import java.time.LocalDate | ||
import java.time.format.DateTimeFormatter | ||
|
||
@AndroidEntryPoint | ||
class OrderActivity : AppCompatActivity() { | ||
private lateinit var binding: ActivityOrderBinding | ||
private val viewModel by viewModels<OrderViewModel>() | ||
private val cartViewModel by viewModels<CartViewModel>() | ||
private var paymentMethod: String = "Cash" | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
enableEdgeToEdge() | ||
setContentView(R.layout.activity_order) | ||
binding = ActivityOrderBinding.inflate(layoutInflater) | ||
setContentView(binding.root) | ||
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -> | ||
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) | ||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) | ||
insets | ||
} | ||
|
||
val totalPrice = intent.getStringExtra("total") ?: "" | ||
val email = intent.getStringExtra("email") ?: "" | ||
val phoneNumber = intent.getStringExtra("phoneNumber") ?: "" | ||
val token = intent.getStringExtra("token") ?: "" | ||
val address = intent.getStringExtra("address") ?: "" | ||
val userId = SharedPrefUtils.getString(this, "id") ?: "" | ||
|
||
|
||
//get date | ||
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") | ||
val date = LocalDate.parse(LocalDate.now().toString(), formatter) | ||
|
||
binding.rdCash.isChecked = true | ||
|
||
binding.cardCash.setOnClickListener { | ||
paymentMethod = "Cash" | ||
binding.rdCash.isChecked = true | ||
binding.rdCc.isChecked = false | ||
binding.rdPaypal.isChecked = false | ||
} | ||
|
||
binding.cardCredit.setOnClickListener { | ||
paymentMethod = "CC" | ||
binding.rdCash.isChecked = false | ||
binding.rdCc.isChecked = true | ||
binding.rdPaypal.isChecked = false | ||
} | ||
|
||
binding.cardPaypal.setOnClickListener { | ||
paymentMethod = "Paypal" | ||
binding.rdCash.isChecked = false | ||
binding.rdCc.isChecked = false | ||
binding.rdPaypal.isChecked = true | ||
} | ||
|
||
binding.btnNext.setOnClickListener { | ||
// Get all carts from the server by userId and create an order | ||
cartViewModel.getAllCartsMain(token, userId) { carts -> | ||
if (carts != null) { | ||
val order = Order( | ||
id = "", | ||
userId = userId, | ||
email = email, | ||
phoneNumber = phoneNumber, | ||
address = address, | ||
createdAt = date.toString(), | ||
updatedAt = date.toString(), | ||
status = "Pending", | ||
paymentMethod = paymentMethod, | ||
total = totalPrice.toDouble().toInt(), | ||
products = carts.map { cart -> | ||
OrderProduct( | ||
id = cart.id, | ||
productId = cart.items.product, | ||
quantity = cart.items.quantity | ||
) | ||
} | ||
) | ||
Log.d("OrderActivity", "onCreate: $order") | ||
viewModel.createOrderOrder(token, order) { isOrderCreated -> | ||
if (isOrderCreated) { | ||
// Order created successfully | ||
// Clear the cart | ||
viewModel.deleteAllCartOrder(token, userId) { done -> | ||
if (done) { | ||
PopupDialog.getInstance(this@OrderActivity) | ||
.setStyle(Styles.SUCCESS) | ||
.setHeading("Order created successfully!") | ||
.setDescription("Thanks for your purchase") | ||
.setDismissButtonText("OK") | ||
.setCancelable(false) | ||
.showDialog(object : OnDialogButtonClickListener() { | ||
override fun onDismissClicked(dialog: Dialog) { | ||
super.onDismissClicked(dialog) | ||
dialog.dismiss() | ||
Intent( | ||
this@OrderActivity, | ||
MainActivity::class.java | ||
).also { intent -> | ||
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) | ||
startActivity(intent) | ||
finish() | ||
} | ||
} | ||
}) | ||
} else { | ||
Toast.makeText(this, "Error deleting cart", Toast.LENGTH_SHORT) | ||
.show() | ||
} | ||
} | ||
} else { | ||
Toast.makeText(this, "Error creating order", Toast.LENGTH_SHORT).show() | ||
} | ||
} | ||
} else { | ||
Toast.makeText(this, "Cart is empty", Toast.LENGTH_SHORT).show() | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
app/src/main/java/com/nqmgaming/shoseshop/ui/activities/order/OrderViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.nqmgaming.shoseshop.ui.activities.order | ||
|
||
import android.util.Log | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.nqmgaming.shoseshop.data.model.order.Order | ||
import com.nqmgaming.shoseshop.data.repository.ShoesRepository | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class OrderViewModel @Inject constructor( | ||
private val shoesRepository: ShoesRepository | ||
) : ViewModel() { | ||
private suspend fun createOrder(token: String, order: Order) = | ||
shoesRepository.createOrder(token, order) | ||
|
||
private suspend fun deleteAllCart(token: String, userId: String) = | ||
shoesRepository.deleteAllCart(token, userId) | ||
|
||
fun createOrderOrder(token: String, order: Order, callback: (Boolean) -> Unit) { | ||
viewModelScope.launch { | ||
try { | ||
val isOrderCreated = createOrder(token, order) | ||
callback(true) | ||
} catch (e: Exception) { | ||
Log.e("OrderViewModel", "Error creating order: ${e.message}") | ||
callback(false) | ||
} | ||
} | ||
} | ||
|
||
fun deleteAllCartOrder(token: String, userId: String, callback: (Boolean) -> Unit) { | ||
viewModelScope.launch { | ||
try { | ||
val isCartDeleted = deleteAllCart(token, userId) | ||
callback(true) | ||
} catch (e: Exception) { | ||
Log.e("OrderViewModel", "Error deleting all cart: ${e.message}") | ||
callback(false) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.