@@ -19,6 +19,7 @@ package com.esri.arcgismaps.kotlin.sampleviewer.model
19
19
import android.content.Context
20
20
import android.util.Log
21
21
import com.esri.arcgismaps.kotlin.sampleviewer.model.Sample.Companion.loadActivityPath
22
+ import com.esri.arcgismaps.kotlin.sampleviewer.model.Sample.Companion.loadCodeFiles
22
23
import com.esri.arcgismaps.kotlin.sampleviewer.model.Sample.Companion.loadReadMe
23
24
import com.esri.arcgismaps.kotlin.sampleviewer.model.Sample.Companion.loadScreenshot
24
25
import com.esri.arcgismaps.kotlin.sampleviewer.model.room.AppDatabase
@@ -33,7 +34,7 @@ import kotlinx.serialization.json.Json
33
34
import java.util.concurrent.atomic.AtomicBoolean
34
35
35
36
/* *
36
- * The single source of truth for app wide data. It reads the sample metadata to create as list of
37
+ * The single source of truth for app wide data. It reads the sample metadata to create a list of
37
38
* [Sample] objects and populates the database used for search.
38
39
* It also provides functions to get samples by category, name, or all samples.
39
40
*/
@@ -47,51 +48,52 @@ object DefaultSampleInfoRepository : SampleInfoRepository {
47
48
private val sampleData = _sampleData .asStateFlow()
48
49
49
50
/* *
50
- * Load the sample metadata from the metadata folder in the assets directory and updates sampleList
51
+ * Load the sample metadata from 'samples.json' in the assets directory and updates sampleList
51
52
* of [Sample] objects.
52
53
*/
53
54
suspend fun load (context : Context ) {
54
55
if (isInitialized.compareAndSet(false , true )) {
55
56
// List that will be populated with samples
56
57
val sampleList = mutableListOf<Sample >()
57
- // Iterate through the metadata folder for all metadata files
58
- context.assets.list(" samples" )?.forEach { samplePath ->
59
- // Get this metadata files as a string
60
- context.assets.open(" samples/$samplePath /README.metadata.json" ).use { inputStream ->
61
- val metadataJsonString = inputStream.bufferedReader().use { it.readText() }
62
- try {
63
- val metadata = json.decodeFromString<SampleMetadata >(metadataJsonString)
64
58
65
- // Create and add a new sample metadata data class object to the list
66
- val sample = Sample (
67
- name = metadata.title,
68
- codeFiles = Sample .loadCodeFiles(
69
- context = context,
70
- sampleName = metadata.title
71
- ),
72
- url = " https://developers.arcgis.com/kotlin/sample-code/" +
73
- metadata.title.replace(" " , " -" ).lowercase(),
74
- readMe = loadReadMe(
75
- context = context,
76
- sampleName = samplePath
77
- ),
78
- screenshotURL = loadScreenshot(
79
- sampleName = metadata.title,
80
- imageArray = metadata.imagePaths
81
- ),
82
- mainActivity = loadActivityPath(
83
- codePaths = metadata.codePaths
84
- ),
85
- metadata = metadata,
86
- )
87
- // Add the new sample to the list
88
- sampleList.add(sample)
89
- } catch (e: Exception ) {
90
- Log .e(
91
- DefaultSampleInfoRepository ::class .simpleName,
92
- " Exception at $samplePath : " + e.printStackTrace()
93
- )
94
- }
59
+ // Read the entire 'samples.json' from build/sampleAssets/samples/
60
+ val samplesJsonString = context.assets.open(" samples/samples.json" ).use { stream ->
61
+ stream.bufferedReader().use { it.readText() }
62
+ }
63
+
64
+ // Parse it into a map of: sampleFolderName -> mapOf( filename -> fileContent )
65
+ val allSamplesData = json.decodeFromString<Map <String , Map <String , String >>>(samplesJsonString)
66
+
67
+ // Build each Sample using the metadata from "README.metadata.json"
68
+ allSamplesData.forEach { (sampleFolderName, fileMap) ->
69
+ val metadataJsonString = fileMap[" README.metadata.json" ]
70
+ ? : throw Exception (" README.metadata.json not found in sample: $sampleFolderName " )
71
+ try {
72
+ val metadata = json.decodeFromString<SampleMetadata >(metadataJsonString)
73
+
74
+ // Create and add a new sample metadata data class object to the list
75
+ val sample = Sample (
76
+ name = metadata.title,
77
+ codeFiles = loadCodeFiles(fileMap),
78
+ url = " https://developers.arcgis.com/kotlin/sample-code/" +
79
+ metadata.title.replace(" " , " -" ).lowercase(),
80
+ readMe = loadReadMe(fileMap),
81
+ screenshotURL = loadScreenshot(
82
+ sampleName = metadata.title,
83
+ imageArray = metadata.imagePaths
84
+ ),
85
+ mainActivity = loadActivityPath(
86
+ codePaths = metadata.codePaths
87
+ ),
88
+ metadata = metadata
89
+ )
90
+ // Add the new sample to the list
91
+ sampleList.add(sample)
92
+ } catch (e: Exception ) {
93
+ Log .e(
94
+ DefaultSampleInfoRepository ::class .simpleName,
95
+ " Exception at $sampleFolderName : ${e.stackTraceToString()} "
96
+ )
95
97
}
96
98
}
97
99
_sampleData .value = sampleList
0 commit comments