Skip to content

Commit 47945e9

Browse files
authored
resources: smoother pdf reader lifecycle scoping (fixes #8974) (#8918)
1 parent 76f71f1 commit 47945e9

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ android {
99
applicationId "org.ole.planet.myplanet"
1010
minSdk = 26
1111
targetSdk = 36
12-
versionCode = 3696
13-
versionName = "0.36.96"
12+
versionCode = 3697
13+
versionName = "0.36.97"
1414
ndkVersion = '26.3.11579264'
1515
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1616
vectorDrawables.useSupportLibrary = true

app/src/main/java/org/ole/planet/myplanet/ui/resources/AddResourceFragment.kt

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import androidx.activity.result.contract.ActivityResultContracts
2525
import androidx.appcompat.app.AlertDialog
2626
import androidx.core.content.ContextCompat
2727
import androidx.core.graphics.drawable.toDrawable
28+
import androidx.lifecycle.lifecycleScope
2829
import com.google.android.material.bottomsheet.BottomSheetBehavior
2930
import com.google.android.material.bottomsheet.BottomSheetDialog
3031
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
@@ -262,7 +263,7 @@ class AddResourceFragment : BottomSheetDialogFragment() {
262263
startActivity(Intent(activity, AddResourceActivity::class.java).putExtra("resource_local_url", path))
263264
} else {
264265
val userModel = userProfileDbHandler.userModel ?: return
265-
showAlert(requireContext(), path, myPersonalRepository, userModel.id, userModel.name) {
266+
showAlert(requireContext(), path, myPersonalRepository, userModel.id, userModel.name, viewLifecycleOwner.lifecycleScope) {
266267
dismiss()
267268
}
268269
}
@@ -278,29 +279,41 @@ class AddResourceFragment : BottomSheetDialogFragment() {
278279
repository: MyPersonalRepository,
279280
userId: String?,
280281
userName: String?,
281-
onDismiss: (() -> Unit)? = null
282+
scope: CoroutineScope,
283+
onDismiss: () -> Unit
282284
) {
283285
val v = LayoutInflater.from(context).inflate(R.layout.alert_my_personal, null)
284286
val etTitle = v.findViewById<EditText>(R.id.et_title)
285287
val etDesc = v.findViewById<EditText>(R.id.et_description)
286-
AlertDialog.Builder(context, R.style.AlertDialogTheme)
288+
val dialog = AlertDialog.Builder(context, R.style.AlertDialogTheme)
287289
.setTitle(R.string.enter_resource_detail)
288290
.setView(v)
289-
.setPositiveButton("Save") { _: DialogInterface?, _: Int ->
291+
.setPositiveButton(R.string.save, null)
292+
.setNegativeButton(R.string.dismiss, null)
293+
.create()
294+
295+
dialog.setOnShowListener {
296+
val positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
297+
positiveButton.setOnClickListener {
290298
val title = etTitle.text.toString().trim { it <= ' ' }
291299
if (title.isEmpty()) {
292300
Utilities.toast(context, context.getString(R.string.title_is_required))
293-
return@setPositiveButton
301+
return@setOnClickListener
294302
}
295303
val desc = etDesc.text.toString().trim { it <= ' ' }
296-
CoroutineScope(Dispatchers.IO).launch {
304+
positiveButton.isEnabled = false
305+
scope.launch(Dispatchers.IO) {
297306
repository.savePersonalResource(title, userId, userName, path, desc)
298307
withContext(Dispatchers.Main) {
299308
Utilities.toast(context, context.getString(R.string.resource_saved_to_my_personal))
300-
onDismiss?.invoke()
309+
positiveButton.isEnabled = true
310+
dialog.dismiss()
311+
onDismiss.invoke()
301312
}
302313
}
303-
}.setNegativeButton(R.string.dismiss, null).show()
314+
}
315+
}
316+
dialog.show()
304317
}
305318
}
306319
}

app/src/main/java/org/ole/planet/myplanet/ui/viewer/PDFReaderActivity.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,9 @@ class PDFReaderActivity : AppCompatActivity(), AudioRecordListener {
125125
outputFile,
126126
myPersonalRepository,
127127
userModel.id,
128-
userModel.name
129-
)
128+
userModel.name,
129+
lifecycleScope
130+
) {}
130131
}
131132
binding.fabRecord.setImageResource(R.drawable.ic_mic)
132133
}

0 commit comments

Comments
 (0)