-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Converted DialogUtil to Kotlin #3621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
1f415e8
Converted DialogUtil to Kotlin
madhurgupta10 bab74e8
Kotlin syntax and standard changes
madhurgupta10 f54224e
Removed ;
madhurgupta10 84bf5e2
Added missing null
madhurgupta10 706f983
Added missing null
madhurgupta10 90e40d8
Removed unnecessary code
madhurgupta10 887cca5
Reduced functions
madhurgupta10 cb89725
added let to customView
madhurgupta10 f5ddf76
reverted "let" changes
madhurgupta10 97f6f8a
reverted "let" changes
madhurgupta10 f5dbafb
removed if-statements
madhurgupta10 759dd60
replaced with "it"
madhurgupta10 0aa5d2e
fixed overflow error
madhurgupta10 2f66abb
Function rename
madhurgupta10 ac6b771
Used named arguments
madhurgupta10 c6d27ad
Fix Typo
madhurgupta10 593e99e
single lined
madhurgupta10 eee902e
Update DialogUtil.kt
madhurgupta10 ad6f5eb
Merge remote-tracking branch 'upstream/master' into kotlin-conv
madhurgupta10 0a785ab
Merge branch 'kotlin-conv' of https://github.com/madhurgupta10/apps-a…
madhurgupta10 c5483e8
changed default value
madhurgupta10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
177 changes: 0 additions & 177 deletions
177
app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java
This file was deleted.
Oops, something went wrong.
161 changes: 161 additions & 0 deletions
161
app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| package fr.free.nrw.commons.utils | ||
|
|
||
| import android.app.Activity | ||
| import android.app.AlertDialog | ||
| import android.app.Dialog | ||
| import android.view.View | ||
| import fr.free.nrw.commons.R | ||
| import timber.log.Timber | ||
|
|
||
| object DialogUtil { | ||
| /** | ||
| * Shows a dialog safely. | ||
| * @param activity the activity | ||
| * @param dialog the dialog to be shown | ||
| */ | ||
| private fun showSafely(activity: Activity?, dialog: Dialog?) { | ||
|
|
||
| if (activity == null || dialog == null) { | ||
| Timber.d("Show called with null activity / dialog. Ignoring.") | ||
| return | ||
| } | ||
|
|
||
| if (activity.isFinishing || activity.isDestroyed) { | ||
| Timber.e("Activity is not running. Could not show dialog. ") | ||
| return | ||
| } | ||
| try { | ||
| dialog.show() | ||
| } catch (e: IllegalStateException) { | ||
| Timber.e(e, "Could not show dialog.") | ||
| } | ||
| } | ||
|
|
||
| @JvmStatic | ||
| fun showAlertDialog( | ||
| activity: Activity, | ||
| title: String, | ||
| message: String, | ||
| onPositiveBtnClick: Runnable?, | ||
| onNegativeBtnClick: Runnable? | ||
| ) { | ||
| createAndShowDialogSafely( | ||
| activity = activity, | ||
| title = title, | ||
| message = message, | ||
| positiveButtonText = activity.getString(R.string.yes), | ||
| negativeButtonText = activity.getString(R.string.no), | ||
| onPositiveBtnClick = onPositiveBtnClick, | ||
| onNegativeBtnClick = onNegativeBtnClick | ||
| ) | ||
| } | ||
|
|
||
| @JvmStatic | ||
| fun showAlertDialog( | ||
| activity: Activity, | ||
| title: String, | ||
| message: String, | ||
| positiveButtonText: String?, | ||
| negativeButtonText: String?, | ||
| onPositiveBtnClick: Runnable?, | ||
| onNegativeBtnClick: Runnable? | ||
| ) { | ||
| createAndShowDialogSafely( | ||
| activity = activity, | ||
| title = title, | ||
| message = message, | ||
| positiveButtonText = positiveButtonText, | ||
| negativeButtonText = negativeButtonText, | ||
| onPositiveBtnClick = onPositiveBtnClick, | ||
| onNegativeBtnClick = onNegativeBtnClick | ||
| ) | ||
| } | ||
|
|
||
| @JvmStatic | ||
| fun showAlertDialog( | ||
| activity: Activity, | ||
| title: String, | ||
| message: String, | ||
| onPositiveBtnClick: Runnable?, | ||
| onNegativeBtnClick: Runnable?, | ||
| customView: View?, | ||
| cancelable: Boolean | ||
| ) { | ||
| createAndShowDialogSafely( | ||
| activity = activity, | ||
| title = title, | ||
| message = message, | ||
| positiveButtonText = activity.getString(R.string.yes), | ||
| negativeButtonText = activity.getString(R.string.no), | ||
| onPositiveBtnClick = onPositiveBtnClick, | ||
| onNegativeBtnClick = onNegativeBtnClick, | ||
| customView = customView, | ||
| cancelable = cancelable | ||
| ) | ||
| } | ||
|
|
||
| @JvmStatic | ||
| fun showAlertDialog( | ||
| activity: Activity, | ||
| title: String, | ||
| message: String, | ||
| positiveButtonText: String?, | ||
| onPositiveBtnClick: Runnable?, | ||
| cancelable: Boolean | ||
| ) { | ||
| createAndShowDialogSafely( | ||
| activity = activity, | ||
| title = title, | ||
| message = message, | ||
| positiveButtonText = positiveButtonText, | ||
| onPositiveBtnClick = onPositiveBtnClick, | ||
| cancelable = cancelable | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * show a dialog | ||
| * @param activity | ||
| * @param title | ||
| * @param message | ||
| * @param positiveButtonText | ||
| * @param negativeButtonText | ||
| * @param onPositiveBtnClick | ||
| * @param onNegativeBtnClick | ||
| * @param customView | ||
| * @param cancelable | ||
| */ | ||
| private fun createAndShowDialogSafely( | ||
| activity: Activity, | ||
| title: String, | ||
| message: String, | ||
| positiveButtonText: String? = null, | ||
| negativeButtonText: String? = null, | ||
| onPositiveBtnClick: Runnable? = null, | ||
| onNegativeBtnClick: Runnable? = null, | ||
| customView: View? = null, | ||
| cancelable: Boolean = true | ||
| ) { | ||
|
|
||
| /* If the custom view already has a parent, there is already a dialog showing with the view | ||
| * This happens for on resume - return to avoid creating a second dialog - the first one | ||
| * will still show | ||
| */ | ||
| if (customView?.parent != null) { | ||
| return | ||
| } | ||
|
|
||
| showSafely(activity, AlertDialog.Builder(activity).apply { | ||
| setTitle(title) | ||
| setMessage(message) | ||
| setView(customView) | ||
| setCancelable(cancelable) | ||
| positiveButtonText?.let { | ||
| setPositiveButton(it) { _, _ -> onPositiveBtnClick?.run() } | ||
| } | ||
| negativeButtonText?.let { | ||
| setNegativeButton(it) { _, _ -> onNegativeBtnClick?.run() } | ||
| } | ||
| }.create()) | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.