Skip to content

Commit

Permalink
Merge branch '48-back-handling' into 'master'
Browse files Browse the repository at this point in the history
Resolve "Back handling"

Closes #48

See merge request pace/mobile/android/pace-cloud-sdk!41
  • Loading branch information
Melle010 committed Feb 11, 2021
2 parents d4172f6 + c200b6f commit b87756e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class AppWebView(context: Context, attributeSet: AttributeSet) : RelativeLayout(
webView.addJavascriptInterface(InvalidTokenInterface(), "pace_invalidToken")
webView.addJavascriptInterface(ImageDataInterface(), "pace_imageData")
webView.addJavascriptInterface(VerifyLocationInterface(), "pace_verifyLocation")
webView.addJavascriptInterface(BackInterface(), "pace_back")
webView.addJavascriptInterface(CloseInterface(), "pace_close")
webView.addJavascriptInterface(GetBiometricStatusInterface(), "pace_getBiometricStatus")
webView.addJavascriptInterface(SetTOTPSecretInterface(), "pace_setTOTPSecret")
Expand Down Expand Up @@ -180,6 +181,14 @@ class AppWebView(context: Context, attributeSet: AttributeSet) : RelativeLayout(
webViewModel.secureData.observe(lifecycleOwner, secureDataObserver)
}

private fun handleBack() {
if (webView.canGoBack()) {
webView.goBack()
} else {
webViewModel.handleClose()
}
}

private fun sendMessageCallback(message: String) {
webView.evaluateJavascript("window.messageCallback('$message')") {}
}
Expand All @@ -205,10 +214,17 @@ class AppWebView(context: Context, attributeSet: AttributeSet) : RelativeLayout(
}
}

inner class BackInterface {
@JavascriptInterface
fun postMessage(message: String) {
onMainThread { handleBack() }
}
}

inner class CloseInterface {
@JavascriptInterface
fun postMessage(message: String) {
onMainThread { webViewModel.handleClose(message) }
onMainThread { webViewModel.handleClose() }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.google.gson.JsonSyntaxException
import dev.turingcomplete.kotlinonetimepassword.HmacAlgorithm
import dev.turingcomplete.kotlinonetimepassword.TimeBasedOneTimePasswordConfig
import dev.turingcomplete.kotlinonetimepassword.TimeBasedOneTimePasswordGenerator
import kotlinx.android.synthetic.main.app_web_view.view.*
import org.apache.commons.codec.binary.Base32
import java.util.*
import java.util.concurrent.TimeUnit
Expand All @@ -49,7 +50,7 @@ abstract class AppWebViewModel : ViewModel(), AppWebViewClient.WebClientCallback
abstract fun handleInvalidToken(message: String)
abstract fun handleImageData(message: String)
abstract fun handleVerifyLocation(message: String)
abstract fun handleClose(message: String)
abstract fun handleClose()
abstract fun handleGetBiometricStatus(message: String)
abstract fun handleSetTOTPSecret(message: String)
abstract fun handleGetTOTP(message: String)
Expand Down Expand Up @@ -173,7 +174,7 @@ class AppWebViewModelImpl(
}
}

override fun handleClose(message: String) {
override fun handleClose() {
appModel.close(true)
}

Expand Down Expand Up @@ -338,7 +339,7 @@ class AppWebViewModelImpl(
} catch (e: JsonSyntaxException) {
statusCode.value = Event(StatusCodeResponse.Failure("The disable JSON $message could not be deserialized.", StatusCode.InternalError.code))
} finally {
handleClose(message)
handleClose()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class AppWebViewModelTest {

@Test
fun `close app`() {
viewModel.handleClose("")
viewModel.handleClose()

verify(appCallback, times(1)).onClose()
}
Expand Down

0 comments on commit b87756e

Please sign in to comment.