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 }
@@ -470,10 +462,8 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
470462 override fun listenWithMetadata () {
471463 // [START listen_with_metadata]
472464 // Listen for metadata changes to the document.
473- val options = DocumentListenOptions ().includeMetadataChanges()
474-
475465 val docRef = db.collection(" cities" ).document(" SF" )
476- docRef.addSnapshotListener(options ) { snapshot, e ->
466+ docRef.addSnapshotListener(MetadataChanges . INCLUDE ) { snapshot, e ->
477467 // ...
478468 }
479469 // [END listen_with_metadata]
@@ -572,7 +562,7 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
572562 }
573563 }
574564
575- if (! snapshots!! .metadata.isFromCache) {
565+ if (! snapshots.metadata.isFromCache) {
576566 Log .d(TAG , " Got initial state." )
577567 }
578568 }
@@ -623,43 +613,43 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
623613 val cities = db.collection(" cities" )
624614
625615 val data1 = HashMap <String , Any >()
626- data1.put( " name" , " San Francisco" )
627- data1.put( " state" , " CA" )
628- data1.put( " country" , " USA" )
629- data1.put( " capital" , false )
630- 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
631621 cities.document(" SF" ).set(data1)
632622
633623 val data2 = HashMap <String , Any >()
634- data2.put( " name" , " Los Angeles" )
635- data2.put( " state" , " CA" )
636- data2.put( " country" , " USA" )
637- data2.put( " capital" , false )
638- 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
639629 cities.document(" LA" ).set(data2)
640630
641631 val data3 = HashMap <String , Any ?>()
642- data3.put( " name" , " Washington D.C." )
643- data3.put( " state" , null )
644- data3.put( " country" , " USA" )
645- data3.put( " capital" , true )
646- 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
647637 cities.document(" DC" ).set(data3)
648638
649639 val data4 = HashMap <String , Any ?>()
650- data4.put( " name" , " Tokyo" )
651- data4.put( " state" , null )
652- data4.put( " country" , " Japan" )
653- data4.put( " capital" , true )
654- data4.put( " population" , 9000000 )
640+ data4[ " name" ] = " Tokyo"
641+ data4[ " state" ] = null
642+ data4[ " country" ] = " Japan"
643+ data4[ " capital" ] = true
644+ data4[ " population" ] = 9000000
655645 cities.document(" TOK" ).set(data4)
656646
657647 val data5 = HashMap <String , Any ?>()
658- data5.put( " name" , " Beijing" )
659- data5.put( " state" , null )
660- data5.put( " country" , " China" )
661- data5.put( " capital" , true )
662- data5.put( " population" , 21500000 )
648+ data5[ " name" ] = " Beijing"
649+ data5[ " state" ] = null
650+ data5[ " country" ] = " China"
651+ data5[ " capital" ] = true
652+ data5[ " population" ] = 21500000
663653 cities.document(" BJ" ).set(data5)
664654 // [END example_data]
665655 }
@@ -906,7 +896,7 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
906896 Log .d(TAG , " New city:" + change.document.data)
907897 }
908898
909- val source = if (querySnapshot!! .metadata.isFromCache)
899+ val source = if (querySnapshot.metadata.isFromCache)
910900 " local cache"
911901 else
912902 " server"
@@ -926,6 +916,7 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
926916 var name: String? = null
927917 @ServerTimestamp
928918 var timestamp: Date ? = null
919+
929920 }
930921 // [END server_timestamp_annotation]
931922
@@ -935,7 +926,7 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
935926
936927 // Update the timestamp field with the value from the server
937928 val updates = HashMap <String , Any >()
938- updates.put( " timestamp" , FieldValue .serverTimestamp() )
929+ updates[ " timestamp" ] = FieldValue .serverTimestamp()
939930
940931 docRef.update(updates).addOnCompleteListener(object : OnCompleteListener <Void > {
941932 // [START_EXCLUDE]
@@ -952,7 +943,7 @@ class DocSnippets(val db: FirebaseFirestore) : DocSnippetsInterface {
952943
953944 // Remove the 'capital' field from the document
954945 val updates = HashMap <String , Any >()
955- updates.put( " capital" , FieldValue .delete() )
946+ updates[ " capital" ] = FieldValue .delete()
956947
957948 docRef.update(updates).addOnCompleteListener(object : OnCompleteListener <Void > {
958949 // [START_EXCLUDE]
0 commit comments