From 3a8129aa515752ebc1f8277d73d47dbf316d9d89 Mon Sep 17 00:00:00 2001 From: Hinton Date: Tue, 13 Aug 2024 13:19:50 +0200 Subject: [PATCH] Update android example --- .../bitwarden/myapplication/MainActivity.kt | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/languages/kotlin/app/src/main/java/com/bitwarden/myapplication/MainActivity.kt b/languages/kotlin/app/src/main/java/com/bitwarden/myapplication/MainActivity.kt index c65e0d3c6..68a076694 100644 --- a/languages/kotlin/app/src/main/java/com/bitwarden/myapplication/MainActivity.kt +++ b/languages/kotlin/app/src/main/java/com/bitwarden/myapplication/MainActivity.kt @@ -2,6 +2,8 @@ package com.bitwarden.myapplication import android.content.Context import android.os.Bundle +import android.view.View +import android.view.ViewTreeObserver import androidx.activity.compose.setContent import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row @@ -22,6 +24,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.dp import androidx.fragment.app.FragmentActivity +import androidx.lifecycle.lifecycleScope import com.bitwarden.core.DateTime import com.bitwarden.vault.Folder import com.bitwarden.core.InitOrgCryptoRequest @@ -87,9 +90,31 @@ class MainActivity : FragmentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) biometric = Biometric(this) - client = Client(null) + http = httpClient() + lifecycleScope.launch { + client = Client.factory(null) + } + + // Set up an OnPreDrawListener to the root view. + val content: View = findViewById(android.R.id.content) + content.viewTreeObserver.addOnPreDrawListener( + object : ViewTreeObserver.OnPreDrawListener { + override fun onPreDraw(): Boolean { + // Check whether the initial data is ready. + return if (client != null) { + // The content is ready. Start drawing. + content.viewTreeObserver.removeOnPreDrawListener(this) + true + } else { + // The content isn't ready. Suspend. + false + } + } + } + ) + setContent { MyApplicationTheme { // A surface container using the 'background' color from the theme @@ -161,7 +186,7 @@ class MainActivity : FragmentActivity() { Button({ GlobalScope.launch { client.destroy() - client = Client(null) + client = Client.factory(null) outputText.value = "OK" } }) {