Skip to content

Commit 869aec6

Browse files
authored
Simple fix to not re-running load (#248)
2 parents 44dbc80 + c7c85ea commit 869aec6

File tree

4 files changed

+67
-59
lines changed

4 files changed

+67
-59
lines changed

app/src/main/AndroidManifest.xml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<uses-permission android:name="android.permission.INTERNET" />
66

77
<application
8+
android:name=".SampleViewerApplication"
89
android:allowBackup="true"
910
android:dataExtractionRules="@xml/data_extraction_rules"
1011
android:fullBackupContent="@xml/backup_rules"

app/src/main/java/com/esri/arcgismaps/kotlin/sampleviewer/MainActivity.kt

-11
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,15 @@ import androidx.activity.compose.setContent
2222
import androidx.activity.enableEdgeToEdge
2323
import androidx.compose.material3.MaterialTheme
2424
import androidx.compose.material3.Surface
25-
import androidx.compose.runtime.LaunchedEffect
26-
import androidx.compose.ui.platform.LocalContext
27-
import androidx.lifecycle.lifecycleScope
28-
import com.esri.arcgismaps.kotlin.sampleviewer.model.DefaultSampleInfoRepository
2925
import com.esri.arcgismaps.kotlin.sampleviewer.navigation.NavGraph
3026
import com.esri.arcgismaps.kotlin.sampleviewer.ui.theme.SampleAppTheme
31-
import kotlinx.coroutines.launch
3227

3328
class MainActivity : ComponentActivity() {
3429
override fun onCreate(savedInstanceState: Bundle?) {
3530
super.onCreate(savedInstanceState)
3631
enableEdgeToEdge()
3732
setContent {
3833
setContent {
39-
val context = LocalContext.current
40-
LaunchedEffect(Unit) {
41-
lifecycleScope.launch {
42-
DefaultSampleInfoRepository.load(context)
43-
}
44-
}
4534

4635
SampleAppTheme {
4736
Surface(color = MaterialTheme.colorScheme.background) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.esri.arcgismaps.kotlin.sampleviewer
2+
3+
import android.app.Application
4+
import com.esri.arcgismaps.kotlin.sampleviewer.model.DefaultSampleInfoRepository
5+
import kotlinx.coroutines.CoroutineScope
6+
import kotlinx.coroutines.Dispatchers
7+
import kotlinx.coroutines.launch
8+
9+
class SampleViewerApplication : Application() {
10+
11+
override fun onCreate() {
12+
super.onCreate()
13+
CoroutineScope(Dispatchers.IO).launch {
14+
// Load the repository once at app launch
15+
DefaultSampleInfoRepository.load(applicationContext)
16+
}
17+
}
18+
}

app/src/main/java/com/esri/arcgismaps/kotlin/sampleviewer/model/DefaultSampleInfoRepository.kt

+48-48
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ import com.esri.arcgismaps.kotlin.sampleviewer.model.Sample.Companion.loadScreen
2424
import com.esri.arcgismaps.kotlin.sampleviewer.model.room.AppDatabase
2525
import com.esri.arcgismaps.kotlin.sampleviewer.model.room.Converters
2626
import kotlinx.coroutines.Dispatchers
27-
import kotlinx.coroutines.sync.Mutex
28-
import kotlinx.coroutines.sync.withLock
2927
import kotlinx.coroutines.withContext
3028
import kotlinx.serialization.json.Json
29+
import java.util.concurrent.atomic.AtomicBoolean
3130

3231
/**
3332
* 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
3635
*/
3736
object DefaultSampleInfoRepository : SampleInfoRepository {
3837

39-
private val sampleList = mutableListOf<Sample>()
40-
private val sampleListMutex = Mutex()
38+
private val isInitialized = AtomicBoolean(false)
4139

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>()
4543

4644
/**
4745
* Load the sample metadata from the metadata folder in the assets directory and updates sampleList
4846
* of [Sample] objects.
4947
*/
5048
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
5951

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+
}
8989
}
9090
}
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+
}
9595
}
9696
}
9797

0 commit comments

Comments
 (0)