Created
May 7, 2024 06:02
-
-
Save UzBestDeveloper/31a552798a244282902b30ba41d53dc2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@AndroidEntryPoint | |
class MainActivity : AppCompatActivity() { | |
private val mainViewModel: MainViewModel by viewModels() | |
private lateinit var binding: ActivityMainBinding | |
private var baseUrl = "https://id.egov.uz" | |
private var responseType = "one_code" | |
private var clientId = "admin-qurilish_sport_uz" | |
private var redirectUri = "https://my.sport.uz/" | |
private var scope = "vakansiya.edu.uz" | |
private var scopeType = "I" | |
private var state = "testState" | |
private val myTag = "MainActivity" | |
@SuppressLint("SetJavaScriptEnabled") | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = ActivityMainBinding.inflate(layoutInflater) | |
setContentView(binding.root) | |
val url = "$baseUrl?client_id=$clientId&redirect_uri=$redirectUri&response_type=$responseType&scope=$scope&scopeType=$scopeType&state=$state" | |
binding.webView.webViewClient = MyWebViewClient(binding.root.context) | |
binding.webView.loadUrl(url) | |
binding.webView.settings.javaScriptEnabled = true | |
binding.webView.settings.domStorageEnabled = true | |
} | |
inner class MyWebViewClient internal constructor(private val context: Context) : | |
WebViewClient() { | |
override fun shouldOverrideUrlLoading( | |
view: WebView?, | |
request: WebResourceRequest? | |
): Boolean { | |
val url: String = request?.url.toString() | |
Log.d(myTag, "Redirect URI: $url") // Log the redirect URI | |
if (url.contains("code=")) { | |
val index1 = url.indexOf("code=") | |
val index2 = url.indexOf("&state") | |
val code = url.substring(index1 + 5, index2) | |
mainViewModel.getCode(code).observe(this@MainActivity) { | |
if (it.status == Status.SUCCESS) { | |
Log.d(myTag, "shouldOverrideUrlLoading: ${it.data}") | |
val intent = Intent(this@MainActivity, SecondActivity::class.java) | |
startActivity(intent) | |
} | |
} | |
} else { | |
view?.loadUrl(url) | |
} | |
return true | |
} | |
override fun onPageFinished(view: WebView?, url: String?) { | |
if (url?.contains("code=") == true) { | |
val index1 = url.indexOf("code=") | |
val index2 = url.indexOf("&state") | |
val code = url.substring(index1 + 5, index2) | |
mainViewModel.getCode(code).observe(this@MainActivity) { | |
if (it.status == Status.SUCCESS) { | |
Log.d(myTag, "shouldOverrideUrlLoading: ${it.data}") | |
val intent = Intent(this@MainActivity, SecondActivity::class.java) | |
startActivity(intent) | |
} | |
} | |
} | |
} | |
override fun onReceivedError( | |
view: WebView, | |
request: WebResourceRequest, | |
error: WebResourceError | |
) { | |
Toast.makeText(context, "Got Error! $error", Toast.LENGTH_SHORT).show() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment