package mx.trackermap.TrackerMap.android.session import android.content.* import android.os.Bundle import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Toast import androidx.core.widget.doAfterTextChanged import androidx.fragment.app.Fragment import androidx.localbroadcastmanager.content.LocalBroadcastManager import androidx.preference.PreferenceManager import com.zhuinden.liveevent.observe import kotlinx.coroutines.DelicateCoroutinesApi import mx.trackermap.TrackerMap.android.R import mx.trackermap.TrackerMap.android.databinding.LoginBinding import mx.trackermap.TrackerMap.android.units.UnitsActivity import mx.trackermap.TrackerMap.controllers.SessionController import org.koin.androidx.viewmodel.ext.android.viewModel import kotlin.time.ExperimentalTime @DelicateCoroutinesApi @ExperimentalTime class LoginFragment : Fragment() { private var _binding: LoginBinding? = null private val binding get() = _binding!! private val loginViewModel: LoginViewModel by viewModel() private lateinit var broadcastManager: LocalBroadcastManager override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { _binding = LoginBinding.inflate(inflater, container, false) return binding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) setupEvents() setupObservers() broadcastManager = LocalBroadcastManager.getInstance(activity!!) loginViewModel.restoreSession() } override fun onStart() { super.onStart() val intentFilter = IntentFilter(EVENT_TOKEN) broadcastManager.registerReceiver(broadcastReceiver, intentFilter) } override fun onStop() { super.onStop() broadcastManager.unregisterReceiver(broadcastReceiver) } override fun onDestroyView() { super.onDestroyView() _binding = null } private fun setupEvents() { binding.signinButton.setOnClickListener { loginViewModel.login( binding.usernameEditText.text.toString(), binding.passwordEditText.text.toString(), PreferenceManager .getDefaultSharedPreferences(activity) .getString(PREFERENCE_TOKEN, null) ) } } private fun setupObservers() { loginViewModel.loginState.observe(viewLifecycleOwner) { result -> Log.d("LoginFragment", result.toString()) when (result) { SessionController.LoginState.Nothing -> { binding.infoLoading.root.visibility = View.GONE } SessionController.LoginState.Loading -> { binding.infoLoading.root.visibility = View.VISIBLE } SessionController.LoginState.EmailMissing -> { binding.infoLoading.root.visibility = View.GONE Toast.makeText(context, getString(R.string.login_username_missing), Toast.LENGTH_SHORT).show() } SessionController.LoginState.PasswordMissing -> { binding.infoLoading.root.visibility = View.GONE Toast.makeText(context, getString(R.string.login_password_missing), Toast.LENGTH_SHORT).show() } SessionController.LoginState.Failure -> { binding.infoLoading.root.visibility = View.GONE Toast.makeText(context, getString(R.string.login_login_failed), Toast.LENGTH_SHORT).show() } SessionController.LoginState.Success -> { broadcastManager.sendBroadcast(Intent(EVENT_LOGIN)) val activity = requireActivity() val intent = Intent(activity.applicationContext, UnitsActivity::class.java) intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK startActivity(intent) activity.finish() } else -> {} } } } private val broadcastReceiver = object : BroadcastReceiver() { override fun onReceive(p0: Context?, p1: Intent?) { p1?.getStringExtra(KEY_TOKEN)?.let { token -> PreferenceManager .getDefaultSharedPreferences(activity) .edit() .putString(PREFERENCE_TOKEN, token) .apply() } } } companion object { const val EVENT_LOGIN = "eventLogin" const val EVENT_TOKEN = "eventToken" const val KEY_TOKEN = "keyToken" const val PREFERENCE_TOKEN = "token" } }