-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTransactionInitDialogFragment.kt
More file actions
113 lines (95 loc) · 3.82 KB
/
TransactionInitDialogFragment.kt
File metadata and controls
113 lines (95 loc) · 3.82 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package eu.defdev.vulnabank.view.fragment
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import dagger.android.support.DaggerAppCompatDialogFragment
import eu.defdev.vulnabank.R
import eu.defdev.vulnabank.repository.transaction.TransactionRepository
import kotlinx.android.synthetic.main.fragment_transaction_init.accountNumber
import kotlinx.android.synthetic.main.fragment_transaction_init.amount
import kotlinx.android.synthetic.main.fragment_transaction_init.cancel
import kotlinx.android.synthetic.main.fragment_transaction_init.comment
import kotlinx.android.synthetic.main.fragment_transaction_init.name
import kotlinx.android.synthetic.main.fragment_transaction_init.ok
import javax.inject.Inject
class TransactionInitDialogFragment: DaggerAppCompatDialogFragment() {
@Inject
lateinit var transactionRepository: TransactionRepository
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_transaction_init, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
ok.setOnClickListener {
val dataIntent = Intent()
val msg = getStringContentOfTransaction()
clearTexts()
dataIntent.putExtra(INTENT_DATA, msg)
targetFragment?.onActivityResult(targetRequestCode, Activity.RESULT_OK, dataIntent)
dismiss()
}
cancel.setOnClickListener {
clearTexts()
targetFragment?.onActivityResult(targetRequestCode, Activity.RESULT_CANCELED, null)
dismiss()
}
amount.addTextChangedListener(textChangeListener {amount.error = null})
name.addTextChangedListener(textChangeListener {amount.error = null})
accountNumber.addTextChangedListener(textChangeListener {amount.error = null})
}
private fun clearTexts(){
amount.text.clear()
name.text.clear()
accountNumber.text.clear()
comment.text.clear()
}
private fun getStringContentOfTransaction(): String {
return "${name.text};" +
"${accountNumber.text};" +
"${amount.text};" +
"${comment.text}";
}
private fun textChangeListener(action: () -> Unit) = object: TextWatcher {
override fun afterTextChanged(p0: Editable?) {
manageButtonState()
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
action.invoke()
}
}
private fun manageButtonState(){
if(name.text.isEmpty()) {
ok.isEnabled = false
name.error = context?.getString(R.string.name_error)
return
}
if (accountNumber.text.length < 8) {
ok.isEnabled = false
accountNumber.error = context?.getString(R.string.account_number_error)
return
}
if (amount.text.isEmpty() || amount.text.toString().toInt() <= 0){
ok.isEnabled = false
amount.error = context?.getString(R.string.amount_error)
return
}
ok.isEnabled = true
}
companion object{
const val INTENT_DATA = "intentData"
private var INSTANCE: TransactionInitDialogFragment? = null
fun getInstance(): TransactionInitDialogFragment {
if (INSTANCE == null)
INSTANCE =
TransactionInitDialogFragment()
return INSTANCE!!
}
}
}