Skip to content

Commit

Permalink
Recycler View by Order ID
Browse files Browse the repository at this point in the history
  • Loading branch information
whyfoo committed Jan 4, 2022
1 parent f9034b4 commit c772c59
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 20 deletions.
7 changes: 3 additions & 4 deletions app/src/main/java/com/trolle/trolleapp/PayFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,12 @@ class PayFragment : Fragment() {
viewModel.getSearchItems().observe(viewLifecycleOwner, {
if (it != null) {
showLoading(true)
var totalPrice = -100000000
var totalPrice = 0
for (i in 0..it.size - 1) {
totalPrice += it.get(i).id
totalPrice += it.get(i).price_per_unit * it.get(i).quantity
}
adapter.setList(it)
showLoading(false)
totalPrice /= 10000
binding.textViewSubTotalPrice.text = getString(R.string.sub_total_price_dummy, totalPrice)

subTotal = totalPrice
Expand All @@ -80,7 +79,7 @@ class PayFragment : Fragment() {
}

private fun searchItem(){
val query = "wondrouss"
val query = 11
viewModel.setSearchItems(query)

}
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/com/trolle/trolleapp/data/CartResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.trolle.trolleapp.data

import java.util.*

data class CartResponse (
val status: String,
val message: String,
val data: CartData
)

data class CartData(
val result: ArrayList<Cart>,
val total_price: Int
)

data class Cart(
val name: String,
val quantity: Int,
val price_per_unit: Int,
)
4 changes: 4 additions & 0 deletions app/src/main/java/com/trolle/trolleapp/data/OrderResponse.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.trolle.trolleapp.data

import java.util.*

data class OrderResponse (
val status: String,
val message: String,
Expand All @@ -8,4 +10,6 @@ data class OrderResponse (

data class OrderData(
val id_order: Int,
val tanggal: Date,
val status_transaksi: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@ package com.trolle.trolleapp.data.adapter
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.trolle.trolleapp.data.Cart
import com.trolle.trolleapp.data.Item
import com.trolle.trolleapp.databinding.ItemRowListBinding
import java.text.NumberFormat
import java.util.*

class ItemAdapter: RecyclerView.Adapter<ItemAdapter.ItemViewHolder>() {
private val list = ArrayList<Item>()
private val list = ArrayList<Cart>()

fun setList(items: ArrayList<Item>){
fun setList(items: ArrayList<Cart>){
list.clear()
list.addAll(items)
notifyDataSetChanged()
}

inner class ItemViewHolder(val binding: ItemRowListBinding) : RecyclerView.ViewHolder(binding.root){
fun bind(item: Item){
val price = item.id/100
binding.tvItemName.text = item.login
binding.tvItemCount.text = "x${price%10+1}"
fun bind(item: Cart){
val price = item.price_per_unit
binding.tvItemName.text = item.name
binding.tvItemCount.text = "x${item.quantity}"

val localeID = Locale("in", "ID")
val rupiah = NumberFormat.getCurrencyInstance(localeID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,17 @@ interface Api {
@Headers("Authorization: Bearer " + "eyJhbGciOiJIUzI1NiJ9.aGFuaXA.TgV2hhHJFndbvucYSV_qN_fzBY5nGJRBOv7NcX2FdIw")
fun getOrderId(

)
): Call<OrderResponse>

@POST("raspiSession/endSession")
@Headers("Authorization: Bearer " + "eyJhbGciOiJIUzI1NiJ9.aGFuaXA.TgV2hhHJFndbvucYSV_qN_fzBY5nGJRBOv7NcX2FdIw")
fun disconnectRaspi(
@Body info: DisconnectRaspi,
): Call<UserRaspiResponse>

@GET("ordersxproducts/getCart/{id_order}")
@Headers("Authorization: Bearer " + "eyJhbGciOiJIUzI1NiJ9.aGFuaXA.TgV2hhHJFndbvucYSV_qN_fzBY5nGJRBOv7NcX2FdIw")
fun getCartItems(
@Path("id_order") id_order: Int
): Call<CartResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import java.util.concurrent.TimeUnit

object RetrofitClient {
// private const val BASE_URL = "https://api.github.com/"
private const val BASE_URL = "http://192.168.100.37:3000/"
private const val BASE_URL = "http://192.168.100.38:3000/"

val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.trolle.trolleapp.data.Cart
import com.trolle.trolleapp.data.CartResponse
import com.trolle.trolleapp.data.Item
import com.trolle.trolleapp.data.ItemResponse
import com.trolle.trolleapp.data.network.api.RetrofitClient
Expand All @@ -14,26 +16,26 @@ import java.util.*

class MainViewModel: ViewModel() {

val listItems = MutableLiveData<ArrayList<Item>>()
val listItems = MutableLiveData<ArrayList<Cart>>()

fun setSearchItems(query: String){
fun setSearchItems(query: Int){
RetrofitClient.apiInstance
.getSearchItems(query)
.enqueue(object: Callback<ItemResponse>{
override fun onResponse(call: Call<ItemResponse>, response: Response<ItemResponse>) {
.getCartItems(query)
.enqueue(object: Callback<CartResponse>{
override fun onResponse(call: Call<CartResponse>, response: Response<CartResponse>) {
if (response.isSuccessful){
listItems.postValue(response.body()?.items)
listItems.postValue(response.body()?.data?.result)
}
}

override fun onFailure(call: Call<ItemResponse>, t: Throwable) {
override fun onFailure(call: Call<CartResponse>, t: Throwable) {
Log.d("Failure", "ada error mainviewmodel")
}

})
}

fun getSearchItems(): LiveData<ArrayList<Item>>{
fun getSearchItems(): LiveData<ArrayList<Cart>>{
return listItems
}
}

0 comments on commit c772c59

Please sign in to comment.