main

square/leakcanary

Last updated at: 29/12/2023 09:45

Views.kt

TLDR

This file provides utility functions and extension functions related to view navigation and manipulation in the leakcanary.internal.navigation package.

Methods

inflate

This function is an extension function for the ViewGroup class that inflates a layout resource into the given ViewGroup using the LayoutInflater.from method.

activity

This property is an extension property for the View class that returns the Activity instance associated with the context of the view.

activity

This function is a generic extension function for the View class that casts the context of the view to the specified type.

onCreateOptionsMenu

This function is an extension function for the View class that sets the onCreateOptionsMenu callback for the associated NavigatingActivity and triggers a call to invalidateOptionsMenu.

goTo

This function is an extension function for the NavigatingActivity class that navigates to the specified screen.

goBack

This function is an extension function for the NavigatingActivity class that navigates back to the previous screen.

getColorCompat

This function is an extension function for the Context class that retrieves the color resource from the resources based on the SDK version.

onScreenExiting

This function is an extension function for the View class that adds a callback function to be invoked when the associated screen is exiting.

notifyScreenExiting

This function is an extension function for the View class that invokes all the registered callbacks for screen exiting.

Classes

No classes found in this file.

package leakcanary.internal.navigation

import android.app.Activity
import android.content.Context
import android.os.Build.VERSION
import android.view.LayoutInflater
import android.view.Menu
import android.view.View
import android.view.ViewGroup
import com.squareup.leakcanary.core.R

internal fun ViewGroup.inflate(layoutResId: Int) = LayoutInflater.from(context)
  .inflate(layoutResId, this, false)!!

internal val View.activity
  get() = context as Activity

@Suppress("UNCHECKED_CAST")
internal fun <T : Activity> View.activity() = context as T

internal fun View.onCreateOptionsMenu(onCreateOptionsMenu: (Menu) -> Unit) {
  activity<NavigatingActivity>().onCreateOptionsMenu = onCreateOptionsMenu
  activity.invalidateOptionsMenu()
}

internal fun View.goTo(screen: Screen) {
  activity<NavigatingActivity>().goTo(screen)
}

internal fun View.goBack() {
  activity<NavigatingActivity>().goBack()
}

internal fun Context.getColorCompat(id: Int): Int {
  return if (VERSION.SDK_INT >= 23) {
    getColor(id)
  } else {
    resources.getColor(id)
  }
}

internal fun View.onScreenExiting(block: () -> Unit) {
  @Suppress("UNCHECKED_CAST")
  var callbacks = getTag(R.id.leak_canary_notification_on_screen_exit) as MutableList<() -> Unit>?
  if (callbacks == null) {
    callbacks = mutableListOf()
    setTag(R.id.leak_canary_notification_on_screen_exit, callbacks)
  }
  callbacks.add(block)
}

internal fun View.notifyScreenExiting() {
  @Suppress("UNCHECKED_CAST")
  val callbacks = getTag(R.id.leak_canary_notification_on_screen_exit)
    as MutableList<() -> Unit>?
  callbacks?.forEach { it.invoke() }
}