@@ -24,10 +24,9 @@ import com.esri.arcgismaps.kotlin.sampleviewer.model.Sample.Companion.loadScreen
24
24
import com.esri.arcgismaps.kotlin.sampleviewer.model.room.AppDatabase
25
25
import com.esri.arcgismaps.kotlin.sampleviewer.model.room.Converters
26
26
import kotlinx.coroutines.Dispatchers
27
- import kotlinx.coroutines.sync.Mutex
28
- import kotlinx.coroutines.sync.withLock
29
27
import kotlinx.coroutines.withContext
30
28
import kotlinx.serialization.json.Json
29
+ import java.util.concurrent.atomic.AtomicBoolean
31
30
32
31
/* *
33
32
* The single source of truth for app wide data. It reads the sample metadata to create as list of
@@ -36,62 +35,63 @@ import kotlinx.serialization.json.Json
36
35
*/
37
36
object DefaultSampleInfoRepository : SampleInfoRepository {
38
37
39
- private val sampleList = mutableListOf<Sample >()
40
- private val sampleListMutex = Mutex ()
38
+ private val isInitialized = AtomicBoolean (false )
41
39
42
- private suspend fun updateSampleList ( block : MutableList < Sample >.() -> Unit ) {
43
- sampleListMutex.withLock { sampleList.block() }
44
- }
40
+ private val json = Json { ignoreUnknownKeys = true }
41
+
42
+ private val sampleList = mutableListOf< Sample >()
45
43
46
44
/* *
47
45
* Load the sample metadata from the metadata folder in the assets directory and updates sampleList
48
46
* of [Sample] objects.
49
47
*/
50
48
suspend fun load (context : Context ) {
51
- // Iterate through the metadata folder for all metadata files
52
- val json = Json { ignoreUnknownKeys = true }
53
- context.assets.list(" samples" )?.forEach { samplePath ->
54
- // Get this metadata files as a string
55
- context.assets.open(" samples/$samplePath /README.metadata.json" ).use { inputStream ->
56
- val metadataJsonString = inputStream.bufferedReader().use { it.readText() }
57
- try {
58
- val metadata = json.decodeFromString<SampleMetadata >(metadataJsonString)
49
+ if (isInitialized.compareAndSet(false , true )) {
50
+ // Iterate through the metadata folder for all metadata files
59
51
60
- // Create and add a new sample metadata data class object to the list
61
- val sample = Sample (
62
- name = metadata.title,
63
- codeFiles = Sample .loadCodeFiles(
64
- context = context,
65
- sampleName = metadata.title
66
- ),
67
- url = " https://developers.arcgis.com/kotlin/sample-code/" +
68
- metadata.title.replace(" " , " -" ).lowercase(),
69
- readMe = loadReadMe(
70
- context = context,
71
- sampleName = samplePath
72
- ),
73
- screenshotURL = loadScreenshot(
74
- sampleName = metadata.title,
75
- imageArray = metadata.imagePaths
76
- ),
77
- mainActivity = loadActivityPath(
78
- codePaths = metadata.codePaths
79
- ),
80
- metadata = metadata,
81
- )
82
- // Add the new sample to the list
83
- updateSampleList { add(sample) }
84
- } catch (e: Exception ) {
85
- Log .e(
86
- DefaultSampleInfoRepository ::class .simpleName,
87
- " Exception at $samplePath : " + e.printStackTrace()
88
- )
52
+ context.assets.list(" samples" )?.forEach { samplePath ->
53
+ // Get this metadata files as a string
54
+ context.assets.open(" samples/$samplePath /README.metadata.json" ).use { inputStream ->
55
+ val metadataJsonString = inputStream.bufferedReader().use { it.readText() }
56
+ try {
57
+ val metadata = json.decodeFromString<SampleMetadata >(metadataJsonString)
58
+
59
+ // Create and add a new sample metadata data class object to the list
60
+ val sample = Sample (
61
+ name = metadata.title,
62
+ codeFiles = Sample .loadCodeFiles(
63
+ context = context,
64
+ sampleName = metadata.title
65
+ ),
66
+ url = " https://developers.arcgis.com/kotlin/sample-code/" +
67
+ metadata.title.replace(" " , " -" ).lowercase(),
68
+ readMe = loadReadMe(
69
+ context = context,
70
+ sampleName = samplePath
71
+ ),
72
+ screenshotURL = loadScreenshot(
73
+ sampleName = metadata.title,
74
+ imageArray = metadata.imagePaths
75
+ ),
76
+ mainActivity = loadActivityPath(
77
+ codePaths = metadata.codePaths
78
+ ),
79
+ metadata = metadata,
80
+ )
81
+ // Add the new sample to the list
82
+ sampleList.add(sample)
83
+ } catch (e: Exception ) {
84
+ Log .e(
85
+ DefaultSampleInfoRepository ::class .simpleName,
86
+ " Exception at $samplePath : " + e.printStackTrace()
87
+ )
88
+ }
89
89
}
90
90
}
91
- }
92
- withContext( Dispatchers . IO ) {
93
- // Populates the Room SQL database
94
- populateDatabase(context)
91
+ withContext( Dispatchers . IO ) {
92
+ // Populates the Room SQL database
93
+ populateDatabase(context)
94
+ }
95
95
}
96
96
}
97
97
0 commit comments