Skip to content
Iseeyourmonsters edited this page Jun 4, 2025 · 1 revision

Search Module Documentation

Table of Contents


Overview

The Search Module provides a comprehensive solution for searching and displaying images within the application. It follows the MVVM architecture and leverages Android’s modern components for efficient UI updates and data handling.

Key Features

  • Image search functionality
  • Real-time search results display
  • Image preview and selection
  • Efficient data management

Components

SearchFragment

Purpose

SearchFragment serves as the main UI component for displaying search results and handling user interactions. It manages the search input, displays results, and handles image selection.

Key Methods

onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

Initializes the RecyclerView, sets up the adapter, and binds ViewModel data.

Implementation Details:

  • Sets up a grid layout for search results.
  • Observes SearchViewModel's image list for data changes.
  • Handles search input and triggers search operations.
onImageClick(Uri uri)
@Override
public void onImageClick(Uri uri)

Handles image click events, launching the detail view for the selected image.

Usage Example

SearchFragment fragment = new SearchFragment();
getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.container, fragment)
    .commit();

SearchViewModel

Purpose

SearchViewModel manages the data and business logic for the search functionality, following the MVVM pattern. It handles loading, searching, and displaying images.

Key Properties

private final MutableLiveData<List<Uri>> imageList
private final MutableLiveData<Boolean> isLoading
private final MutableLiveData<String> error

Key Methods

searchImages(String query)
public void searchImages(String query)

Searches for images based on the provided query and updates LiveData.

Parameters:

  • query: The search query string
loadImages()
public void loadImages()

Loads images from the local file system or database.

Usage Example

SearchViewModel viewModel = new ViewModelProvider(this).get(SearchViewModel.class);
viewModel.getImageList().observe(this, images -> {
    // Update UI
});
viewModel.searchImages("nature");

ImageAdapter

Purpose

ImageAdapter is a RecyclerView adapter for displaying image thumbnails in a grid. It manages image loading and click event delegation.

Key Components

Constructor
public ImageAdapter(List<Uri> imageList, OnImageClickListener listener)

Initializes the adapter with a list of image URIs and a click listener.

updateData(List<Uri> imageList)
public void updateData(List<Uri> imageList)

Updates the adapter’s data and refreshes the view.

ViewHolder
class ImageViewHolder extends RecyclerView.ViewHolder

Holds references to the image view.

OnImageClickListener
public interface OnImageClickListener {
    void onImageClick(Uri uri);
}

Callback interface for image click events.

Usage Example

ImageAdapter adapter = new ImageAdapter(imageUris, uri -> {
    // Handle image click
});
recyclerView.setAdapter(adapter);