Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making pRMT compatible with radar-commons-android (ktor-coroutines) #152

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
58 changes: 34 additions & 24 deletions app/src/main/java/org/radarbase/android/widget/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,53 @@ import android.graphics.drawable.Animatable2
import android.graphics.drawable.AnimatedVectorDrawable
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.MainThread
import androidx.appcompat.app.AppCompatActivity
import io.ktor.http.Url
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.radarbase.android.RadarApplication.Companion.radarApp
import org.radarcns.detail.InfoActivity
import org.radarcns.detail.R

@MainThread
fun ImageView.repeatAnimation() {
val avd = drawable as? AnimatedVectorDrawable ?: return
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
avd.registerAnimationCallback(
object : Animatable2.AnimationCallback() {
override fun onAnimationEnd(drawable: Drawable?) {
post { avd.start() }
}
suspend fun ImageView.repeatAnimation() {
withContext(Dispatchers.Main) {
val avd = drawable as? AnimatedVectorDrawable ?: return@withContext
avd.registerAnimationCallback(object : Animatable2.AnimationCallback() {
override fun onAnimationEnd(drawable: Drawable?) {
post { avd.start() }
}
)
})
avd.start()
}
avd.start()
}

@MainThread
fun AppCompatActivity.addPrivacyPolicy(view: TextView) {
radarApp.configuration.config.observe(this) { config ->
val privacyPolicyUrl = config.optString(InfoActivity.PRIVACY_POLICY)
if (privacyPolicyUrl != null) {
view.setText(R.string.privacy_policy)
view.setOnClickListener {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(privacyPolicyUrl)))
suspend fun AppCompatActivity.addPrivacyPolicy(view: TextView, activityScope: CoroutineScope) {
activityScope.launch(Dispatchers.Main) {
radarApp.configuration.config.collect { config ->
val privacyPolicyUrl = config.optString(InfoActivity.PRIVACY_POLICY)
if (privacyPolicyUrl != null) {
view.setText(R.string.privacy_policy)
view.setOnClickListener {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(privacyPolicyUrl)))
}
view.visibility = View.VISIBLE
} else {
view.visibility = View.GONE
}
view.visibility = View.VISIBLE
} else {
view.visibility = View.GONE
}
}
}

fun String.toUrlOrNull(): Url? {
return try {
Url(this)
} catch (e: IllegalArgumentException) {
null
}
}

4 changes: 3 additions & 1 deletion app/src/main/java/org/radarcns/detail/AuthServiceImpl.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package org.radarcns.detail

import kotlinx.coroutines.flow.StateFlow
import org.radarbase.android.auth.AppAuthState
import org.radarbase.android.auth.AuthService
import org.radarbase.android.auth.LoginManager
import org.radarbase.android.auth.portal.ManagementPortalLoginManager

class AuthServiceImpl : AuthService() {
override fun createLoginManagers(appAuth: AppAuthState): List<LoginManager> = listOf(
override suspend fun createLoginManagers(appAuth: StateFlow<AppAuthState>): List<LoginManager> = listOf(
ManagementPortalLoginManager(this, appAuth),
)

override fun showLoginNotification() = Unit
}

21 changes: 13 additions & 8 deletions app/src/main/java/org/radarcns/detail/InfoActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,34 @@ import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.google.android.gms.oss.licenses.OssLicensesMenuActivity
import kotlinx.coroutines.launch
import org.radarbase.android.RadarApplication.Companion.radarConfig
import org.radarbase.android.RadarConfiguration.Companion.BASE_URL_KEY

class InfoActivity : AppCompatActivity() {
private var policyUrl: String? = null

public override fun onCreate(bundle: Bundle?) {
super.onCreate(bundle)
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContentView(R.layout.compact_info)

findViewById<TextView>(R.id.app_name).setText(R.string.app_name)
findViewById<TextView>(R.id.version).text = BuildConfig.VERSION_NAME

radarConfig.config.observe(this) { config ->
findViewById<TextView>(R.id.server_base_url).text = config.getString(BASE_URL_KEY, "")
lifecycleScope.launch {
radarConfig.config.collect { config ->
findViewById<TextView>(R.id.server_base_url).text =
config.getString(BASE_URL_KEY, "")

policyUrl = config.optString(PRIVACY_POLICY)
policyUrl = config.optString(PRIVACY_POLICY)

if (policyUrl == null) {
findViewById<TextView>(R.id.generalPrivacyPolicyStatement).apply {
visibility = View.GONE
if (policyUrl == null) {
findViewById<TextView>(R.id.generalPrivacyPolicyStatement).apply {
visibility = View.GONE
}
}
}
}
Expand Down
Loading
Loading