I have second session manager where I store order data ( first session manager is for login ) anyway, while my requests are storing successfully in server database data are not storing by session manager therefore I have wrong details in other fragments (because they’re relay on this session status).
session manager
class OrderSessionManager {
lateinit var pref: SharedPreferences
lateinit var editor: SharedPreferences.Editor
lateinit var con: Context
var PRIVATE_MODE: Int = 0
constructor(con: Context?) {
if (con != null) {
this.con = con
}
if (con != null) {
pref = con.getSharedPreferences(PREF_NAME,PRIVATE_MODE)
}
editor = pref.edit()
}
companion object {
val PREF_NAME: String = "OrdersSession"
val ORDERINPROGRESS: String = "orderInProgress"
val KEY_DRIVER_ID: String = "id"
val KEY_CUSTOMER_ID: String = "user.id"
val KEY_CUSTOMER_NAME: String = "user.name"
val KEY_CUSTOMER_ADDRESS: String = "user.address"
val KEY_CUSTOMER_PHOTO: String = "user.photo"
val KEY_ORDER_ID: String = "order.id"
}
fun createOrderSession(
customer_name: String,
customer_address: String,
customer_photo: String,
driver_id: String,
order_id: String?,
) {
editor.putBoolean(ORDERINPROGRESS,true)
editor.putString(KEY_CUSTOMER_NAME,customer_name)
editor.putString(KEY_CUSTOMER_ADDRESS,customer_address)
editor.putString(KEY_CUSTOMER_PHOTO,customer_photo)
editor.putString(KEY_DRIVER_ID,driver_id)
editor.putString(KEY_ORDER_ID,order_id)
editor.commit()
}
fun checkOrder(){
if(!this.isOrderInProgress()){
var i: Intent = Intent(con, OrderActivity::class.java)
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
con.startActivity(i)
}
}
fun getOrderDetails(): Map<String, String>
{
var currentorder: Map<String, String> = HashMap<String, String>()
pref.getString(KEY_DRIVER_ID,null)?.let { (currentorder as HashMap).put(KEY_DRIVER_ID, it) }
pref.getString(KEY_CUSTOMER_ID,null)?.let { (currentorder as HashMap).put(KEY_CUSTOMER_ID, it) }
pref.getString(KEY_CUSTOMER_NAME,null)?.let { (currentorder as HashMap).put(KEY_CUSTOMER_NAME, it) }
pref.getString(KEY_CUSTOMER_ADDRESS,null)?.let { (currentorder as HashMap).put(KEY_CUSTOMER_ADDRESS, it) }
pref.getString(KEY_CUSTOMER_PHOTO,null)?.let { (currentorder as HashMap).put(KEY_CUSTOMER_PHOTO, it) }
pref.getString(KEY_ORDER_ID,null)?.let { (currentorder as HashMap).put(KEY_ORDER_ID, it) }
return currentorder
}
fun DeleteCurrentOrder() {
pref.getString(KEY_DRIVER_ID,null)
pref.getString(KEY_ORDER_ID,null)
pref.getString(KEY_CUSTOMER_ID,null)
pref.getString(KEY_CUSTOMER_NAME,null)
pref.getString(KEY_CUSTOMER_ADDRESS,null)
pref.getString(KEY_CUSTOMER_PHOTO,null)
pref.getString(KEY_ORDER_ID,null)
// var i: Intent = Intent(con, OrderActivity::class.java)
// i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
// i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
// con.startActivity(i)
}
fun isOrderInProgress(): Boolean
{
return pref.getBoolean(ORDERINPROGRESS,false)
}
}
storing data on API request success
private fun getDriverID() {
var session = SessionManager(context)
session.checkLogin()
var user = session.getUserDetails()
var uId: String? = user.get(SessionManager.KEY_ID)
var token: String? = user.get(SessionManager.KEY_ACCESS_TOKEN)
val tokenFull = "Bearer $token"
val queue = Volley.newRequestQueue(context)
val url = "https://example.com.id/api/getDriverDetails"
val stringReq : StringRequest =
object : StringRequest(
Method.GET, url,
Response.Listener { response ->
val responseObject = Gson().fromJson(response, DriverDetails::class.java)
driver_ID = responseObject.data.id
// Store data in session (doesn't work)
responseObject.data.user.name?.let { customer_name ->
responseObject.data.user.address?.let { customer_address ->
responseObject.data.user.photo?.let { customer_photo ->
responseObject.data.id?.let { driver_id -> // somehow this return wrong id that's why I've create above line (driver_ID = responseObject.data.id) but it's not my concern for this question at the time
orderIDArgument.let { order_id ->
orderSession.createOrderSession(
customer_name,
customer_address,
customer_photo,
driver_id,
order_id
)
}
}
}
}
}
},
Response.ErrorListener { error ->
Toast.makeText(context, error.message, Toast.LENGTH_SHORT)
.show()
}
){
override fun getHeaders(): Map<String, String> {
val headers = HashMap<String, String>()
headers["Content-Type"] = "application/json"
headers["Authorization"] = tokenFull
return headers
}
}
queue.add(stringReq)
}