1+ @file:Suppress(" UNUSED_VARIABLE" , " UNUSED_ANONYMOUS_PARAMETER" )
2+
13package com.google.example.firestore.kotlin
24
35import android.support.annotation.WorkerThread
@@ -80,16 +82,6 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
8082
8183 }
8284
83-
84- internal fun deleteAll () {
85- deleteCollection(" cities" )
86- deleteCollection(" users" )
87- }
88-
89- private fun deleteCollection (path : String ) {
90- deleteCollection(db.collection(path), 50 , EXECUTOR )
91- }
92-
9385 override fun setup () {
9486 // [START get_firestore_instance]
9587 val db = FirebaseFirestore .getInstance()
@@ -107,9 +99,9 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
10799 // [START add_ada_lovelace]
108100 // Create a new user with a first and last name
109101 val user = HashMap <String , Any >()
110- user.put( " first" , " Ada" )
111- user.put( " last" , " Lovelace" )
112- user.put( " born" , 1815 )
102+ user[ " first" ] = " Ada"
103+ user[ " last" ] = " Lovelace"
104+ user[ " born" ] = 1815
113105
114106 // Add a new document with a generated ID
115107 db.collection(" users" )
@@ -124,10 +116,10 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
124116 // [START add_alan_turing]
125117 // Create a new user with a first, middle, and last name
126118 val user = HashMap <String , Any >()
127- user.put( " first" , " Alan" )
128- user.put( " middle" , " Mathison" )
129- user.put( " last" , " Turring" )
130- user.put( " born" , 1912 )
119+ user[ " first" ] = " Alan"
120+ user[ " middle" ] = " Mathison"
121+ user[ " last" ] = " Turring"
122+ user[ " born" ] = 1912
131123
132124 // Add a new document with a generated ID
133125 db.collection(" users" )
@@ -214,9 +206,9 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
214206 override fun setDocument () {
215207 // [START set_document]
216208 val city = HashMap <String , Any >()
217- city.put( " name" , " Los Angeles" )
218- city.put( " state" , " CA" )
219- city.put( " country" , " USA" )
209+ city[ " name" ] = " Los Angeles"
210+ city[ " state" ] = " CA"
211+ city[ " country" ] = " USA"
220212
221213 db.collection(" cities" ).document(" LA" )
222214 .set(city)
@@ -234,18 +226,18 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
234226 override fun dataTypes () {
235227 // [START data_types]
236228 val docData = HashMap <String , Any ?>()
237- docData.put( " stringExample" , " Hello world!" )
238- docData.put( " booleanExample" , true )
239- docData.put( " numberExample" , 3.14159265 )
240- docData.put( " dateExample" , Date () )
241- docData.put( " listExample" , Arrays .asList(1 , 2 , 3 ) )
242- docData.put( " nullExample" , null )
229+ docData[ " stringExample" ] = " Hello world!"
230+ docData[ " booleanExample" ] = true
231+ docData[ " numberExample" ] = 3.14159265
232+ docData[ " dateExample" ] = Date ()
233+ docData[ " listExample" ] = Arrays .asList(1 , 2 , 3 )
234+ docData[ " nullExample" ] = null
243235
244236 val nestedData = HashMap <String , Any >()
245- nestedData.put( " a" , 5 )
246- nestedData.put( " b" , true )
237+ nestedData[ " a" ] = 5
238+ nestedData[ " b" ] = true
247239
248- docData.put( " objectExample" , nestedData)
240+ docData[ " objectExample" ] = nestedData
249241
250242 db.collection(" data" ).document(" one" )
251243 .set(docData)
@@ -265,8 +257,8 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
265257 // [START add_document]
266258 // Add a new document with a generated id.
267259 val data = HashMap <String , Any >()
268- data.put( " name" , " Tokyo" )
269- data.put( " country" , " Japan" )
260+ data[ " name" ] = " Tokyo"
261+ data[ " country" ] = " Japan"
270262
271263 db.collection(" cities" )
272264 .add(data)
@@ -320,7 +312,7 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
320312 // [START set_field_with_merge]
321313 // Update one field, creating the document if it does not already exist.
322314 val data = HashMap <String , Any >()
323- data.put( " capital" , true )
315+ data[ " capital" ] = true
324316
325317 db.collection(" cities" ).document(" BJ" )
326318 .set(data, SetOptions .merge())
@@ -433,8 +425,8 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
433425 return @EventListener
434426 }
435427
436- if (snapshot != null && snapshot!! .exists()) {
437- Log .d(TAG , " Current data: " + snapshot!! .data)
428+ if (snapshot != null && snapshot.exists()) {
429+ Log .d(TAG , " Current data: " + snapshot.data)
438430 } else {
439431 Log .d(TAG , " Current data: null" )
440432 }
@@ -452,13 +444,13 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
452444 return @EventListener
453445 }
454446
455- val source = if (snapshot != null && snapshot!! .metadata.hasPendingWrites())
447+ val source = if (snapshot != null && snapshot.metadata.hasPendingWrites())
456448 " Local"
457449 else
458450 " Server"
459451
460- if (snapshot != null && snapshot!! .exists()) {
461- Log .d(TAG , source + " data: " + snapshot!! .data)
452+ if (snapshot != null && snapshot.exists()) {
453+ Log .d(TAG , source + " data: " + snapshot.data)
462454 } else {
463455 Log .d(TAG , source + " data: null" )
464456 }
@@ -570,7 +562,7 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
570562 }
571563 }
572564
573- if (! snapshots!! .metadata.isFromCache) {
565+ if (! snapshots.metadata.isFromCache) {
574566 Log .d(TAG , " Got initial state." )
575567 }
576568 }
@@ -621,43 +613,43 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
621613 val cities = db.collection(" cities" )
622614
623615 val data1 = HashMap <String , Any >()
624- data1.put( " name" , " San Francisco" )
625- data1.put( " state" , " CA" )
626- data1.put( " country" , " USA" )
627- data1.put( " capital" , false )
628- data1.put( " population" , 860000 )
616+ data1[ " name" ] = " San Francisco"
617+ data1[ " state" ] = " CA"
618+ data1[ " country" ] = " USA"
619+ data1[ " capital" ] = false
620+ data1[ " population" ] = 860000
629621 cities.document(" SF" ).set(data1)
630622
631623 val data2 = HashMap <String , Any >()
632- data2.put( " name" , " Los Angeles" )
633- data2.put( " state" , " CA" )
634- data2.put( " country" , " USA" )
635- data2.put( " capital" , false )
636- data2.put( " population" , 3900000 )
624+ data2[ " name" ] = " Los Angeles"
625+ data2[ " state" ] = " CA"
626+ data2[ " country" ] = " USA"
627+ data2[ " capital" ] = false
628+ data2[ " population" ] = 3900000
637629 cities.document(" LA" ).set(data2)
638630
639631 val data3 = HashMap <String , Any ?>()
640- data3.put( " name" , " Washington D.C." )
641- data3.put( " state" , null )
642- data3.put( " country" , " USA" )
643- data3.put( " capital" , true )
644- data3.put( " population" , 680000 )
632+ data3[ " name" ] = " Washington D.C."
633+ data3[ " state" ] = null
634+ data3[ " country" ] = " USA"
635+ data3[ " capital" ] = true
636+ data3[ " population" ] = 680000
645637 cities.document(" DC" ).set(data3)
646638
647639 val data4 = HashMap <String , Any ?>()
648- data4.put( " name" , " Tokyo" )
649- data4.put( " state" , null )
650- data4.put( " country" , " Japan" )
651- data4.put( " capital" , true )
652- data4.put( " population" , 9000000 )
640+ data4[ " name" ] = " Tokyo"
641+ data4[ " state" ] = null
642+ data4[ " country" ] = " Japan"
643+ data4[ " capital" ] = true
644+ data4[ " population" ] = 9000000
653645 cities.document(" TOK" ).set(data4)
654646
655647 val data5 = HashMap <String , Any ?>()
656- data5.put( " name" , " Beijing" )
657- data5.put( " state" , null )
658- data5.put( " country" , " China" )
659- data5.put( " capital" , true )
660- data5.put( " population" , 21500000 )
648+ data5[ " name" ] = " Beijing"
649+ data5[ " state" ] = null
650+ data5[ " country" ] = " China"
651+ data5[ " capital" ] = true
652+ data5[ " population" ] = 21500000
661653 cities.document(" BJ" ).set(data5)
662654 // [END example_data]
663655 }
@@ -904,7 +896,7 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
904896 Log .d(TAG , " New city:" + change.document.data)
905897 }
906898
907- val source = if (querySnapshot!! .metadata.isFromCache)
899+ val source = if (querySnapshot.metadata.isFromCache)
908900 " local cache"
909901 else
910902 " server"
@@ -924,6 +916,7 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
924916 var name: String? = null
925917 @ServerTimestamp
926918 var timestamp: Date ? = null
919+
927920 }
928921 // [END server_timestamp_annotation]
929922
@@ -933,7 +926,7 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
933926
934927 // Update the timestamp field with the value from the server
935928 val updates = HashMap <String , Any >()
936- updates.put( " timestamp" , FieldValue .serverTimestamp() )
929+ updates[ " timestamp" ] = FieldValue .serverTimestamp()
937930
938931 docRef.update(updates).addOnCompleteListener(object : OnCompleteListener <Void > {
939932 // [START_EXCLUDE]
@@ -950,7 +943,7 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
950943
951944 // Remove the 'capital' field from the document
952945 val updates = HashMap <String , Any >()
953- updates.put( " capital" , FieldValue .delete() )
946+ updates[ " capital" ] = FieldValue .delete()
954947
955948 docRef.update(updates).addOnCompleteListener(object : OnCompleteListener <Void > {
956949 // [START_EXCLUDE]
0 commit comments