2021-01-26 19:45:18 +01:00
|
|
|
package sushi.hardcore.aira
|
|
|
|
|
2021-05-27 20:15:03 +02:00
|
|
|
import android.content.Context
|
2021-01-26 19:45:18 +01:00
|
|
|
import android.content.Intent
|
|
|
|
import android.os.Bundle
|
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import android.widget.Toast
|
2021-05-27 20:15:03 +02:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2021-01-26 19:45:18 +01:00
|
|
|
import sushi.hardcore.aira.databinding.FragmentCreateIdentityBinding
|
2021-05-27 20:15:03 +02:00
|
|
|
import sushi.hardcore.aira.utils.AvatarPicker
|
2021-01-26 19:45:18 +01:00
|
|
|
|
2021-05-27 20:15:03 +02:00
|
|
|
class CreateIdentityFragment(private val activity: AppCompatActivity) : Fragment() {
|
2021-01-26 19:45:18 +01:00
|
|
|
private external fun createNewIdentity(databaseFolder: String, name: String, password: ByteArray?): Boolean
|
|
|
|
|
|
|
|
companion object {
|
2021-05-27 20:15:03 +02:00
|
|
|
fun newInstance(activity: AppCompatActivity): CreateIdentityFragment {
|
|
|
|
return CreateIdentityFragment(activity)
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-27 20:15:03 +02:00
|
|
|
private val avatarPicker = AvatarPicker(activity) { picker, avatar ->
|
|
|
|
picker.setOnAvatarCompressed { compressedAvatar ->
|
|
|
|
AIRADatabase.setIdentityAvatar(Constants.getDatabaseFolder(activity), compressedAvatar)
|
|
|
|
}
|
|
|
|
avatar.circleCrop().into(binding.avatar)
|
|
|
|
}
|
2021-01-26 19:45:18 +01:00
|
|
|
private lateinit var binding: FragmentCreateIdentityBinding
|
|
|
|
|
2021-05-27 20:15:03 +02:00
|
|
|
override fun onAttach(context: Context) {
|
|
|
|
super.onAttach(context)
|
|
|
|
avatarPicker.register()
|
|
|
|
}
|
|
|
|
|
2021-01-26 19:45:18 +01:00
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
|
|
|
binding = FragmentCreateIdentityBinding.inflate(inflater, container, false)
|
|
|
|
return binding.root
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
2021-05-27 20:15:03 +02:00
|
|
|
binding.buttonSetAvatar.setOnClickListener {
|
|
|
|
avatarPicker.launch()
|
|
|
|
}
|
2021-01-26 19:45:18 +01:00
|
|
|
binding.checkboxEnablePassword.setOnCheckedChangeListener { _, isChecked ->
|
|
|
|
if (isChecked) {
|
|
|
|
binding.editPassword.visibility = View.VISIBLE
|
|
|
|
binding.editPasswordConfirm.visibility = View.VISIBLE
|
|
|
|
} else {
|
|
|
|
binding.editPassword.visibility = View.GONE
|
|
|
|
binding.editPasswordConfirm.visibility = View.GONE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
binding.buttonCreate.setOnClickListener {
|
|
|
|
val identityName = binding.editName.text.toString()
|
|
|
|
val password = binding.editPassword.text.toString().toByteArray()
|
|
|
|
if (password.isEmpty()) {
|
|
|
|
createIdentity(identityName, null)
|
|
|
|
} else {
|
|
|
|
val passwordConfirm = binding.editPasswordConfirm.text.toString().toByteArray()
|
|
|
|
if (password.contentEquals(passwordConfirm)) {
|
|
|
|
createIdentity(identityName, password)
|
|
|
|
} else {
|
|
|
|
Toast.makeText(activity, R.string.password_mismatch, Toast.LENGTH_SHORT).show()
|
|
|
|
}
|
|
|
|
passwordConfirm.fill(0)
|
|
|
|
password.fill(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun createIdentity(identityName: String, password: ByteArray?) {
|
|
|
|
val databaseFolder = Constants.getDatabaseFolder(requireContext())
|
|
|
|
if (createNewIdentity(
|
|
|
|
databaseFolder,
|
|
|
|
identityName,
|
|
|
|
password
|
|
|
|
)) {
|
|
|
|
val intent = Intent(activity, MainActivity::class.java)
|
|
|
|
intent.putExtra("identityName", identityName)
|
|
|
|
startActivity(intent)
|
2021-05-27 20:15:03 +02:00
|
|
|
activity.finish()
|
2021-01-26 19:45:18 +01:00
|
|
|
} else {
|
|
|
|
Toast.makeText(activity, R.string.identity_create_failed, Toast.LENGTH_SHORT).show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|