Skip to content

Commit

Permalink
Disconnect from Raspi, Clear SharedPref when logout
Browse files Browse the repository at this point in the history
  • Loading branch information
whyfoo committed Jan 4, 2022
1 parent 81a1f85 commit f9034b4
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/trolle/trolleapp/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class HomeFragment : Fragment() {
alertDialogStatus.apply {
setTitle("Success!")
setMessage("Your smartphone has successfully connected with Trolley ID = " + input.toString() +
" Happy Shopping!")
"\nHappy Shopping!")
setPositiveButton("OK") { dialog, which ->
Navigation.findNavController(view).navigate(R.id.payFragment)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.trolle.trolleapp.data

data class DisconnectRaspi(
val id_user: Int
)
11 changes: 11 additions & 0 deletions app/src/main/java/com/trolle/trolleapp/data/OrderResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.trolle.trolleapp.data

data class OrderResponse (
val status: String,
val message: String,
val data: OrderData
)

data class OrderData(
val id_order: Int,
)
14 changes: 13 additions & 1 deletion app/src/main/java/com/trolle/trolleapp/data/network/api/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ interface Api {
@POST("raspiSession/requestSession")
@Headers("Authorization: Bearer " + "eyJhbGciOiJIUzI1NiJ9.aGFuaXA.TgV2hhHJFndbvucYSV_qN_fzBY5nGJRBOv7NcX2FdIw")
fun connectRaspi(
@Body info: UserRaspi
@Body info: UserRaspi,
): Call<UserRaspiResponse>

@GET("orders/getAllOrders/5")
@Headers("Authorization: Bearer " + "eyJhbGciOiJIUzI1NiJ9.aGFuaXA.TgV2hhHJFndbvucYSV_qN_fzBY5nGJRBOv7NcX2FdIw")
fun getOrderId(

)

@POST("raspiSession/endSession")
@Headers("Authorization: Bearer " + "eyJhbGciOiJIUzI1NiJ9.aGFuaXA.TgV2hhHJFndbvucYSV_qN_fzBY5nGJRBOv7NcX2FdIw")
fun disconnectRaspi(
@Body info: DisconnectRaspi,
): Call<UserRaspiResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import retrofit2.create
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/"

val retrofit = Retrofit.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.trolle.trolleapp.R
import com.trolle.trolleapp.data.sharedpref.SharedPreference
import com.trolle.trolleapp.databinding.ActivityHomeBinding
import com.trolle.trolleapp.ui.side_menu.AboutActivity
import com.trolle.trolleapp.ui.side_menu.EditProfileActivity
Expand All @@ -35,6 +36,8 @@ class HomeActivity : AppCompatActivity() {
binding.drawerLayout.open()
}

val sharedPreference: SharedPreference = SharedPreference(this)

binding.navigationView.setNavigationItemSelectedListener { menuItem ->
// Handle menu item selected
when(menuItem.itemId){
Expand All @@ -47,7 +50,9 @@ 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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@ package com.trolle.trolleapp.ui.home
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.core.content.ContentProviderCompat.requireContext
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.databinding.ActivityHomeSuccessBinding
import com.trolle.trolleapp.ui.pay.CheckoutActivity
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class HomeSuccessActivity : AppCompatActivity() {

Expand All @@ -19,11 +30,40 @@ class HomeSuccessActivity : AppCompatActivity() {
val extras = this.intent.extras
val totalPrice = extras!!.getInt("totalPrice")

val sharedPreference: SharedPreference = SharedPreference(this)
val idUser = sharedPreference.getValueInt("id_user")

binding.textViewGrandTotalPrice.text = getString(R.string.sub_total_price_dummy, totalPrice)

binding.buttonBackToHome.setOnClickListener {
startActivity(Intent(this, HomeActivity::class.java))
disconnectRaspi(idUser)
finish()
}
}

private fun disconnectRaspi(id_user: Int){
val retIn = RetrofitClient.apiInstance
val idUser = DisconnectRaspi(id_user)
retIn.disconnectRaspi(idUser).enqueue(object : Callback<UserRaspiResponse> {
override fun onFailure(call: Call<UserRaspiResponse>, t: Throwable) {
Toast.makeText(this@HomeSuccessActivity, "Timeout", Toast.LENGTH_SHORT).show()
}

override fun onResponse(call: Call<UserRaspiResponse>, response: Response<UserRaspiResponse>) {
if (response.code() == 200) {
Toast.makeText(this@HomeSuccessActivity, response.body()!!.message, Toast.LENGTH_LONG).show()
startActivity(Intent(this@HomeSuccessActivity, HomeActivity::class.java))
} else {
var responseMessage = response.message()
response.errorBody()?.string()?.let {
val jsonObject = JSONObject(it)
val msg = jsonObject.getString("message")
responseMessage = "$msg"
}
val errorMessage = "Error: $responseMessage"
Toast.makeText(this@HomeSuccessActivity, errorMessage, Toast.LENGTH_SHORT).show()
}
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class SignInActivity : AppCompatActivity() {
}
}

binding.textViewForgetPassword.setOnClickListener{
startActivity(Intent(applicationContext, HomeActivity::class.java))
}

binding.textViewThenSignUp.setOnClickListener{
startActivity(Intent(this, SignUpActivity::class.java))
}
Expand Down

0 comments on commit f9034b4

Please sign in to comment.