Skip to content

Commit

Permalink
[feat/upgrade_web_bridge]: 변경된 브릿지 스팩에 따른 모델 생성 및 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
soopeach committed Mar 15, 2024
1 parent af6f8cc commit 86ce5b6
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {},
Expand All @@ -28,29 +58,56 @@ class WebAppInterface(
else slideDownBottomNavigationView()
}

@JavascriptInterface
fun token() = setAccessToken()
// @JavascriptInterface
// fun token() = setAccessToken()

// 딥링크 생성 필요
@JavascriptInterface
fun share() {
}

// 데이터 구조는 다시 상의 후에 결정해서, 객체화 시키면 좋을 것으로 보임
// @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) {
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Expand All @@ -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")

}
Expand Down

0 comments on commit 86ce5b6

Please sign in to comment.