Skip to content

Commit 2bd81c2

Browse files
authored
Merge pull request #174 from firebase/ss-ktx
Convert Kotlin samples to KTX
2 parents 37bb175 + 1b71cd9 commit 2bd81c2

File tree

33 files changed

+261
-229
lines changed

33 files changed

+261
-229
lines changed

database/app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ android {
77

88
defaultConfig {
99
applicationId "com.google.firebase.referencecode.database"
10-
minSdkVersion 15
10+
minSdkVersion 16
1111
targetSdkVersion 29
1212
versionCode 1
1313
versionName "1.0"
@@ -29,7 +29,7 @@ android {
2929
dependencies {
3030
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
3131
implementation 'androidx.appcompat:appcompat:1.1.0'
32-
implementation "com.google.firebase:firebase-database:19.2.1"
32+
implementation "com.google.firebase:firebase-database-ktx:19.2.1"
3333
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61"
3434
}
3535

database/app/src/main/java/com/google/firebase/referencecode/database/kotlin/EmulatorSuite.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.google.firebase.referencecode.database.kotlin
22

33
import com.google.firebase.database.FirebaseDatabase
4+
import com.google.firebase.database.ktx.database
5+
import com.google.firebase.ktx.Firebase
46

57
class EmulatorSuite {
68

@@ -9,7 +11,7 @@ class EmulatorSuite {
911
// 10.0.2.2 is the special IP address to connect to the 'localhost' of
1012
// the host computer from an Android emulator.
1113
// In almost all cases the ns (namespace) is your project ID.
12-
val database = FirebaseDatabase.getInstance("http://10.0.2.2:9000?ns=YOUR_DATABASE_NAMESPACE")
14+
val database = Firebase.database("http://10.0.2.2:9000?ns=YOUR_DATABASE_NAMESPACE")
1315
// [END rtdb_emulator_connect]
1416
}
1517

@@ -19,4 +21,4 @@ class EmulatorSuite {
1921
database.reference.setValue(null)
2022
// [END rtdb_emulator_flush]
2123
}
22-
}
24+
}

database/app/src/main/java/com/google/firebase/referencecode/database/kotlin/MainActivity.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.google.firebase.referencecode.database.kotlin
22

33
import android.os.Bundle
4-
import androidx.appcompat.app.AppCompatActivity
54
import android.util.Log
5+
import androidx.appcompat.app.AppCompatActivity
66
import com.google.firebase.database.DataSnapshot
77
import com.google.firebase.database.DatabaseError
8-
import com.google.firebase.database.FirebaseDatabase
98
import com.google.firebase.database.ValueEventListener
9+
import com.google.firebase.database.ktx.database
10+
import com.google.firebase.database.ktx.getValue
11+
import com.google.firebase.ktx.Firebase
1012
import com.google.firebase.referencecode.database.R
1113

1214
abstract class MainActivity : AppCompatActivity() {
@@ -24,7 +26,7 @@ abstract class MainActivity : AppCompatActivity() {
2426
fun basicReadWrite() {
2527
// [START write_message]
2628
// Write a message to the database
27-
val database = FirebaseDatabase.getInstance()
29+
val database = Firebase.database
2830
val myRef = database.getReference("message")
2931

3032
myRef.setValue("Hello, World!")
@@ -36,7 +38,7 @@ abstract class MainActivity : AppCompatActivity() {
3638
override fun onDataChange(dataSnapshot: DataSnapshot) {
3739
// This method is called once with the initial value and again
3840
// whenever data at this location is updated.
39-
val value = dataSnapshot.getValue(String::class.java)
41+
val value = dataSnapshot.getValue<String>()
4042
Log.d(TAG, "Value is: $value")
4143
}
4244

database/app/src/main/java/com/google/firebase/referencecode/database/kotlin/OfflineActivity.kt

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
package com.google.firebase.referencecode.database.kotlin
22

3-
import androidx.appcompat.app.AppCompatActivity
43
import android.util.Log
4+
import androidx.appcompat.app.AppCompatActivity
55
import com.google.firebase.database.ChildEventListener
66
import com.google.firebase.database.DataSnapshot
77
import com.google.firebase.database.DatabaseError
8-
import com.google.firebase.database.FirebaseDatabase
98
import com.google.firebase.database.ServerValue
109
import com.google.firebase.database.ValueEventListener
10+
import com.google.firebase.database.ktx.database
11+
import com.google.firebase.database.ktx.getValue
12+
import com.google.firebase.ktx.Firebase
1113

1214
class OfflineActivity : AppCompatActivity() {
1315

1416
private fun enablePersistence() {
1517
// [START rtdb_enable_persistence]
16-
FirebaseDatabase.getInstance().setPersistenceEnabled(true)
18+
Firebase.database.setPersistenceEnabled(true)
1719
// [END rtdb_enable_persistence]
1820
}
1921

2022
private fun keepSynced() {
2123
// [START rtdb_keep_synced]
22-
val scoresRef = FirebaseDatabase.getInstance().getReference("scores")
24+
val scoresRef = Firebase.database.getReference("scores")
2325
scoresRef.keepSynced(true)
2426
// [END rtdb_keep_synced]
2527

@@ -30,7 +32,7 @@ class OfflineActivity : AppCompatActivity() {
3032

3133
private fun queryRecentScores() {
3234
// [START rtdb_query_recent_scores]
33-
val scoresRef = FirebaseDatabase.getInstance().getReference("scores")
35+
val scoresRef = Firebase.database.getReference("scores")
3436
scoresRef.orderByValue().limitToLast(4).addChildEventListener(object : ChildEventListener {
3537
override fun onChildAdded(snapshot: DataSnapshot, previousChild: String?) {
3638
Log.d(TAG, "The ${snapshot.key} dinosaur's score is ${snapshot.value}")
@@ -69,7 +71,7 @@ class OfflineActivity : AppCompatActivity() {
6971

7072
private fun onDisconnect() {
7173
// [START rtdb_on_disconnect_set]
72-
val presenceRef = FirebaseDatabase.getInstance().getReference("disconnectmessage")
74+
val presenceRef = Firebase.database.getReference("disconnectmessage")
7375
// Write a string when this client loses connection
7476
presenceRef.onDisconnect().setValue("I disconnected!")
7577
// [END rtdb_on_disconnect_set]
@@ -94,7 +96,7 @@ class OfflineActivity : AppCompatActivity() {
9496

9597
private fun getConnectionState() {
9698
// [START rtdb_listen_connected]
97-
val connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected")
99+
val connectedRef = Firebase.database.getReference(".info/connected")
98100
connectedRef.addValueEventListener(object : ValueEventListener {
99101
override fun onDataChange(snapshot: DataSnapshot) {
100102
val connected = snapshot.getValue(Boolean::class.java) ?: false
@@ -114,14 +116,14 @@ class OfflineActivity : AppCompatActivity() {
114116

115117
private fun disconnectionTimestamp() {
116118
// [START rtdb_on_disconnect_timestamp]
117-
val userLastOnlineRef = FirebaseDatabase.getInstance().getReference("users/joe/lastOnline")
119+
val userLastOnlineRef = Firebase.database.getReference("users/joe/lastOnline")
118120
userLastOnlineRef.onDisconnect().setValue(ServerValue.TIMESTAMP)
119121
// [END rtdb_on_disconnect_timestamp]
120122
}
121123

122124
private fun getServerTimeOffset() {
123125
// [START rtdb_server_time_offset]
124-
val offsetRef = FirebaseDatabase.getInstance().getReference(".info/serverTimeOffset")
126+
val offsetRef = Firebase.database.getReference(".info/serverTimeOffset")
125127
offsetRef.addValueEventListener(object : ValueEventListener {
126128
override fun onDataChange(snapshot: DataSnapshot) {
127129
val offset = snapshot.getValue(Double::class.java) ?: 0.0
@@ -139,7 +141,7 @@ class OfflineActivity : AppCompatActivity() {
139141
// [START rtdb_full_connection_example]
140142
// Since I can connect from multiple devices, we store each connection instance separately
141143
// any time that connectionsRef's value is null (i.e. has no children) I am offline
142-
val database = FirebaseDatabase.getInstance()
144+
val database = Firebase.database
143145
val myConnectionsRef = database.getReference("users/joe/connections")
144146

145147
// Stores the timestamp of my last disconnect (the last time I was seen online)
@@ -148,7 +150,7 @@ class OfflineActivity : AppCompatActivity() {
148150
val connectedRef = database.getReference(".info/connected")
149151
connectedRef.addValueEventListener(object : ValueEventListener {
150152
override fun onDataChange(snapshot: DataSnapshot) {
151-
val connected = snapshot.getValue(Boolean::class.java) ?: false
153+
val connected = snapshot.getValue<Boolean>() ?: false
152154
if (connected) {
153155
val con = myConnectionsRef.push()
154156

database/app/src/main/java/com/google/firebase/referencecode/database/kotlin/QueryActivity.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package com.google.firebase.referencecode.database.kotlin
22

33
import android.os.Bundle
4-
import androidx.appcompat.app.AppCompatActivity
54
import android.util.Log
5+
import androidx.appcompat.app.AppCompatActivity
66
import com.google.firebase.database.ChildEventListener
77
import com.google.firebase.database.DataSnapshot
88
import com.google.firebase.database.DatabaseError
99
import com.google.firebase.database.DatabaseReference
10-
import com.google.firebase.database.FirebaseDatabase
1110
import com.google.firebase.database.Query
1211
import com.google.firebase.database.ValueEventListener
12+
import com.google.firebase.database.ktx.database
13+
import com.google.firebase.database.ktx.getValue
14+
import com.google.firebase.ktx.Firebase
1315
import com.google.firebase.referencecode.database.R
1416
import com.google.firebase.referencecode.database.models.Message
1517

@@ -35,7 +37,7 @@ abstract class QueryActivity : AppCompatActivity() {
3537
super.onCreate(savedInstanceState)
3638
setContentView(R.layout.activity_query)
3739

38-
databaseReference = FirebaseDatabase.getInstance().reference
40+
databaseReference = Firebase.database.reference
3941
}
4042

4143
private fun basicListen() {
@@ -51,7 +53,7 @@ abstract class QueryActivity : AppCompatActivity() {
5153
Log.d(TAG, "Number of messages: ${dataSnapshot.childrenCount}")
5254
dataSnapshot.children.forEach { child ->
5355
// Extract Message object from the DataSnapshot
54-
val message: Message? = child.getValue(Message::class.java)
56+
val message: Message? = child.getValue<Message>()
5557

5658
// Use the message
5759
// [START_EXCLUDE]
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package com.google.firebase.referencecode.database.kotlin
22

33
import androidx.appcompat.app.AppCompatActivity
4-
5-
import com.google.firebase.database.FirebaseDatabase
4+
import com.google.firebase.database.ktx.database
5+
import com.google.firebase.ktx.Firebase
66

77
class ShardingActivity : AppCompatActivity() {
88

99
private fun getMultipleInstances() {
1010
// [START rtdb_multi_instance]
1111
// Get the default database instance for an app
12-
val primary = FirebaseDatabase.getInstance().reference
12+
val primary = Firebase.database.reference
1313

1414
// Get a secondary database instance by URL
15-
val secondary = FirebaseDatabase
16-
.getInstance("https://testapp-1234.firebaseio.com").reference
15+
val secondary = Firebase.database("https://testapp-1234.firebaseio.com").reference
1716
// [END rtdb_multi_instance]
1817
}
1918
}

dynamic-links/app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ dependencies {
2626
implementation 'androidx.appcompat:appcompat:1.1.0'
2727
implementation "com.google.firebase:firebase-auth:19.2.0"
2828
implementation "com.google.firebase:firebase-invites:17.0.0"
29-
implementation "com.google.firebase:firebase-dynamic-links:19.1.0"
29+
implementation "com.google.firebase:firebase-dynamic-links-ktx:19.1.0"
3030

3131
// For an optimal experience using Dynamic Links, add the Firebase SDK
3232
// for Google Analytics. This is recommended, but not required.
3333
implementation 'com.google.firebase:firebase-analytics:17.2.2'
3434

35-
implementation "com.google.firebase:firebase-database:19.2.1"
35+
implementation "com.google.firebase:firebase-database-ktx:19.2.1"
3636
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61"
3737
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
3838
}

0 commit comments

Comments
 (0)