Skip to content

Commit

Permalink
Recording
Browse files Browse the repository at this point in the history
  • Loading branch information
whyfoo committed Jan 4, 2022
1 parent 01db077 commit 0a7a45e
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 20 deletions.
9 changes: 8 additions & 1 deletion app/src/main/java/com/trolle/trolleapp/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import com.trolle.trolleapp.data.*
import com.trolle.trolleapp.data.network.api.RetrofitClient
import com.trolle.trolleapp.data.sharedpref.SharedPreference
import com.trolle.trolleapp.data.viewmodel.MainViewModel
import com.trolle.trolleapp.databinding.FragmentHomeBinding
import com.trolle.trolleapp.ui.home.HomeActivity
import org.json.JSONObject
Expand Down Expand Up @@ -46,6 +48,10 @@ class HomeFragment : Fragment() {
val sharedPreference: SharedPreference = SharedPreference(requireContext())
val idUser = sharedPreference.getValueInt("id_user")

if (sharedPreference.getValueBoolien("status", false)){
Navigation.findNavController(view).navigate(R.id.payFragment)
}

binding.imageViewBarcode.setOnClickListener{
val takePhotoIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(takePhotoIntent, 1)
Expand All @@ -72,8 +78,9 @@ class HomeFragment : Fragment() {
setMessage("Your smartphone has successfully connected with Trolley ID = " + input.toString() +
"\nHappy Shopping!")
setPositiveButton("OK") { dialog, which ->
Navigation.findNavController(view).navigate(R.id.payFragment)
getOrderId(idUser)
sharedPreference.save("status", true)
Navigation.findNavController(view).navigate(R.id.payFragment)
}
alertDialogStatus.setNegativeButton("Cancel") { dialog, which ->
dialog.cancel()
Expand Down
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 @@ -41,6 +41,7 @@ class PayFragment : Fragment() {
super.onViewCreated(view, savedInstanceState)

var subTotal: Int = 0
binding.textViewSubTotalPrice.text = subTotal.toString()

adapter = ItemAdapter()
adapter.notifyDataSetChanged()
Expand All @@ -52,7 +53,7 @@ class PayFragment : Fragment() {

searchItem()

Toast.makeText(context, "Searching items", Toast.LENGTH_SHORT).show()
Toast.makeText(context, "Shopping process started", Toast.LENGTH_SHORT).show()

viewModel.getSearchItems().observe(viewLifecycleOwner, {
if (it != null) {
Expand Down Expand Up @@ -81,9 +82,7 @@ class PayFragment : Fragment() {
}

private fun searchItem(){
val sharedPreference: SharedPreference = SharedPreference(requireContext())
val query = sharedPreference.getValueInt("id_order")
viewModel.setSearchItems(query)
viewModel.setSearchItems(requireContext())

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.trolle.trolleapp.data.viewmodel

import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Looper
Expand All @@ -12,31 +13,49 @@ 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
import com.trolle.trolleapp.data.sharedpref.SharedPreference
import com.trolle.trolleapp.ui.signin.SignInActivity
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.util.*

class MainViewModel: ViewModel() {


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

fun setSearchItems(query: Int){
RetrofitClient.apiInstance
.getCartItems(query)
.enqueue(object: Callback<CartResponse>{
override fun onResponse(call: Call<CartResponse>, response: Response<CartResponse>) {
if (response.isSuccessful){
listItems.postValue(response.body()?.data?.result)
fun setSearchItems(ctx: Context) = GlobalScope.async{
val sharedPreference: SharedPreference = SharedPreference(ctx)
var status = sharedPreference.getValueBoolien("status", false)
while (status) {
status = sharedPreference.getValueBoolien("status", false)
val query = sharedPreference.getValueInt("id_order")
Log.d("homeit", "ngeloop" + query.toString())
RetrofitClient.apiInstance
.getCartItems(query)
.enqueue(object: Callback<CartResponse>{
override fun onResponse(call: Call<CartResponse>, response: Response<CartResponse>) {
if (response.isSuccessful){
Log.d("homeit", response.body()?.data?.result.toString())
if(response.body()?.data?.result.toString() == "") {
sharedPreference.save("inCart", false)
} else {
listItems.value = response.body()?.data?.result
}
}
}

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

})
})
delay(5000)
}
}

fun getSearchItems(): LiveData<ArrayList<Cart>>{
Expand Down
13 changes: 9 additions & 4 deletions app/src/main/java/com/trolle/trolleapp/ui/home/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,15 @@ class HomeActivity : AppCompatActivity() {
R.id.item_help ->
startActivity(Intent(this, HelpActivity::class.java))
R.id.item_sign_out -> {
sharedPreference.clearSharedPreference()
startActivity(Intent(this, SignInActivity::class.java))
Toast.makeText(this, "Logged Out Successfully", Toast.LENGTH_SHORT).show()
finish()
if (sharedPreference.getValueBoolien("status", false).equals(false)){
sharedPreference.clearSharedPreference()
startActivity(Intent(this, SignInActivity::class.java))
Toast.makeText(this, "Logged Out Successfully", Toast.LENGTH_SHORT).show()
finish()
} else {
Toast.makeText(this, "Please checkout your order first!", Toast.LENGTH_LONG).show()
}

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.lifecycle.ViewModelProvider
import com.trolle.trolleapp.R
import com.trolle.trolleapp.data.DisconnectRaspi
import com.trolle.trolleapp.data.UserRaspi
import com.trolle.trolleapp.data.UserRaspiResponse
import com.trolle.trolleapp.data.network.api.RetrofitClient
import com.trolle.trolleapp.data.sharedpref.SharedPreference
import com.trolle.trolleapp.data.viewmodel.MainViewModel
import com.trolle.trolleapp.databinding.ActivityHomeSuccessBinding
import com.trolle.trolleapp.ui.pay.CheckoutActivity
import org.json.JSONObject
Expand All @@ -37,6 +39,8 @@ class HomeSuccessActivity : AppCompatActivity() {

binding.buttonBackToHome.setOnClickListener {
disconnectRaspi(idUser)
sharedPreference.save("status", false)
sharedPreference.removeValue("id_order")
finish()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class SignInActivity : AppCompatActivity() {
binding = ActivitySignInBinding.inflate(layoutInflater)
setContentView(binding.root)

val sharedPreference: SharedPreference = SharedPreference(this@SignInActivity)
if (!sharedPreference.getValueInt("id_user").equals(0)){
startActivity(Intent(this, HomeActivity::class.java))
}


binding.buttonSignIn.setOnClickListener {
val username: String = binding.editTextUsername.getText().toString()
val password: String = binding.editTextPassword.getText().toString()
Expand Down

0 comments on commit 0a7a45e

Please sign in to comment.