Skip to content

Commit

Permalink
Use new way of doing view bindings (see https://developer.android.com…
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdapioneer committed Apr 3, 2021
1 parent 55b801a commit db25176
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
5 changes: 4 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion project.ext.compileSdkVersion
Expand All @@ -17,6 +16,10 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildFeatures {
viewBinding true
}

signingConfigs {
release {
// Intentionally storing plaintext passwords as this is just a demo app
Expand Down
48 changes: 28 additions & 20 deletions app/src/main/java/com/lambdapioneer/argon2kt/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,35 @@ import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.lambdapioneer.argon2kt.Argon2Kt
import com.lambdapioneer.argon2kt.Argon2KtUtils
import com.lambdapioneer.argon2kt.Argon2Mode
import com.lambdapioneer.argon2kt.Argon2Version
import com.lambdapioneer.argon2kt.Argon2KtUtils
import kotlinx.android.synthetic.main.activity_main.*
import com.lambdapioneer.argon2kt.app.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
spinner_argon_mode.adapter = ArgonModeAdapter(layoutInflater)
spinner_argon_version.adapter = ArgonVersionAdapter(layoutInflater)
spinner_argon_version.setSelection(1)

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.spinnerArgonMode.adapter = ArgonModeAdapter(layoutInflater)
binding.spinnerArgonVersion.adapter = ArgonVersionAdapter(layoutInflater)
binding.spinnerArgonVersion.setSelection(1)
}

fun onClickButtonRun(@Suppress("UNUSED_PARAMETER") view: View) {
try {
val params = Argon2AsyncTaskParams(
saltInHex = edit_salt.text.toString(),
passwordInUnicode = edit_password.text.toString(),
iterations = edit_iterations.text.toString().toInt(),
memory = edit_memory.text.toString().toInt(),
mode = spinner_argon_mode.selectedItem as Argon2Mode,
version = spinner_argon_version.selectedItem as Argon2Version
saltInHex = binding.editSalt.text.toString(),
passwordInUnicode = binding.editPassword.text.toString(),
iterations = binding.editIterations.text.toString().toInt(),
memory = binding.editMemory.text.toString().toInt(),
mode = binding.spinnerArgonMode.selectedItem as Argon2Mode,
version = binding.spinnerArgonVersion.selectedItem as Argon2Version
)
Argon2AsyncTask().execute(params)
} catch (e: Exception) {
Expand Down Expand Up @@ -83,27 +88,30 @@ class MainActivity : AppCompatActivity() {
override fun onPostExecute(result: Argon2AsyncTaskResult) {
setProgressIndicator(false)

result.hashInHex?.apply { text_output_hash.text = this }
result.encodedOutputAsString?.apply { text_output_encoded_string.text = this }
result.hashInHex?.apply { binding.textOutputHash.text = this }
result.encodedOutputAsString?.apply { binding.textOutputEncodedString.text = this }

fun showToast(message: String) = Toast.makeText(applicationContext, message, Toast.LENGTH_SHORT).show()
fun showToast(message: String) =
Toast.makeText(applicationContext, message, Toast.LENGTH_SHORT).show()
result.error?.apply { showToast(getString(R.string.toast_failure, this.message)) }
result.timeInMs?.apply { showToast(getString(R.string.toast_wall_time, this)) }
}
}

private fun setProgressIndicator(inProgress: Boolean) {
button_run.isEnabled = !inProgress
progress_bar.visibility = if (inProgress) View.VISIBLE else View.GONE
layout_output.visibility = if (inProgress) View.GONE else View.VISIBLE
binding.buttonRun.isEnabled = !inProgress
binding.progressBar.visibility = if (inProgress) View.VISIBLE else View.GONE
binding.layoutOutput.visibility = if (inProgress) View.GONE else View.VISIBLE
}

private fun copyStringToClipboard(string: String) {
val clipboard = ContextCompat.getSystemService(applicationContext, ClipboardManager::class.java)
val clipboard =
ContextCompat.getSystemService(applicationContext, ClipboardManager::class.java)
val clip = ClipData.newPlainText("argon2", string)
clipboard!!.setPrimaryClip(clip)

Toast.makeText(applicationContext, R.string.toast_copied_to_clipboard, Toast.LENGTH_SHORT).show()
Toast.makeText(applicationContext, R.string.toast_copied_to_clipboard, Toast.LENGTH_SHORT)
.show()
}
}

Expand Down

0 comments on commit db25176

Please sign in to comment.