diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 01cbb0edb..98db38a29 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -141,4 +141,7 @@ dependencies { // Pagination implementation("androidx.paging:paging-runtime-ktx:3.3.0") + + // WebView + implementation("androidx.webkit:webkit:1.9.0") } diff --git a/android/app/src/main/assets/html/address.html b/android/app/src/main/assets/html/address.html new file mode 100644 index 000000000..6b1ff5869 --- /dev/null +++ b/android/app/src/main/assets/html/address.html @@ -0,0 +1,60 @@ + + +
+ + + + + + + + + + + + diff --git a/android/app/src/main/java/com/zzang/chongdae/presentation/view/address/AddressFinderDialog.kt b/android/app/src/main/java/com/zzang/chongdae/presentation/view/address/AddressFinderDialog.kt new file mode 100644 index 000000000..a8250f0fa --- /dev/null +++ b/android/app/src/main/java/com/zzang/chongdae/presentation/view/address/AddressFinderDialog.kt @@ -0,0 +1,90 @@ +package com.zzang.chongdae.presentation.view.address + +import android.annotation.SuppressLint +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.core.os.bundleOf +import androidx.fragment.app.DialogFragment +import androidx.fragment.app.setFragmentResult +import androidx.webkit.WebViewAssetLoader +import com.zzang.chongdae.databinding.DialogAddressFinderBinding + +class AddressFinderDialog : DialogFragment(), OnAddressClickListener { + private var _binding: DialogAddressFinderBinding? = null + private val binding get() = _binding!! + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle?, + ): View { + _binding = DialogAddressFinderBinding.inflate(inflater, container, false) + + return binding.root + } + + override fun onViewCreated( + view: View, + savedInstanceState: Bundle?, + ) { + super.onViewCreated(view, savedInstanceState) + + initDialog() + initWebView() + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } + + private fun initDialog() { + dialog?.window?.apply { + setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) + } + } + + @SuppressLint("SetJavaScriptEnabled") + private fun initWebView() { + val assetLoader = webViewAssetLoader() + + binding.wvAddress.run { + with(settings) { + javaScriptEnabled = true + allowFileAccess = false + allowContentAccess = false + } + addJavascriptInterface( + JavascriptInterface(this@AddressFinderDialog), + JS_BRIDGE, + ) + webViewClient = AddressWebViewClient(assetLoader) + loadUrl("https://$DOMAIN/$PATH/html/address.html") + } + } + + private fun webViewAssetLoader() = + WebViewAssetLoader.Builder() + .addPathHandler( + "/$PATH/", + WebViewAssetLoader.AssetsPathHandler(requireContext()), + ) + .setDomain(DOMAIN) + .build() + + override fun onClickAddress(address: String) { + setFragmentResult(ADDRESS_KEY, bundleOf(BUNDLE_ADDRESS_KEY to address)) + dismiss() + } + + companion object { + private const val JS_BRIDGE = "address_finder" + private const val DOMAIN = "address.finder.net" + private const val PATH = "assets" + + const val ADDRESS_KEY = "address_key" + const val BUNDLE_ADDRESS_KEY = "address" + } +} diff --git a/android/app/src/main/java/com/zzang/chongdae/presentation/view/address/AddressWebViewClient.kt b/android/app/src/main/java/com/zzang/chongdae/presentation/view/address/AddressWebViewClient.kt new file mode 100644 index 000000000..edc95df88 --- /dev/null +++ b/android/app/src/main/java/com/zzang/chongdae/presentation/view/address/AddressWebViewClient.kt @@ -0,0 +1,17 @@ +package com.zzang.chongdae.presentation.view.address + +import android.webkit.WebResourceRequest +import android.webkit.WebResourceResponse +import android.webkit.WebView +import androidx.webkit.WebViewAssetLoader +import androidx.webkit.WebViewClientCompat + +class AddressWebViewClient(private val assetLoader: WebViewAssetLoader) : + WebViewClientCompat() { + override fun shouldInterceptRequest( + view: WebView?, + request: WebResourceRequest?, + ): WebResourceResponse? { + return assetLoader.shouldInterceptRequest(request!!.url) + } +} diff --git a/android/app/src/main/java/com/zzang/chongdae/presentation/view/address/JavascriptInterface.kt b/android/app/src/main/java/com/zzang/chongdae/presentation/view/address/JavascriptInterface.kt new file mode 100644 index 000000000..d760f7401 --- /dev/null +++ b/android/app/src/main/java/com/zzang/chongdae/presentation/view/address/JavascriptInterface.kt @@ -0,0 +1,12 @@ +package com.zzang.chongdae.presentation.view.address + +import android.os.Looper + +class JavascriptInterface(private val onAddressClickListener: OnAddressClickListener) { + @android.webkit.JavascriptInterface + fun result(address: String) { + android.os.Handler(Looper.getMainLooper()).post { + onAddressClickListener.onClickAddress(address) + } + } +} diff --git a/android/app/src/main/java/com/zzang/chongdae/presentation/view/address/OnAddressClickListener.kt b/android/app/src/main/java/com/zzang/chongdae/presentation/view/address/OnAddressClickListener.kt new file mode 100644 index 000000000..ecf863c85 --- /dev/null +++ b/android/app/src/main/java/com/zzang/chongdae/presentation/view/address/OnAddressClickListener.kt @@ -0,0 +1,5 @@ +package com.zzang.chongdae.presentation.view.address + +interface OnAddressClickListener { + fun onClickAddress(address: String) +} diff --git a/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/HomeFragment.kt b/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/HomeFragment.kt index f399a2052..b0cea117c 100644 --- a/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/HomeFragment.kt +++ b/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/HomeFragment.kt @@ -16,7 +16,7 @@ import com.zzang.chongdae.databinding.FragmentHomeBinding import com.zzang.chongdae.presentation.view.home.adapter.OfferingAdapter import com.zzang.chongdae.presentation.view.offeringdetail.OfferingDetailActivity -class HomeFragment : Fragment(), OnArticleClickListener { +class HomeFragment : Fragment(), OnOfferingClickListener { private var _binding: FragmentHomeBinding? = null private val binding get() = _binding!! private lateinit var offeringAdapter: OfferingAdapter diff --git a/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/OnArticleClickListener.kt b/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/OnOfferingClickListener.kt similarity index 70% rename from android/app/src/main/java/com/zzang/chongdae/presentation/view/home/OnArticleClickListener.kt rename to android/app/src/main/java/com/zzang/chongdae/presentation/view/home/OnOfferingClickListener.kt index 3166924fe..423003870 100644 --- a/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/OnArticleClickListener.kt +++ b/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/OnOfferingClickListener.kt @@ -1,5 +1,5 @@ package com.zzang.chongdae.presentation.view.home -interface OnArticleClickListener { +interface OnOfferingClickListener { fun onClick(offeringId: Long) } diff --git a/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/adapter/OfferingAdapter.kt b/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/adapter/OfferingAdapter.kt index d0e3709f8..28e06c08c 100644 --- a/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/adapter/OfferingAdapter.kt +++ b/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/adapter/OfferingAdapter.kt @@ -6,10 +6,10 @@ import androidx.paging.PagingDataAdapter import androidx.recyclerview.widget.DiffUtil import com.zzang.chongdae.databinding.ItemOfferingBinding import com.zzang.chongdae.domain.model.Offering -import com.zzang.chongdae.presentation.view.home.OnArticleClickListener +import com.zzang.chongdae.presentation.view.home.OnOfferingClickListener class OfferingAdapter( - private val onArticleClickListener: OnArticleClickListener, + private val onOfferingClickListener: OnOfferingClickListener, ) : PagingDataAdapter