Skip to content

Commit 8170d3d

Browse files
authored
Merge pull request #22 from nativeapptemplate/fix_null_safety_and_resource_leak
Fix null safety bugs and resource leak
2 parents 35a88f3 + a2340ac commit 8170d3d

3 files changed

Lines changed: 12 additions & 17 deletions

File tree

app/src/main/kotlin/com/nativeapptemplate/nativeapptemplatefree/ui/shop_detail/ShopDetailView.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ private fun TopAppBar(
361361
IconButton(
362362
onClick = {
363363
if (uiState.success) {
364-
onSettingsClick(uiState.shop.getData()?.id!!)
364+
uiState.shop.getData()?.id?.let { onSettingsClick(it) }
365365
}
366366
},
367367
) {

app/src/main/kotlin/com/nativeapptemplate/nativeapptemplatefree/ui/shop_settings/ShopSettingsView.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ private fun ShopSettingsContentView(
169169
dialogTitle = stringResource(R.string.are_you_sure),
170170
confirmButtonTitle = stringResource(R.string.title_reset_number_tags),
171171
onDismissRequest = { isShowingResetConfirmationDialog = false },
172-
onConfirmation = { viewModel.resetShop(uiState.shop.getData()?.id!!) },
172+
onConfirmation = { uiState.shop.getData()?.id?.let { viewModel.resetShop(it) } },
173173
icon = Icons.Outlined.AddAlert,
174174
)
175175
}
@@ -179,7 +179,7 @@ private fun ShopSettingsContentView(
179179
dialogTitle = stringResource(R.string.are_you_sure),
180180
confirmButtonTitle = stringResource(R.string.title_delete_shop),
181181
onDismissRequest = { isShowingDeleteConfirmationDialog = false },
182-
onConfirmation = { viewModel.deleteShop(uiState.shop.getData()?.id!!) },
182+
onConfirmation = { uiState.shop.getData()?.id?.let { viewModel.deleteShop(it) } },
183183
icon = Icons.Outlined.AddAlert,
184184
)
185185
}
@@ -230,7 +230,7 @@ private fun ShopSettingsContentView(
230230
)
231231
},
232232
modifier = Modifier
233-
.clickable { onShowBasicSettingsClick(uiState.shop.getData()?.id!!) },
233+
.clickable { uiState.shop.getData()?.id?.let { onShowBasicSettingsClick(it) } },
234234
)
235235

236236
HorizontalDivider()
@@ -265,7 +265,7 @@ private fun ShopSettingsContentView(
265265
)
266266
},
267267
modifier = Modifier
268-
.clickable { onShowItemTagListClick(uiState.shop.getData()?.id!!) },
268+
.clickable { uiState.shop.getData()?.id?.let { onShowItemTagListClick(it) } },
269269
)
270270

271271
HorizontalDivider()
@@ -300,7 +300,7 @@ private fun ShopSettingsContentView(
300300
)
301301
},
302302
modifier = Modifier
303-
.clickable { onShowNumberTagsWebpageListClick(uiState.shop.getData()?.id!!) },
303+
.clickable { uiState.shop.getData()?.id?.let { onShowNumberTagsWebpageListClick(it) } },
304304
)
305305

306306
HorizontalDivider()

app/src/main/kotlin/com/nativeapptemplate/nativeapptemplatefree/utils/Utility.kt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,10 @@ object Utility {
116116
// https://stackoverflow.com/a/75714502/1160200
117117
// https://qiita.com/irgaly/items/b942bd985a4647e372ea
118118
fun Context.shareImage(title: String, image: ImageBitmap, filename: String) {
119-
val file = try {
120-
val outputFile = File(cacheDir, "$filename.png")
121-
val outPutStream = FileOutputStream(outputFile)
122-
image.asAndroidBitmap().compress(Bitmap.CompressFormat.PNG, 100, outPutStream)
123-
outPutStream.flush()
124-
outPutStream.close()
125-
outputFile
126-
} catch (e: Throwable) {
127-
throw e
119+
val file = File(cacheDir, "$filename.png").also { outputFile ->
120+
FileOutputStream(outputFile).use { outputStream ->
121+
image.asAndroidBitmap().compress(Bitmap.CompressFormat.PNG, 100, outputStream)
122+
}
128123
}
129124

130125
val uri = file.toUriCompat(this)
@@ -145,8 +140,8 @@ object Utility {
145140
// https://stackoverflow.com/a/78039163/1160200
146141
fun Context.restartApp() {
147142
val packageManager = packageManager
148-
val intent = packageManager.getLaunchIntentForPackage(packageName)!!
149-
val componentName = intent.component!!
143+
val intent = packageManager.getLaunchIntentForPackage(packageName) ?: return
144+
val componentName = intent.component ?: return
150145
val restartIntent = Intent.makeRestartActivityTask(componentName)
151146
startActivity(restartIntent)
152147
Runtime.getRuntime().exit(0)

0 commit comments

Comments
 (0)