|
| 1 | +package org.oppia.app.profile |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.content.Intent |
| 5 | +import android.net.Uri |
| 6 | +import android.provider.MediaStore |
| 7 | +import android.text.Editable |
| 8 | +import android.text.TextWatcher |
| 9 | +import android.view.inputmethod.InputMethodManager |
| 10 | +import android.widget.ImageView |
| 11 | +import androidx.appcompat.app.AlertDialog |
| 12 | +import androidx.appcompat.app.AppCompatActivity |
| 13 | +import androidx.databinding.DataBindingUtil |
| 14 | +import androidx.lifecycle.Observer |
| 15 | +import com.bumptech.glide.Glide |
| 16 | +import com.bumptech.glide.request.RequestOptions |
| 17 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 18 | +import org.oppia.app.R |
| 19 | +import org.oppia.app.activity.ActivityScope |
| 20 | +import org.oppia.app.databinding.AddProfileActivityBinding |
| 21 | +import org.oppia.app.viewmodel.ViewModelProvider |
| 22 | +import org.oppia.domain.profile.ProfileManagementController |
| 23 | +import javax.inject.Inject |
| 24 | + |
| 25 | +const val GALLERY_INTENT_RESULT_CODE = 1 |
| 26 | + |
| 27 | +/** The presenter for [AddProfileActivity]. */ |
| 28 | +@ActivityScope |
| 29 | +class AddProfileActivityPresenter @Inject constructor( |
| 30 | + private val activity: AppCompatActivity, |
| 31 | + private val profileManagementController: ProfileManagementController, |
| 32 | + private val viewModelProvider: ViewModelProvider<AddProfileViewModel> |
| 33 | +) { |
| 34 | + lateinit var uploadImageView: ImageView |
| 35 | + private val addViewModel by lazy { |
| 36 | + getAddProfileViewModel() |
| 37 | + } |
| 38 | + private var selectedImage: Uri? = null |
| 39 | + var allowDownloadAccess = false |
| 40 | + var inputtedPin = false |
| 41 | + var inputtedConfirmPin = false |
| 42 | + |
| 43 | + @ExperimentalCoroutinesApi |
| 44 | + fun handleOnCreate() { |
| 45 | + activity.title = "Add Profile" |
| 46 | + activity.supportActionBar?.setDisplayHomeAsUpEnabled(true) |
| 47 | + activity.supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_close_white_24dp) |
| 48 | + |
| 49 | + val binding = DataBindingUtil.setContentView<AddProfileActivityBinding>(activity, R.layout.add_profile_activity) |
| 50 | + |
| 51 | + binding.apply { |
| 52 | + viewModel = addViewModel |
| 53 | + } |
| 54 | + |
| 55 | + binding.allowDownloadSwitch.setOnCheckedChangeListener { _, isChecked -> |
| 56 | + allowDownloadAccess = isChecked |
| 57 | + } |
| 58 | + |
| 59 | + binding.infoIcon.setOnClickListener { |
| 60 | + showInfoDialog() |
| 61 | + } |
| 62 | + |
| 63 | + addTextChangeListeners(binding) |
| 64 | + |
| 65 | + binding.uploadImageButton.setOnClickListener { |
| 66 | + val galleryIntent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI) |
| 67 | + activity.startActivityForResult(galleryIntent, GALLERY_INTENT_RESULT_CODE) |
| 68 | + } |
| 69 | + uploadImageView = binding.uploadImageButton |
| 70 | + |
| 71 | + binding.createButton.setOnClickListener { |
| 72 | + addViewModel.clearAllErrorMessages() |
| 73 | + val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager |
| 74 | + imm?.hideSoftInputFromWindow(activity.currentFocus?.windowToken, 0) |
| 75 | + val name = binding.inputName.getInput() |
| 76 | + val pin = binding.inputPin.getInput() |
| 77 | + val confirmPin = binding.inputConfirmPin.getInput() |
| 78 | + var failed = false |
| 79 | + if (name.isEmpty()) { |
| 80 | + addViewModel.nameErrorMsg.set(activity.resources.getString(R.string.add_profile_error_name_empty)) |
| 81 | + failed = true |
| 82 | + } |
| 83 | + if (pin.isNotEmpty() && pin.length < 3) { |
| 84 | + addViewModel.pinErrorMsg.set(activity.resources.getString(R.string.add_profile_error_pin_length)) |
| 85 | + failed = true |
| 86 | + } |
| 87 | + if (pin != confirmPin) { |
| 88 | + addViewModel.confirmPinErrorMsg.set(activity.resources.getString(R.string.add_profile_error_pin_confirm_wrong)) |
| 89 | + failed = true |
| 90 | + } |
| 91 | + if (failed) { |
| 92 | + binding.scroll.smoothScrollTo(0,0) |
| 93 | + return@setOnClickListener |
| 94 | + } |
| 95 | + profileManagementController.addProfile(name, pin, selectedImage, allowDownloadAccess, isAdmin = false).observe(activity, Observer { |
| 96 | + if (it.isSuccess()) { |
| 97 | + val intent = Intent(activity, ProfileActivity::class.java) |
| 98 | + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) |
| 99 | + activity.startActivity(intent) |
| 100 | + } else if (it.isFailure()) { |
| 101 | + when (it.getErrorOrNull()) { |
| 102 | + is ProfileManagementController.ProfileNameNotUniqueException -> addViewModel.nameErrorMsg.set(activity.resources.getString(R.string.add_profile_error_name_not_unique)) |
| 103 | + is ProfileManagementController.ProfileNameOnlyLettersException -> addViewModel.nameErrorMsg.set(activity.resources.getString(R.string.add_profile_error_name_only_letters)) |
| 104 | + } |
| 105 | + binding.scroll.smoothScrollTo(0,0) |
| 106 | + } |
| 107 | + }) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + fun handleOnActivityResult(data: Intent?) { |
| 112 | + data?.let { |
| 113 | + selectedImage = data.data |
| 114 | + Glide.with(activity) |
| 115 | + .load(selectedImage) |
| 116 | + .centerCrop() |
| 117 | + .apply(RequestOptions.circleCropTransform()) |
| 118 | + .into(uploadImageView) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private fun addTextChangeListeners(binding: AddProfileActivityBinding) { |
| 123 | + fun setValidPin() { |
| 124 | + if (inputtedPin && inputtedConfirmPin) { |
| 125 | + addViewModel.validPin.set(true) |
| 126 | + } else { |
| 127 | + binding.allowDownloadSwitch.isChecked = false |
| 128 | + addViewModel.validPin.set(false) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + binding.inputPin.addTextChangedListener(object: TextWatcher { |
| 133 | + override fun onTextChanged(pin: CharSequence?, start: Int, before: Int, count: Int) { |
| 134 | + pin?.let { |
| 135 | + addViewModel.pinErrorMsg.set("") |
| 136 | + inputtedPin = it.isNotEmpty() |
| 137 | + setValidPin() |
| 138 | + } |
| 139 | + } |
| 140 | + override fun afterTextChanged(confirmPin: Editable?) {} |
| 141 | + override fun beforeTextChanged(p0: CharSequence?, start: Int, count: Int, after: Int) {} |
| 142 | + }) |
| 143 | + |
| 144 | + binding.inputConfirmPin.addTextChangedListener(object: TextWatcher { |
| 145 | + override fun onTextChanged(confirmPin: CharSequence?, start: Int, before: Int, count: Int) { |
| 146 | + confirmPin?.let { |
| 147 | + addViewModel.confirmPinErrorMsg.set("") |
| 148 | + inputtedConfirmPin = confirmPin.isNotEmpty() |
| 149 | + setValidPin() |
| 150 | + } |
| 151 | + } |
| 152 | + override fun afterTextChanged(confirmPin: Editable?) {} |
| 153 | + override fun beforeTextChanged(p0: CharSequence?, start: Int, count: Int, after: Int) {} |
| 154 | + }) |
| 155 | + |
| 156 | + binding.inputName.addTextChangedListener(object: TextWatcher { |
| 157 | + override fun onTextChanged(confirmPin: CharSequence?, start: Int, before: Int, count: Int) { |
| 158 | + confirmPin?.let { |
| 159 | + addViewModel.nameErrorMsg.set("") |
| 160 | + } |
| 161 | + } |
| 162 | + override fun afterTextChanged(confirmPin: Editable?) {} |
| 163 | + override fun beforeTextChanged(p0: CharSequence?, start: Int, count: Int, after: Int) {} |
| 164 | + }) |
| 165 | + } |
| 166 | + |
| 167 | + private fun showInfoDialog() { |
| 168 | + AlertDialog.Builder(activity as Context, R.style.AlertDialogTheme) |
| 169 | + .setMessage(R.string.add_profile_pin_info) |
| 170 | + .setPositiveButton(R.string.add_profile_close) { dialog, _ -> |
| 171 | + dialog.dismiss() |
| 172 | + }.create().show() |
| 173 | + } |
| 174 | + |
| 175 | + private fun getAddProfileViewModel(): AddProfileViewModel { |
| 176 | + return viewModelProvider.getForActivity(activity, AddProfileViewModel::class.java) |
| 177 | + } |
| 178 | +} |
0 commit comments