import android.app.Activity;
import android.content.Intent;
import androidx.activity.result.ActivityResultContracts;
import androidx.activity.result.ActivityResultLauncher;
import androidx.lifecycle.MutableLiveData;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
const val TERMINAL_ID = "terminalId"
const val SERIAL_NO = "serialNo"
const val DEVICE_INFO_ARGUMENTS = "deviceInfoArguments"
const val DEVICE_INFO_RESULT = "deviceInfoResult"
const val DEVICE_INFO_INTENT = "com.nomba.pro.feature.device_setup.ACTION_VIEW"
val resultString = mutableStateOf("")
var deviceInfoResult = HashMap<String, String>()
val gson = GsonBuilder()
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
.create()
//setup a launcher that would help launch the intent
var deviceInfoResultLauncher = registerForActivityResult(
ActivityResultContracts
.StartActivityForResult()
) { result ->
val data: Intent? = result.data
if (result.resultCode == Activity.RESULT_OK) {
val type = object : TypeToken<HashMap<String, Any>>() {}.type
val resultJson = data?.getStringExtra(DEVICE_INFO_RESULT)
deviceInfoResult = gson.fromJson(resultJson, type)
val terminalId = deviceInfoResult[TERMINAL_ID]
val serialNo = deviceInfoResult[SERIAL_NO]
resultString.value = "Terminal Id: $terminalId Serial No: $serialNo"
}
}
//setup an intent to be triggered
val intent = Intent(DEVICE_INFO_INTENT)
//pass the terminalId and serialNo as extra data
intent.putExtra(DEVICE_INFO_ARGUMENTS, "$TERMINAL_ID,$SERIAL_NO")
//launch the intent
deviceInfoResultLauncher.launch(intent)