Skip to content

Commit

Permalink
User connect to raspi
Browse files Browse the repository at this point in the history
  • Loading branch information
whyfoo committed Jan 4, 2022
1 parent 2c75e03 commit 81a1f85
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 25 deletions.
65 changes: 46 additions & 19 deletions app/src/main/java/com/trolle/trolleapp/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,20 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.Toast
import androidx.navigation.Navigation
import com.trolle.trolleapp.data.SignInBody
import com.trolle.trolleapp.data.SignInResponse
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.FragmentHomeBinding
import com.trolle.trolleapp.ui.home.HomeActivity
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class HomeFragment : Fragment() {

Expand All @@ -33,25 +45,12 @@ class HomeFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.imageViewBarcode.setOnClickListener{
val sharedPreference: SharedPreference = SharedPreference(requireContext())
val idUser = sharedPreference.getValueInt("id_user")

binding.imageViewBarcode.setOnClickListener{
val takePhotoIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(takePhotoIntent, 1)


// val alertDialog = AlertDialog.Builder(requireContext())
// alertDialog.apply {
// setTitle("Success!")
// setMessage("Your smartphone has successfully connected with Trolley ID = (ID). " +
// " Happy Shopping!")
// setPositiveButton("OK") { dialog, which ->
// Navigation.findNavController(view).navigate(R.id.payFragment)
// }
// alertDialog.setNegativeButton("Cancel") { dialog, which ->
// dialog.cancel()
// }
// }
// alertDialog.create().show()
}

binding.imageViewInput.setOnClickListener {
Expand All @@ -60,17 +59,19 @@ class HomeFragment : Fragment() {
input.setHint("Enter ID")
input.inputType = InputType.TYPE_CLASS_NUMBER


val alertDialog = AlertDialog.Builder(requireContext())
alertDialog.setTitle("Input Trolley ID")
alertDialog.setView(input)
alertDialog.setPositiveButton("Connect") { dialog, which ->
var input = input.text.toString()
// var input = input.text.toString()
var input = Integer.parseInt(input.text.toString())

connectRaspi(idUser, input)

val alertDialogStatus = AlertDialog.Builder(requireContext())
alertDialogStatus.apply {
setTitle("Success!")
setMessage("Your smartphone has successfully connected with Trolley ID = " + input +
setMessage("Your smartphone has successfully connected with Trolley ID = " + input.toString() +
" Happy Shopping!")
setPositiveButton("OK") { dialog, which ->
Navigation.findNavController(view).navigate(R.id.payFragment)
Expand All @@ -86,4 +87,30 @@ class HomeFragment : Fragment() {
}.create().show()
}
}

private fun connectRaspi(id_user: Int, id_raspi: Int){
val retIn = RetrofitClient.apiInstance
val idInfo = UserRaspi(id_user, id_raspi)
retIn.connectRaspi(idInfo).enqueue(object : Callback<UserRaspiResponse> {
override fun onFailure(call: Call<UserRaspiResponse>, t: Throwable) {
Toast.makeText(requireContext(), "Timeout", Toast.LENGTH_SHORT).show()
}

override fun onResponse(call: Call<UserRaspiResponse>, response: Response<UserRaspiResponse>) {
if (response.code() == 200) {
Toast.makeText(requireContext(), response.body()!!.message, Toast.LENGTH_LONG).show()

} 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(requireContext(), errorMessage, Toast.LENGTH_SHORT).show()
}
}
})
}
}
6 changes: 6 additions & 0 deletions app/src/main/java/com/trolle/trolleapp/data/UserRaspi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.trolle.trolleapp.data

data class UserRaspi(
val id_user: Int,
val id_raspi: Int
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.trolle.trolleapp.data

data class UserRaspiResponse(
val message: String
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.trolle.trolleapp.data.network.api

import com.trolle.trolleapp.data.*
import com.trolle.trolleapp.data.sharedpref.SharedPreference
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.http.*
Expand All @@ -21,4 +22,10 @@ interface Api {
fun registerUser(
@Body info: User
): Call<ResponseBody>

@POST("raspiSession/requestSession")
@Headers("Authorization: Bearer " + "eyJhbGciOiJIUzI1NiJ9.aGFuaXA.TgV2hhHJFndbvucYSV_qN_fzBY5nGJRBOv7NcX2FdIw")
fun connectRaspi(
@Body info: UserRaspi
): Call<UserRaspiResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import retrofit2.create
import java.util.concurrent.TimeUnit

object RetrofitClient {
private const val BASE_URL = "http://192.168.100.22:3000/"
private const val BASE_URL = "http://192.168.100.37:3000/"

val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.trolle.trolleapp.data.sharedpref

import android.content.Context
import android.content.SharedPreferences

class SharedPreference(val context: Context) {
private val PREFS_NAME = "trolle_pref"
val sharedPref: SharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)

fun save(KEY_NAME: String, text: String) {

val editor: SharedPreferences.Editor = sharedPref.edit()

editor.putString(KEY_NAME, text)

editor!!.commit()
}

fun save(KEY_NAME: String, value: Int) {
val editor: SharedPreferences.Editor = sharedPref.edit()

editor.putInt(KEY_NAME, value)

editor.commit()
}

fun save(KEY_NAME: String, status: Boolean) {

val editor: SharedPreferences.Editor = sharedPref.edit()

editor.putBoolean(KEY_NAME, status!!)

editor.commit()
}

fun getValueString(KEY_NAME: String): String? {

return sharedPref.getString(KEY_NAME, null)

}

fun getValueInt(KEY_NAME: String): Int {

return sharedPref.getInt(KEY_NAME, 0)
}

fun getValueBoolien(KEY_NAME: String, defaultValue: Boolean): Boolean {

return sharedPref.getBoolean(KEY_NAME, defaultValue)

}

fun clearSharedPreference() {

val editor: SharedPreferences.Editor = sharedPref.edit()

//sharedPref = PreferenceManager.getDefaultSharedPreferences(context);

editor.clear()
editor.commit()
}

fun removeValue(KEY_NAME: String) {

val editor: SharedPreferences.Editor = sharedPref.edit()

editor.remove(KEY_NAME)
editor.commit()
}
}
15 changes: 10 additions & 5 deletions app/src/main/java/com/trolle/trolleapp/ui/signin/SignInActivity.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.trolle.trolleapp.ui.signin

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.trolle.trolleapp.data.SignInBody
import com.trolle.trolleapp.data.SignInResponse
import com.trolle.trolleapp.data.network.api.RetrofitClient
import com.trolle.trolleapp.data.sharedpref.SharedPreference
import com.trolle.trolleapp.databinding.ActivitySignInBinding
import com.trolle.trolleapp.ui.home.HomeActivity
import com.trolle.trolleapp.ui.signup.SignUpActivity
import okhttp3.ResponseBody
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Callback
Expand Down Expand Up @@ -51,22 +51,27 @@ class SignInActivity : AppCompatActivity() {
val signInInfo = SignInBody(username, password)
retIn.login(signInInfo).enqueue(object : Callback<SignInResponse> {
override fun onFailure(call: Call<SignInResponse>, t: Throwable) {
Toast.makeText(applicationContext, "on Failure", Toast.LENGTH_SHORT).show()
Toast.makeText(applicationContext, "Timeout", Toast.LENGTH_SHORT).show()
}

override fun onResponse(call: Call<SignInResponse>, response: Response<SignInResponse>) {
if (response.code() == 200) {
Toast.makeText(applicationContext, response.body()!!.message, Toast.LENGTH_LONG).show()

val sharedPreference: SharedPreference = SharedPreference(this@SignInActivity)
sharedPreference.save("id_user", response.body()!!.data.id)
sharedPreference.save("token", response.body()!!.data.token)

startActivity(Intent(applicationContext, HomeActivity::class.java))
finish()
} else {
var responseMessage = response.message()
response.errorBody()?.string()?.let {
val jsonObject = JSONObject(it)
val msg = jsonObject.getString("message")
val status = jsonObject.getString("status")
responseMessage = "$msg"
}
val errorMessage = "Error ${response.code()}: $responseMessage"
val errorMessage = "Error: $responseMessage"
Toast.makeText(applicationContext, errorMessage, Toast.LENGTH_SHORT).show()
}
}
Expand Down

0 comments on commit 81a1f85

Please sign in to comment.