/
NewPostActivity.kt
129 lines (108 loc) · 4.5 KB
/
NewPostActivity.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.google.firebase.quickstart.database.kotlin
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.database.ValueEventListener
import com.google.firebase.quickstart.database.R
import com.google.firebase.quickstart.database.kotlin.models.Post
import com.google.firebase.quickstart.database.kotlin.models.User
import kotlinx.android.synthetic.main.activity_new_post.fabSubmitPost
import kotlinx.android.synthetic.main.activity_new_post.fieldBody
import kotlinx.android.synthetic.main.activity_new_post.fieldTitle
import java.util.HashMap
class NewPostActivity : BaseActivity() {
// [START declare_database_ref]
private lateinit var database: DatabaseReference
// [END declare_database_ref]
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_new_post)
// [START initialize_database_ref]
database = FirebaseDatabase.getInstance().reference
// [END initialize_database_ref]
fabSubmitPost.setOnClickListener { submitPost() }
}
private fun submitPost() {
val title = fieldTitle.text.toString()
val body = fieldBody.text.toString()
// Title is required
if (TextUtils.isEmpty(title)) {
fieldTitle.error = REQUIRED
return
}
// Body is required
if (TextUtils.isEmpty(body)) {
fieldBody.error = REQUIRED
return
}
// Disable button so there are no multi-posts
setEditingEnabled(false)
Toast.makeText(this, "Posting...", Toast.LENGTH_SHORT).show()
// [START single_value_read]
val userId = uid
database.child("users").child(userId).addListenerForSingleValueEvent(
object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
// Get user value
val user = dataSnapshot.getValue(User::class.java)
// [START_EXCLUDE]
if (user == null) {
// User is null, error out
Log.e(TAG, "User $userId is unexpectedly null")
Toast.makeText(baseContext,
"Error: could not fetch user.",
Toast.LENGTH_SHORT).show()
} else {
// Write new post
writeNewPost(userId, user.username.toString(), title, body)
}
// Finish this Activity, back to the stream
setEditingEnabled(true)
finish()
// [END_EXCLUDE]
}
override fun onCancelled(databaseError: DatabaseError) {
Log.w(TAG, "getUser:onCancelled", databaseError.toException())
// [START_EXCLUDE]
setEditingEnabled(true)
// [END_EXCLUDE]
}
})
// [END single_value_read]
}
private fun setEditingEnabled(enabled: Boolean) {
fieldTitle.isEnabled = enabled
fieldBody.isEnabled = enabled
if (enabled) {
fabSubmitPost.show()
} else {
fabSubmitPost.hide()
}
}
// [START write_fan_out]
private fun writeNewPost(userId: String, username: String, title: String, body: String) {
// Create new post at /user-posts/$userid/$postid and at
// /posts/$postid simultaneously
val key = database.child("posts").push().key
if (key == null) {
Log.w(TAG, "Couldn't get push key for posts")
return
}
val post = Post(userId, username, title, body)
val postValues = post.toMap()
val childUpdates = HashMap<String, Any>()
childUpdates["/posts/$key"] = postValues
childUpdates["/user-posts/$userId/$key"] = postValues
database.updateChildren(childUpdates)
}
// [END write_fan_out]
companion object {
private const val TAG = "NewPostActivity"
private const val REQUIRED = "Required"
}
}