Skip to content

Commit

Permalink
Some improvements to the UI hint
Browse files Browse the repository at this point in the history
  • Loading branch information
hichamboushaba committed Dec 4, 2024
1 parent 613cf49 commit 725d6e1
Showing 1 changed file with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.app.Application
import android.app.Application.ActivityLifecycleCallbacks
import android.graphics.Color
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
Expand All @@ -14,6 +15,7 @@ import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.appcompat.app.AlertDialog
import androidx.core.view.doOnLayout
import androidx.core.view.setPadding
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -70,6 +72,13 @@ class ApiFakerUiHelper @Inject constructor() : ActivityLifecycleCallbacks {

@SuppressLint("SetTextI18n")
private fun View.showApiFakerHint(activity: ComponentActivity) {
fun dpToPx(dp: Int): Int {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dp.toFloat(),
resources.displayMetrics
).toInt()
}
// This works only for activities that has the content view as a direct child of the FrameLayout, which is true
// for all AppCompat activities, so it should work for all the cases we need.
val contentLayout = findViewById<View>(android.R.id.content) as? FrameLayout ?: return
Expand All @@ -78,6 +87,7 @@ class ApiFakerUiHelper @Inject constructor() : ActivityLifecycleCallbacks {
val apiFakerHint = FrameLayout(context).apply {
id = apiFakerHintId
setBackgroundColor(Color.RED)
setPadding(dpToPx(4))
addView(
TextView(context).apply {
text = "ApiFaker Enabled"
Expand Down Expand Up @@ -114,8 +124,11 @@ class ApiFakerUiHelper @Inject constructor() : ActivityLifecycleCallbacks {

private fun View.hideApiFakerHint() {
val contentLayout = findViewById<ViewGroup>(android.R.id.content)
val activityLayout = contentLayout.getChildAt(0)

contentLayout.findViewById<View>(apiFakerHintId)?.let { apiFakerHint ->
contentLayout.removeView(apiFakerHint)
activityLayout.updateLayoutParams<MarginLayoutParams> { bottomMargin = 0 }
}
}
}

0 comments on commit 725d6e1

Please sign in to comment.