-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMainViewModel.kt
48 lines (41 loc) · 1.4 KB
/
MainViewModel.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.fernandocejas.sample
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.fernandocejas.rust.Cryptor
/**
* Used to communicate between screens.
*/
class MainViewModel : ViewModel() {
/**
* TODO: Just for Learning Purpose.
*
* This collaborator should be passed as
* a constructor argument of the
* [ViewModel].
*
* @link https://fernandocejas.com/blog/engineering/2019-05-08-architecting-android-reloaded/
*/
private val cryptor = Cryptor()
// Encryption
private val _encryptedStringResult = MutableLiveData<String>()
val encryptedStringResult: LiveData<String> = _encryptedStringResult
// Decryption
private val _decryptedStringResult = MutableLiveData<String>()
val decryptedStringResult: LiveData<String> = _decryptedStringResult
val encryptString: (String) -> Unit = {
_encryptedStringResult.value = cryptor.encrypt(it)
}
val decryptString: (String) -> Unit = {
/**
* TODO: Just for Learning Purpose.
*
* Proper handle exceptions and failure.
*/
val decryptionResult = cryptor.decrypt(it)
when {
decryptionResult.isNotBlank() -> _decryptedStringResult.value = decryptionResult
else -> _decryptedStringResult.value = "Invalid Base64 String!!!"
}
}
}