From 13ee40af94f4a02258b28a8d55ba615e33c62e58 Mon Sep 17 00:00:00 2001 From: Sergio De la torre Date: Tue, 20 Jun 2023 16:38:55 +0200 Subject: [PATCH] refs #5613 user, password not saved --- .../java/es/verdnatura/MobileApplication.kt | 2 + .../java/es/verdnatura/domain/ApiUtils.kt | 4 +- .../es/verdnatura/domain/SilexInterceptor.kt | 5 +- .../presentation/base/BaseFragment.kt | 237 ++---------------- .../ajustes/fragment/AjustesFragment.kt | 3 +- .../ControlVehiculoUsuarioFragment.kt | 4 +- .../feature/login/fragment/LoginFragment.kt | 52 ++-- .../feature/login/fragment/LoginViewModel.kt | 4 +- 8 files changed, 55 insertions(+), 256 deletions(-) diff --git a/app/src/main/java/es/verdnatura/MobileApplication.kt b/app/src/main/java/es/verdnatura/MobileApplication.kt index 6d95a230..e15afbaf 100644 --- a/app/src/main/java/es/verdnatura/MobileApplication.kt +++ b/app/src/main/java/es/verdnatura/MobileApplication.kt @@ -16,6 +16,8 @@ class MobileApplication : Application() { lateinit var silex: VerdnaturaService lateinit var salix: SalixService var userId: Int? = null + var userName: String? = null + var userPassword: String? = null var mperror: MediaPlayer? = null var mpok: MediaPlayer? = null private lateinit var customDialog: CustomDialogMainActivity diff --git a/app/src/main/java/es/verdnatura/domain/ApiUtils.kt b/app/src/main/java/es/verdnatura/domain/ApiUtils.kt index 3f4c1743..c7334f9c 100644 --- a/app/src/main/java/es/verdnatura/domain/ApiUtils.kt +++ b/app/src/main/java/es/verdnatura/domain/ApiUtils.kt @@ -47,8 +47,8 @@ class ApiUtils { PreferenceManager.getDefaultSharedPreferences(context) val editor = preferences.edit() editor.putString(key, value) - //editor.commit() - editor.apply() + editor.commit() + //editor.apply() } fun getDefaults(key: String?, context: Context?): String? { diff --git a/app/src/main/java/es/verdnatura/domain/SilexInterceptor.kt b/app/src/main/java/es/verdnatura/domain/SilexInterceptor.kt index 6b990972..2aa78eab 100644 --- a/app/src/main/java/es/verdnatura/domain/SilexInterceptor.kt +++ b/app/src/main/java/es/verdnatura/domain/SilexInterceptor.kt @@ -2,6 +2,7 @@ package es.verdnatura.domain import android.content.Context import android.content.SharedPreferences +import es.verdnatura.MobileApplication import okhttp3.FormBody import okhttp3.Interceptor import okhttp3.RequestBody @@ -29,8 +30,8 @@ class SilexInterceptor : Interceptor { val newRequest = request.newBuilder() .addHeader("aplicacion", "json") .addHeader("version", "1") - .addHeader("user", prefs.getString("user", null)) - .addHeader("pass", prefs.getString("password", null)) + .addHeader("user",(context as MobileApplication).userName) + .addHeader("pass", (context as MobileApplication).userPassword) .addHeader("Authorization", prefs.getString("token", "")) .addHeader("Content-Type", "application/json") .build() diff --git a/app/src/main/java/es/verdnatura/presentation/base/BaseFragment.kt b/app/src/main/java/es/verdnatura/presentation/base/BaseFragment.kt index 7349f4ae..a74d89b7 100644 --- a/app/src/main/java/es/verdnatura/presentation/base/BaseFragment.kt +++ b/app/src/main/java/es/verdnatura/presentation/base/BaseFragment.kt @@ -19,6 +19,7 @@ import androidx.preference.PreferenceManager import androidx.room.Room import com.google.gson.Gson import com.google.gson.reflect.TypeToken +import es.verdnatura.MobileApplication import es.verdnatura.R import es.verdnatura.databinding.ToolbarFragmentBinding import es.verdnatura.db.ExpedicionDatabase @@ -36,7 +37,7 @@ import kotlin.reflect.KClass abstract class BaseFragment(viewModelClass: KClass) : Fragment() { - + protected lateinit var mobileApplication: MobileApplication protected val TAG = "VERDNATURA::" protected val PREFS_USER = "es.verdnatura.user.prefs" protected val PREFS_SERVER = "es.verdnatura.server.prefs" @@ -111,7 +112,7 @@ abstract class BaseFragment(viewModelCla ma = activity as MainActivity } - + mobileApplication = requireActivity().application as MobileApplication } override fun onCreateView( @@ -149,29 +150,13 @@ abstract class BaseFragment(viewModelCla } - @LayoutRes abstract fun getLayoutId(): Int open fun init() {} open fun observeViewModel() {} open fun getBundleArguments() {} open fun addBindingVariables() {} - - open fun restartapp(f: Fragment) { - val mStartActivity = Intent(context, f::class.java) - val mPendingIntentId = 123456 - val mPendingIntent = PendingIntent.getActivity( - context, - mPendingIntentId, - mStartActivity, - PendingIntent.FLAG_CANCEL_CURRENT - ) - val mgr = requireContext().getSystemService(Context.ALARM_SERVICE) as AlarmManager - mgr[AlarmManager.RTC, System.currentTimeMillis() + 100] = mPendingIntent - System.exit(0) - } - - + private fun initDataBinding() { binding = DataBindingUtil.bind(requireView())!! binding.lifecycleOwner = viewLifecycleOwner @@ -233,12 +218,12 @@ abstract class BaseFragment(viewModelCla R.drawable.ic_eye_ui -> getString(R.string.icViewCollection) R.drawable.ic_collection -> getString(R.string.icViewCollection) R.drawable.ic_ubicator_check -> getString(R.string.checkerUbication) - R.drawable.ic_delivery_truck ->"Abre la app de Reparto para escanear carga" - R.drawable.ic_confirm ->getString(R.string.confirmDescription) + R.drawable.ic_delivery_truck -> "Abre la app de Reparto para escanear carga" + R.drawable.ic_confirm -> getString(R.string.confirmDescription) - R.drawable.camion ->getString(R.string.openLoadingConsignee) - R.drawable.exit ->getString(R.string.exitScreen) - R.drawable.ic_image_upload->getString(R.string.messageUpload) + R.drawable.camion -> getString(R.string.openLoadingConsignee) + R.drawable.exit -> getString(R.string.exitScreen) + R.drawable.ic_image_upload -> getString(R.string.messageUpload) else -> { @@ -247,201 +232,7 @@ abstract class BaseFragment(viewModelCla } } -/* - override fun onInit(status: Int) { - /* if (status == TextToSpeech.SUCCESS) { - val spanish = Locale("es", "ES") - textToSpeech!!.language = spanish - textToSpeech!!.setOnUtteranceProgressListener(object : UtteranceProgressListener() { - override fun onDone(utteranceId: String?) { - //Log.i("SPEEAK", "on done") - requireActivity().runOnUiThread(Runnable { - if (mSpeechRecognizer != null) { - mSpeechRecognizer!!.destroy() - mSpeechRecognizer = null - } - initialize() - }) - } - - override fun onError(utteranceId: String?) { - "Error to speak".toast(requireContext()) - } - - override fun onStart(utteranceId: String?) { - Log.i("SPEEAK", "on start") - runSound() - - } - - }) - } else { - "La voz no se ha podido iniciar".toast(requireContext()) - }*/ - } -*/ - /* private fun runSound() { - // Get the maximum media/music volume - //sergio: se quita por no utilizar - // val maxVolume = mAudioManager?.mediaMaxVolume - // mAudioManager?.setMediaVolume(maxVolume!!) - }*/ - - /* private fun muteSound() { - if (mAudioManager?.mediaCurrentVolume != 0) { - // mAudioManager?.setMediaVolume(0) - } - - }*/ - - /* fun speak(frase: String) { - textToSpeech!!.speak(frase, TextToSpeech.QUEUE_FLUSH, null, "frase") - } - - open fun startListening() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - //muteSound() - } - mSpeechRecognizer!!.startListening(mSpeechRecognizerIntent) - } - - - override fun onReadyForSpeech(params: Bundle?) { - isOnReadyForSpeech = true - // Log.i("Speech", "onReadyForSpeech") - } - - override fun onBeginningOfSpeech() { - // Log.i("Speech", "onBeginningOfSpeech") - } - - override fun onRmsChanged(rmsdB: Float) {} - - override fun onBufferReceived(buffer: ByteArray?) { - // Log.i("Speech", "onBufferReceived") - } - - override fun onEndOfSpeech() { - // Log.i("Speech", "onEndOfSpeech") - } - - override fun onError(error: Int) { - if (!isOnReadyForSpeech && error == SpeechRecognizer.ERROR_NO_MATCH) { - (getString(R.string.speedProblems)).toast(requireContext()) - return - } else { - //Log.i("Speech", "onError " + error) - if (mSpeechRecognizer != null) mSpeechRecognizer!!.destroy() - mSpeechRecognizer = null - //initialize() - } - - } - - override fun onResults(results: Bundle) {} - - override fun onPartialResults(partialResults: Bundle?) { - //Log.i("Speech", "onPartialResults") - } - - - - override fun onEvent(eventType: Int, params: Bundle?) { - //Log.i("Speech", "onEvent") - }*/ - - /* open fun checkText(text: String): Int { - - //check for nuevo - val nuevo = getMatch(text, "nuevo") - - //check for listo - val listo = getMatch(text, "listo") - - //check for cancelar - val cancelar = getMatch(text, getString(R.string.cancel)) - - //check for volver - val volver = getMatch(text, "volver") - - //check for repite - val repite = getMatch(text, "repite") - - //check for siguiente - val otro = getMatch(text, "otro") - - //check for FALTA - val falta = getMatch(text, "falta") - val words = IntArray(7) - words[0] = nuevo - words[1] = listo - words[2] = cancelar - words[3] = volver - words[4] = repite - words[5] = otro - words[6] = falta - return getMax(words) - } - - private fun getMax(a: IntArray): Int { - var max = a[0] - var pos = 0 - for (i in a.indices) { - if (a[i] > max) { - max = a[i] - pos = i - } - } - return if (max < 80) { - -1 - } else pos - } - - private fun getMatch(a: String, b: String): Int { - var count = 0 - val cha = a.toCharArray() - val chb = b.toCharArray() - for (i in cha) { - for (x in chb) { - if (x == i) { - count += 1 - break - } - } - } - val numChar = a.length - if (numChar != 0) - return count * 100 / numChar - else - return 100 - }*/ - - /* private fun requestReadPhoneStatePermission(){ - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - val requiredPermission: String = Manifest.permission.READ_PHONE_STATE - val res = context!!.checkCallingOrSelfPermission(requiredPermission) - if (res == PackageManager.PERMISSION_DENIED) { - requestPermissions(arrayOf(requiredPermission), 102) - } - } - - }*/ - - - /*sergio:deprecated - private fun requestRecordAudioPermission() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - val requiredPermission: String = Manifest.permission.RECORD_AUDIO - val res = requireContext().checkCallingOrSelfPermission(requiredPermission) - // If the user previously denied this permission then show a message explaining why - // this permission is needed - if (res == PackageManager.PERMISSION_DENIED) { - requestPermissions(arrayOf(requiredPermission), 101) - - } - } - }*/ private fun requestCallPhonePermission() { val permissionLauncher = registerForActivityResult( @@ -513,7 +304,7 @@ abstract class BaseFragment(viewModelCla WAREHOUSEFK -> prefs.getInt(name, WAREHOUSEFKDEFAULT) PRINTERFK -> prefs.getInt(name, 0) USERFK -> prefs.getInt(USERFK, 0) - SUPPLIERID->prefs.getInt(SUPPLIERID,0) + SUPPLIERID -> prefs.getInt(SUPPLIERID, 0) else -> { 0 } @@ -699,7 +490,7 @@ abstract class BaseFragment(viewModelCla urlSalix = "http://localhost:3000" ) ) - }else{ + } else { deleteWorkForm("all") } saveWorkForm( @@ -731,18 +522,22 @@ abstract class BaseFragment(viewModelCla editorPref.commit() } + "base_urlSalix" -> { val preferences = PreferenceManager.getDefaultSharedPreferences(context) val editorPref = preferences.edit() editorPref.putString("base_urlSalix", value.toString()) editorPref.commit() } + DEPARTMENTMISTAKEID -> { editor.putInt(name, value as Int) } + "SUPPLIERID" -> { editor.putString(name, value.toString()) } + else -> { editor.putString(name, value.toString()) } @@ -838,7 +633,7 @@ abstract class BaseFragment(viewModelCla } - fun database(myContext:Context): ExpedicionDatabase { +fun database(myContext: Context): ExpedicionDatabase { val db = Room.databaseBuilder( myContext, diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/ajustes/fragment/AjustesFragment.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/ajustes/fragment/AjustesFragment.kt index f1da51a4..11dff9c1 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/ajustes/fragment/AjustesFragment.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/ajustes/fragment/AjustesFragment.kt @@ -65,7 +65,7 @@ class AjustesFragment : hideBackButton(binding.mainToolbar) viewModel.worker_isF11Allowed() - binding.userText.setText(getData(USER)) + binding.userText.text = mobileApplication.userName val versionName = requireActivity().packageManager.getPackageInfo( requireActivity().packageName, 0 @@ -114,7 +114,6 @@ class AjustesFragment : if (item.id == 0) { binding.splashProgress.visibility = VISIBLE - // viewModel.getSectors(getData(USER),getData(PASSWORD)) viewModel.getSectorsSalix() } else if (item.id == 5) { requireActivity().onBackPressed() diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/controlvehiculo/fragment/ControlVehiculoUsuarioFragment.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/controlvehiculo/fragment/ControlVehiculoUsuarioFragment.kt index 16792487..16b2e772 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/controlvehiculo/fragment/ControlVehiculoUsuarioFragment.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/controlvehiculo/fragment/ControlVehiculoUsuarioFragment.kt @@ -85,7 +85,7 @@ class ControlVehiculoUsuarioFragment( private fun setControlVehicleUser() { userFk = getDataInt(USERFK) - if (userFk==vUserFK.toInt()) { + if (userFk == vUserFK.toInt()) { binding.splashProgress.visibility = VISIBLE viewModel.machine_getWorkerPlate() viewModel.deviceProduction_getnameDevice( @@ -110,7 +110,7 @@ class ControlVehiculoUsuarioFragment( ) }" ) - binding.nameVehiclecontrol.text = getData(USER) + binding.nameVehiclecontrol.text = mobileApplication.userName binding.registeredVehiclecontrol.text = "" binding.deviceName.text = "" binding.assignedDevicename.text = "" diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/login/fragment/LoginFragment.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/login/fragment/LoginFragment.kt index 50ef958d..bd2b231e 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/login/fragment/LoginFragment.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/login/fragment/LoginFragment.kt @@ -81,6 +81,7 @@ class LoginFragment(var imageUri: Uri?) : binding.edittextServer.setText(getData(WORKFORMSELECTED)) + if (binding.edittextServer.text.toString() == "TestLOCAL") { binding.edittextUsername.setText("developer") binding.edittextPassword.setText("nightmare") @@ -371,7 +372,6 @@ class LoginFragment(var imageUri: Uri?) : } viewModel.device_checkLogin( - getData(USER), getData(ANDROID_ID) ) viewModel.operator_getNumberOfWagons() @@ -421,30 +421,30 @@ class LoginFragment(var imageUri: Uri?) : } - /* loadOperatorList.observe(viewLifecycleOwner, Observer { event -> - //binding.splashProgress.visibility = View.GONE + /* loadOperatorList.observe(viewLifecycleOwner, Observer { event -> + //binding.splashProgress.visibility = View.GONE - event.getContentIfNotHandled().notNull { + event.getContentIfNotHandled().notNull { - if (it.list.isEmpty()) { - "No se han podido obtener datos de Salix".toast(context) - removePrinter() + if (it.list.isEmpty()) { + "No se han podido obtener datos de Salix".toast(context) + removePrinter() - } else { - if (!it.list.get(0).isError) { - saveDataInt(NUMBEROFWAGONS, it.list[0].numberOfWagons) - saveDataInt(PRINTERFK, it.list[0].labelerFk!!) - saveDataInt(TRAINFK, it.list[0].trainFk) - saveData(ITEMPACKINGFK, it.list[0].itemPackingTypeFk) + } else { + if (!it.list.get(0).isError) { + saveDataInt(NUMBEROFWAGONS, it.list[0].numberOfWagons) + saveDataInt(PRINTERFK, it.list[0].labelerFk!!) + saveDataInt(TRAINFK, it.list[0].trainFk) + saveData(ITEMPACKINGFK, it.list[0].itemPackingTypeFk) - //saveData(PRINTERNAME, it.list.get(0).name) - } else { - it.list.get(0).errorMessage.toast(context) - } - } - } + //saveData(PRINTERNAME, it.list.get(0).name) + } else { + it.list.get(0).errorMessage.toast(context) + } + } + } - })*/ + })*/ loadWorkerPrintList.observe(viewLifecycleOwner, Observer { event -> @@ -493,11 +493,13 @@ class LoginFragment(var imageUri: Uri?) : }.show() } else { saveTokenPref(it.token) - saveUserAccesPref( + /* saveUserAccesPref( binding.edittextUsername.text.toString(), binding.edittextPassword.text.toString() - ) - setupFirebase() + )*/ + app.userName = binding.edittextUsername.text.toString() + app.userPassword = binding.edittextPassword.text.toString() + setupFirebase(app.userName!!) //Tarea 4351 quitar el if y poner e device_checkLogin a continuaciĆ³n del response de operator_add viewModel.operator_add() @@ -509,8 +511,8 @@ class LoginFragment(var imageUri: Uri?) : super.observeViewModel() } - private fun setupFirebase() { - Firebase.crashlytics.setUserId(getData(USER)) + private fun setupFirebase(userName: String) { + Firebase.crashlytics.setUserId(userName) } private fun getInfoVersionNameApp(): String { diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/login/fragment/LoginViewModel.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/login/fragment/LoginViewModel.kt index e52e7c30..3402a198 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/login/fragment/LoginViewModel.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/login/fragment/LoginViewModel.kt @@ -123,9 +123,9 @@ class LoginViewModel(val context: Context) : BaseViewModel(context) { }) } - fun device_checkLogin(user: String, android_id: String) { + fun device_checkLogin( android_id: String) { - silex.device_checkLogin(user, android_id) + silex.device_checkLogin(android_id) .enqueue(object : SilexCallback(context) { override fun onSuccess(response: Response) { if (response.body() != null) {