Skip to content

Commit

Permalink
belindas-closet-android_3_191-fix-products-for-same-category-disappea…
Browse files Browse the repository at this point in the history
…ring (#196)

* Fix bugs with archive function

* Update invalid id toast string
  • Loading branch information
heosman authored Nov 27, 2023
1 parent c15f99d commit d1f0193
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,15 @@ fun UpdateIndividualProductCard(product: Product, navController: NavController)
}
if (isArchive) {
ConfirmationArchiveDialogIndividual(onConfirm = {
val hidden = MainActivity.getPref().getStringSet("hidden", mutableSetOf(product.productType.name))
hidden?.add(product.productType.name)
val editor = MainActivity.getPref().edit()
editor.putStringSet("hidden", hidden)
editor.apply()
navController.navigate(Routes.ProductDetail.route)
coroutineScope.launch {
archive(product.id, navController, current)
val isArchiveSuccessful = archive(product.id, navController, current)
if (isArchiveSuccessful) {
val hidden = MainActivity.getPref().getStringSet("hidden", mutableSetOf(product.id))
hidden?.add(product.id)
val editor = MainActivity.getPref().edit()
editor.putStringSet("hidden", hidden)
editor.apply()
}
}
isArchive = false
}, onDismiss = {
Expand Down Expand Up @@ -477,7 +478,7 @@ suspend fun delete(productId: String, navController: NavController, current: Con
Toast.makeText(current, R.string.unauthorized_toast, Toast.LENGTH_SHORT).show()
false
} else if (productId.count() != 24) {
Toast.makeText(current, R.string.invalid_id, Toast.LENGTH_SHORT).show()
Toast.makeText(current, R.string.invalid_id_toast, Toast.LENGTH_SHORT).show()
false
} else if (deleteResponse != null) {
MainActivity.getPref().edit().putBoolean("isHidden", deleteResponse.isHidden).apply()
Expand All @@ -496,26 +497,35 @@ suspend fun delete(productId: String, navController: NavController, current: Con
}
}

suspend fun archive(productId: String, navController: NavController, current: Context) {
suspend fun archive(productId: String, navController: NavController, current: Context): Boolean {
return try {
val userRole = MainActivity.getPref().getString("userRole", Role.USER.name)?.let {
Role.valueOf(it) } ?: Role.USER
val archiveRequest = ArchiveRequest(
id = productId,
role = Role.ADMIN
)
val archiveResponse = ArchiveService.create().archive(archiveRequest)
if (productId.count() != 24) {
Toast.makeText(current, R.string.invalid_id, Toast.LENGTH_SHORT).show()
if (userRole != Role.ADMIN) {
Toast.makeText(current, R.string.unauthorized_toast, Toast.LENGTH_SHORT).show()
false
} else if (productId.count() != 24) {
Toast.makeText(current, R.string.invalid_id_toast, Toast.LENGTH_SHORT).show()
false
} else if (archiveResponse != null) {
MainActivity.getPref().edit().putBoolean("isSold", archiveResponse.isSold).apply()
navController.navigate(Routes.ProductDetail.route)
Toast.makeText(current, R.string.archive_successful_toast, Toast.LENGTH_SHORT).show()
true
} else {
Toast.makeText(current, R.string.archive_failed_toast, Toast.LENGTH_SHORT).show()
false
}
} catch (e: HttpException) {
e.printStackTrace()
println("Error: ${e.message}")
Toast.makeText(current, "Archive failed. Error: ${e.message}", Toast.LENGTH_SHORT).show()
false
}
}

2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<string name="delete_successful_toast">Deletion successful!</string>
<string name="archive_successful_toast">Archival successful!</string>
<string name="unauthorized_toast">Unauthorized!</string>
<string name="invalid_id">Invalid ID!</string>
<string name="invalid_id_toast">Invalid ID!</string>
<string name="delete_failed_toast">Deletion failed, please try again!</string>
<string name="archive_failed_toast">Archival failed, please try again!</string>

Expand Down

0 comments on commit d1f0193

Please sign in to comment.