diff --git a/presentation/src/main/java/org/gdsc/presentation/view/WebAppInterface.kt b/presentation/src/main/java/org/gdsc/presentation/view/WebAppInterface.kt index 1225491e..1ad25d41 100644 --- a/presentation/src/main/java/org/gdsc/presentation/view/WebAppInterface.kt +++ b/presentation/src/main/java/org/gdsc/presentation/view/WebAppInterface.kt @@ -2,12 +2,42 @@ package org.gdsc.presentation.view import android.content.Context import android.webkit.JavascriptInterface -import android.widget.Toast -import org.gdsc.presentation.model.Route import org.json.JSONObject const val WEB_BASE_URL = "https://jmt-frontend-ad7b8.web.app/" +sealed class WebViewReceivedData { + data class Navigation( + val isVisible: Boolean + ): WebViewReceivedData() + + data class Back( + val enable: Boolean + ): WebViewReceivedData() +} +sealed class WebViewBrideObject { + data class Token(val data: Any? = null): WebViewBrideObject() + data class Other(val data: WebViewReceivedData): WebViewBrideObject() + +} + +fun JSONObject.toWebViewBrideObject(): WebViewBrideObject { + return when(this.get("name")) { + "token" -> { + WebViewBrideObject.Token() + } + "navigation" -> { + val isVisible = JSONObject(this.get("data").toString()).get("isVisible") as Boolean + WebViewBrideObject.Other(WebViewReceivedData.Navigation(isVisible)) + } + // back + else -> { + val enable = JSONObject(this.get("data").toString()).get("enable") as Boolean + WebViewBrideObject.Other(WebViewReceivedData.Back(enable)) + } + } +} + class WebAppInterface( private val mContext: Context, private val slideUpBottomNavigationView: () -> Unit = {}, @@ -28,8 +58,8 @@ class WebAppInterface( else slideDownBottomNavigationView() } - @JavascriptInterface - fun token() = setAccessToken() +// @JavascriptInterface +// fun token() = setAccessToken() // 딥링크 생성 필요 @JavascriptInterface @@ -37,20 +67,47 @@ class WebAppInterface( } // 데이터 구조는 다시 상의 후에 결정해서, 객체화 시키면 좋을 것으로 보임 +// @JavascriptInterface +// fun navigate(data: String) { +// val result = JSONObject(data) +// +// when(result.getString("route")) { +// Route.EDIT_RESTAURANT.route-> { +// val restaurantId = result.getString("restaurantId").toInt() +// navigateToRestaurantEdit(restaurantId) +// } +// } +// } + @JavascriptInterface - fun navigate(data: String) { - val result = JSONObject(data) + fun sendData(data: String) { + when (val result = JSONObject(data).toWebViewBrideObject()) { + is WebViewBrideObject.Token -> { + setAccessToken() + } + is WebViewBrideObject.Other -> { + when (val receivedData = result.data) { + is WebViewReceivedData.Navigation -> { + if (receivedData.isVisible) { + slideUpBottomNavigationView() + } else { + slideDownBottomNavigationView() + } + } + is WebViewReceivedData.Back -> { + if (receivedData.enable) { + + } else { - when(result.getString("route")) { - Route.EDIT_RESTAURANT.route-> { - val restaurantId = result.getString("restaurantId").toInt() - navigateToRestaurantEdit(restaurantId) + } + } + } } } } // webView.canGoBack으로 뒤로가기 처리 완료해서 비워뒀습니다. - @JavascriptInterface - fun back(isEnableBack: Boolean) { - } +// @JavascriptInterface +// fun back(isEnableBack: Boolean) { +// } } diff --git a/presentation/src/main/java/org/gdsc/presentation/view/group/MyGroupFragment.kt b/presentation/src/main/java/org/gdsc/presentation/view/group/MyGroupFragment.kt index c2602793..a488e81f 100644 --- a/presentation/src/main/java/org/gdsc/presentation/view/group/MyGroupFragment.kt +++ b/presentation/src/main/java/org/gdsc/presentation/view/group/MyGroupFragment.kt @@ -9,9 +9,12 @@ import android.webkit.WebViewClient import androidx.activity.addCallback import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels +import androidx.lifecycle.lifecycleScope import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.launch import org.gdsc.presentation.databinding.FragmentMyGroupBinding import org.gdsc.presentation.utils.repeatWhenUiStarted +import org.gdsc.presentation.view.MainActivity import org.gdsc.presentation.view.WEB_BASE_URL import org.gdsc.presentation.view.WebAppInterface import org.gdsc.presentation.view.webview.SpecificWebViewViewModel @@ -23,6 +26,8 @@ class MyGroupFragment: Fragment() { private val specificWebViewViewModel: SpecificWebViewViewModel by viewModels() + private val parentActivity by lazy { requireActivity() as MainActivity } + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? @@ -47,7 +52,20 @@ class MyGroupFragment: Fragment() { webViewClient = WebViewClient() addJavascriptInterface(WebAppInterface( - requireContext() + mContext = requireContext(), + slideUpBottomNavigationView = { + parentActivity.slideUpBottomNavigationView() + }, + slideDownBottomNavigationView ={ + parentActivity.slideDownBottomNavigationView() + }, + setAccessToken = { + viewLifecycleOwner.lifecycleScope.launch { + binding.webView.loadUrl( + "javascript:setAccessToken('${specificWebViewViewModel.getAccessToken()}')" + ) + } + } ), "webviewBridge") }