This repository has been archived by the owner on Dec 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ProfileFragment.kt
67 lines (53 loc) · 2.02 KB
/
ProfileFragment.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package net.aquadc.advancedkotlinpatterns.feature.bind
import android.app.Fragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.EditText
import android.widget.FrameLayout
import org.jetbrains.anko.*
import java.util.*
class ProfileFragment : Fragment() {
private lateinit var user: User
private lateinit var emailInput: EditText
private lateinit var nameInput: EditText
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle?) = UI {
user = savedInstanceState?.getParcelable("user") ?: User(UUID.randomUUID(), "", "")
verticalLayout {
layoutParams = FrameLayout.LayoutParams(matchParent, matchParent)
padding = dip(16)
textView {
textSize = 20f
padding = dip(8)
bind(ACTION_USER_UPDATED) {
it.text = when {
user.name.isNotBlank() && user.email.isNotBlank() -> "${user.name} (${user.email})"
user.name.isNotBlank() -> user.name
user.email.isNotBlank() -> user.email
else -> user.id.toString()
}
}
}.lparams(matchParent, wrapContent)
emailInput = editText {
id = 1
hint = "E-mail"
}.lparams(matchParent, wrapContent)
nameInput = editText {
id = 2
hint = "Name"
}.lparams(matchParent, wrapContent)
button("Save") {
setOnClickListener { saveButtonClicked() }
}
}
}.view
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putParcelable("user", user)
}
private fun saveButtonClicked() {
user.email = emailInput.text.toString()
user.name = nameInput.text.toString()
updated(ACTION_USER_UPDATED)
}
}