Skip to content

Commit

Permalink
Merge pull request #119 from Trendyol/InfoDialogWebViewDownloadListener
Browse files Browse the repository at this point in the history
Set WebView download listener separately
  • Loading branch information
bilgehankalkan authored Sep 27, 2023
2 parents 2301301 + 5352ea6 commit 33603f3
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
3 changes: 2 additions & 1 deletion libraries/dialogs/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<img src="https://raw.githubusercontent.com/Trendyol/android-ui-components/master/images/dialogs-1.png" width="280"/> <img src="https://raw.githubusercontent.com/Trendyol/android-ui-components/master/images/dialogs-2.png" width="280"/> <img src="https://raw.githubusercontent.com/Trendyol/android-ui-components/master/images/dialogs-3.png" width="280"/> <img src="https://raw.githubusercontent.com/Trendyol/android-ui-components/master/images/dialogs-4.png" width="280"/> <img src="https://raw.githubusercontent.com/Trendyol/android-ui-components/master/images/dialogs-5.png" width="280"/>

$dialogsVersion = dialogs-1.4.1 [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
$dialogsVersion = dialogs-1.4.2 [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

## Dialogs
Dialogs is a bunch of BottomSheetDialogs to use in app to show user an information, agreement or list.
Expand Down Expand Up @@ -44,6 +44,7 @@ Simple dialog to show information, error or text.
| `contentImage` | Int | Drawable resource id of an visual, will be shown on top of `content` | 0 |
| `webViewContent` | WebViewContent | If you provide a webview content such as Html data content or url , that will be shown in the webview. | null |
| `webViewBuilder` | WebViewBuilder | If you provide a webview content such as Html data content or url , necessary settings should we given via webViewBuilder | null |
| `webViewDownloadListener` | DownloadListener | If you need to handle download action in webview, you need to provide webViewDownloadListener to handle events | null |

Sample usage:
```kotlin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.trendyol.uicomponents.dialogs

import android.text.SpannableString
import android.webkit.DownloadListener
import android.webkit.WebView
import androidx.annotation.DrawableRes

Expand Down Expand Up @@ -29,6 +30,7 @@ open class InfoDialogBuilder internal constructor() : Builder() {
var webViewBuilder: (WebView.() -> Unit)? = null
var horizontalPadding: Float? = null
var verticalPadding: Float? = null
var webViewDownloadListener: DownloadListener? = null

internal fun buildInfoDialog(block: InfoDialogBuilder.() -> Unit): DialogFragment {
return InfoDialogBuilder().apply(block).let {
Expand All @@ -52,6 +54,7 @@ open class InfoDialogBuilder internal constructor() : Builder() {
).toBundle()
this.closeButtonListener = it.closeButtonListener ?: { }
this.onDismissListener = it.onDialogDismissListener ?: {}
this.webViewDownloadListener = it.webViewDownloadListener
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewOutlineProvider
import android.webkit.DownloadListener
import android.webkit.WebChromeClient
import android.webkit.WebViewClient
import androidx.core.widget.doAfterTextChanged
Expand All @@ -31,6 +32,7 @@ class DialogFragment internal constructor() : BaseBottomSheetDialog() {
var onItemSelectedListener: ((DialogFragment, Int) -> Unit)? = null
var onItemReselectedListener: ((DialogFragment, Int) -> Unit)? = null
var onDismissListener: ((DialogFragment) -> Unit)? = null
var webViewDownloadListener: DownloadListener? = null

internal lateinit var binding: FragmentDialogBinding
private val dialogArguments by lazy(LazyThreadSafetyMode.NONE) {
Expand Down Expand Up @@ -239,6 +241,16 @@ class DialogFragment internal constructor() : BaseBottomSheetDialog() {
super.onDismiss(dialog)
}

override fun onResume() {
super.onResume()
binding.webViewContent.setDownloadListener(webViewDownloadListener)
}

override fun onPause() {
super.onPause()
binding.webViewContent.setDownloadListener(null)
}

companion object {

const val TAG: String = "TRENDYOL_BOTTOM_SHEET_DIALOG"
Expand Down
36 changes: 36 additions & 0 deletions sample/src/main/java/com/trendyol/uicomponents/DialogsActivity.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.trendyol.uicomponents

import android.app.DownloadManager
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.webkit.DownloadListener
import android.webkit.URLUtil
import android.webkit.WebViewClient
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
Expand All @@ -24,6 +29,10 @@ class DialogsActivity : AppCompatActivity() {

private lateinit var binding: ActivityDialogsBinding

private val downloadManager: DownloadManager? by lazy(LazyThreadSafetyMode.NONE) {
getSystemService(DOWNLOAD_SERVICE) as? DownloadManager
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityDialogsBinding.inflate(layoutInflater)
Expand All @@ -36,6 +45,9 @@ class DialogsActivity : AppCompatActivity() {
buttonSelectionWithSearchDialog.setOnClickListener { showSelectionWithSearchDialog() }
buttonInfoDialogWebview.setOnClickListener { showInfoDialogWithWebView() }
buttonInfoListDialog.setOnClickListener { showInfoListDialog() }
buttonInfoDialogWithWebviewDownloadListener.setOnClickListener {
showInfoDialogWithWebViewDownloadListener()
}
}
}

Expand Down Expand Up @@ -163,6 +175,30 @@ class DialogsActivity : AppCompatActivity() {
}.showDialog(supportFragmentManager)
}

private fun showInfoDialogWithWebViewDownloadListener() {
infoDialog {
title = "Info Dialog with WebView Download Listener"
webViewContent = WebViewContent.UrlContent("https://github.com/Trendyol")
showContentAsHtml = true
titleTextColor = R.color.civ_error_stroke
showCloseButton = true
webViewBuilder = {
settings.javaScriptEnabled = true
}
webViewDownloadListener = DownloadListener { url, _, contentDisposition, mimetype, _ ->
val downloadRequest = DownloadManager.Request(Uri.parse(url))
.setTitle(title)
.setMimeType(mimetype)
.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,
URLUtil.guessFileName(url, contentDisposition, mimetype)
)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
downloadManager?.enqueue(downloadRequest)
}
}.showDialog(supportFragmentManager)
}

override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
setDialogFragmentListenersIfNecessary()
Expand Down
12 changes: 12 additions & 0 deletions sample/src/main/res/layout/activity_dialogs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,16 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/button_info_dialog_webview" />

<com.google.android.material.button.MaterialButton
android:id="@+id/button_info_dialog_with_webview_download_listener"
style="@style/Sample.ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Show Info Dialog with WebView Download Listener"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/button_info_list_dialog" />

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 33603f3

Please sign in to comment.