-
Notifications
You must be signed in to change notification settings - Fork 590
/
AuthenticationFragment.kt
68 lines (53 loc) · 2.18 KB
/
AuthenticationFragment.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package io.homeassistant.companion.android.onboarding.authentication
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.fragment.app.Fragment
import io.homeassistant.companion.android.R
class AuthenticationFragment : Fragment(), AuthenticationView {
companion object {
private const val TAG = "AuthenticationFragment"
private const val EXTRA_URL = "extra_url"
fun newInstance(url: String): AuthenticationFragment {
return AuthenticationFragment().apply {
this.arguments = Bundle().apply {
this.putString(EXTRA_URL, url)
}
}
}
}
private lateinit var presenter: AuthenticationPresenter
private lateinit var webView: WebView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
presenter = AuthenticationPresenterImpl(this)
presenter.initialize(arguments?.getString(EXTRA_URL) ?: throw IllegalArgumentException("Url should not be null"))
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_authentication, container, false).apply {
webView = findViewById(R.id.webview)
webView.apply {
settings.javaScriptEnabled = true
settings.domStorageEnabled = true
webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String): Boolean {
return presenter.onRedirectUrl(url)
}
}
}
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
presenter.onViewReady()
}
override fun loadUrl(url: String) {
webView.loadUrl(url)
}
override fun openWebview(url: String) {
(activity as AuthenticationListener).onAuthenticationSuccess(url)
}
}