Skip to main content

Overview

The Space Flight News app provides several Kotlin extension functions to simplify common Android operations. These extensions enhance readability and reduce boilerplate code. Package: com.bsvillarraga.spaceflightnews.core.extensions

Context Extensions

Source: ContextExtensions.kt

isPermissionGranted

Checks if a specific permission is granted.
fun Context.isPermissionGranted(permission: String): Boolean
permission
String
required
The permission string to check (e.g., Manifest.permission.CAMERA)
Returns: true if the permission is granted, false otherwise Example:
if (context.isPermissionGranted(Manifest.permission.POST_NOTIFICATIONS)) {
    // Send notification
}

Date Extensions

Source: DateExtensions.kt

toFormattedDate

Converts an ISO 8601 date string to a human-readable format.
fun String.toFormattedDate(): String
Input Format: yyyy-MM-dd'T'HH:mm:ss'Z' (ISO 8601 UTC) Output Format: dd/MM/yyyy hh:mm a (e.g., “04/03/2026 02:30 PM”) Returns: Formatted date string, or the original string if parsing fails Example:
val isoDate = "2026-03-04T14:30:00Z"
val formattedDate = isoDate.toFormattedDate()
// Result: "04/03/2026 02:30 PM"

Image Extensions

Source: ImageExtensions.kt

toResourceGlide

Loads an image from a local resource using Glide. Supports both regular images and GIFs.
fun ImageView.toResourceGlide(context: Context, image: Int)
context
Context
required
Android context for Glide
image
Int
required
Resource ID of the image (e.g., R.drawable.placeholder)
Example:
imageView.toResourceGlide(context, R.drawable.loading_animation)

toNetworkGlide

Loads an image from a URL using Glide.
fun ImageView.toNetworkGlide(context: Context, image: String)
context
Context
required
Android context for Glide
image
String
required
URL of the image to load
Example:
imageView.toNetworkGlide(context, article.imageUrl)

RecyclerView Extensions

Source: RecyclerViewExtensions.kt

init

Initializes a RecyclerView with a LinearLayoutManager and adapter.
fun RecyclerView.init(adapter: RecyclerView.Adapter<*>, context: Context)
adapter
RecyclerView.Adapter<*>
required
The adapter to set on the RecyclerView
context
Context
required
Context for creating the LinearLayoutManager
Example:
recyclerView.init(articlesAdapter, context)

onLoadMoreScrollListener

Adds a scroll listener that triggers pagination when the user scrolls to the bottom.
fun RecyclerView.onLoadMoreScrollListener(loadMore: () -> Unit)
loadMore
() -> Unit
required
Callback function invoked when the user reaches the bottom of the list
Example:
recyclerView.onLoadMoreScrollListener {
    viewModel.loadNextPage()
}

SearchView Extensions

Source: SearchViewExtensions.kt

queryTextListener

Configures a SearchView with callbacks for query submission and text changes.
fun SearchView.queryTextListener(
    onSubmit: (String) -> Unit, 
    onTextChanged: (String?) -> Unit
)
onSubmit
(String) -> Unit
required
Callback invoked when the user submits a search query
onTextChanged
(String?) -> Unit
required
Callback invoked when the search text is cleared (empty or null)
Example:
searchView.queryTextListener(
    onSubmit = { query ->
        viewModel.searchArticles(query)
    },
    onTextChanged = { query ->
        if (query.isNullOrEmpty()) {
            viewModel.clearSearch()
        }
    }
)

Dependencies

These extensions use the following libraries:
  • Glide - Image loading (for Image extensions)
  • AndroidX - Core Android libraries
  • RecyclerView - List components

Build docs developers (and LLMs) love