Skip to content

Commit

Permalink
[feat/backstack_after_register]: 구체적인 주소를 웹뷰에 띄워주는 프레그먼트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
soopeach committed Sep 23, 2023
1 parent ad81982 commit b7c2655
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.gdsc.presentation.view.webview

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebViewClient
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.navArgs
import dagger.hilt.android.AndroidEntryPoint
import org.gdsc.domain.Empty
import org.gdsc.presentation.databinding.FragmentSpecificWebViewBinding
import org.gdsc.presentation.utils.repeatWhenUiStarted
import org.gdsc.presentation.view.MainActivity
import org.gdsc.presentation.view.WebAppInterface

@AndroidEntryPoint
class SpecificWebViewFragment : Fragment() {

private var _binding: FragmentSpecificWebViewBinding? = null
private val binding get() = _binding!!

private val viewModel: SpecificWebViewViewModel by viewModels()

private val navArgs by navArgs<SpecificWebViewFragmentArgs>()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentSpecificWebViewBinding.inflate(inflater, container, false)
return binding.root
}

@SuppressLint("SetJavaScriptEnabled")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

val parentActivity = requireActivity() as MainActivity
parentActivity.changeToolbarTitle(String.Empty)
parentActivity.changeToolbarVisible(false)

binding.webView.apply {
loadUrl(navArgs.url)

(requireActivity() as MainActivity).let {
it.detailLink?.let { link ->
binding.webView.loadUrl(link)
}
}

settings.javaScriptEnabled = true
settings.domStorageEnabled = true
webViewClient = WebViewClient()

addJavascriptInterface(WebAppInterface(
requireContext(),
{
parentActivity.slideUpBottomNavigationView()
},
{
parentActivity.slideDownBottomNavigationView()
},
{
parentActivity.navigateToEditRestaurantInfo(it)
},
{
repeatWhenUiStarted {
binding.webView.loadUrl(
"javascript:setAccessToken(\"${viewModel.getAccessToken()}\")"
)
}
}
), "webviewBridge")
}
}

override fun onDestroyView() {
_binding = null
super.onDestroyView()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.gdsc.presentation.view.webview

import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import org.gdsc.domain.usecase.token.GetAccessTokenUseCase
import javax.inject.Inject

@HiltViewModel
class SpecificWebViewViewModel @Inject constructor(
private val getAccessTokenUseCase: GetAccessTokenUseCase,
) : ViewModel() {

suspend fun getAccessToken() = getAccessTokenUseCase.invoke()
}
15 changes: 15 additions & 0 deletions presentation/src/main/res/layout/fragment_specific_web_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView
android:id="@+id/web_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit b7c2655

Please sign in to comment.