Skip to content

Commit

Permalink
Merge pull request #97 from team-JMT/feat/upgrade_web_bridge
Browse files Browse the repository at this point in the history
변경된 브릿지 스팩에 따른 모델 생성 및 적용
  • Loading branch information
dogdduddy authored Mar 16, 2024
2 parents af6f8cc + 68179ea commit 0d352da
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 84 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 @@ -20,36 +20,23 @@ import org.gdsc.presentation.utils.repeatWhenUiStarted

class WebViewActivity : AppCompatActivity() {
private lateinit var binding: ActivityWebViewBinding
private lateinit var webView:WebView
private lateinit var webView: WebView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityWebViewBinding.inflate(layoutInflater)
setContentView(binding.root)

// webViewInit()

webView = binding.webView

setWebViewBackPress()
intent.extras?.let {
binding.webView.apply {

repeatWhenUiStarted {
loadUrl(it.getString("url") ?: WEB_BASE_URL)
}
webViewInit()

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

addJavascriptInterface(WebAppInterface(context), "webviewBridge")
}

}
setWebViewBackPress()

// val actionBar: ActionBar? = supportActionBar
// actionBar!!.hide()
}

private fun setWebViewBackPress() {
this.onBackPressedDispatcher.addCallback(this) {
if (binding.webView.canGoBack()) {
Expand All @@ -60,14 +47,20 @@ class WebViewActivity : AppCompatActivity() {
}
}

fun webViewInit() {
val webSettings: WebSettings = webView.getSettings()
webSettings.javaScriptEnabled = true // allow the js
private fun webViewInit() {

webView.apply {
loadUrl(WEB_BASE_URL + "group-create/name/")

settings.javaScriptEnabled = true // allow the js
settings.domStorageEnabled = true
webViewClient = WebViewClient()
addJavascriptInterface(WebAppInterface(this@WebViewActivity), "webviewBridge")
}


window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) //화면이 계속 켜짐
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_FULL_USER
webView.webViewClient = WebViewClient()

webView.addJavascriptInterface(WebAppInterface(this), "webviewBridge")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,26 @@ class BottomSheetDialog {
behavior.maxHeight = getBottomSheetDialogDefaultHeight()
}

fun <T: ViewBinding> bindBuilder(binding: T, callback: T.(BottomSheetDialog) -> Unit) {
val isShowing: Boolean
get() {
return this.btmDlg.isShowing
}

fun show() {
this.btmDlg.show()
}


fun <T : ViewBinding> bindBuilder(
binding: T,
callback: T.(BottomSheetDialog) -> Unit
): org.gdsc.presentation.view.custom.BottomSheetDialog {
this.customView = binding.root
btmDlg.setContentView(this.customView!!)
binding.callback(btmDlg)
return this
}

private fun getBottomSheetDialogDefaultHeight(): Int {
return getScreenRealHeight() * 70 / 100
}
Expand Down
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
Loading

0 comments on commit 0d352da

Please sign in to comment.