Skip to content

Conversation

@robertjamison
Copy link
Contributor

  • Add initial support for Native Ads

This commit introduces the initial structure for displaying native ads on both Android and iOS. It includes the necessary handlers, data classes, and composable functions, although the core ad loading and display logic is not yet implemented.

  • New API Components:

    • NativeAdData: A new data class in commonMain to hold the assets of a native ad (e.g., headline, body, advertiser).
    • NativeAdHandler: An expect class in commonMain with actual implementations for Android and iOS. This class will manage the lifecycle of a native ad. The current implementation is a skeleton with TODO() markers.
    • NativeAd Composable: A new composable function in commonMain to display a native ad. It takes a nativeAdTemplate composable lambda to render the ad UI.
    • rememberNativeAd Composable: A helper function to create and remember a NativeAdHandler instance within a composition.
  • Ad Unit ID:

    • Added NATIVE_DEFAULT test ad unit IDs to AdUnitId for both Android and iOS platforms.
  • Refactor Native Ad API and introduce expect/actual for NativeAdData

This commit refactors the Native Ad implementation by moving all related classes into a nativead subpackage. It also converts NativeAdData from a data class into an expect class with platform-specific actual implementations, providing a richer, type-safe representation of native ad assets.

  • Package Refactoring:

    • All files related to Native Ads (NativeAdHandler, NativeAdData, NativeAd.kt, RememberNativeAd.kt) have been moved from app.lexilabs.basic.ads to a new app.lexilabs.basic.ads.nativead package.
  • API Enhancement (NativeAdData):

    • NativeAdData in commonMain is now an expect class instead of a data class.
    • It exposes a more comprehensive set of properties, including adChoicesInfo, mediaContent, and muteThisAdReasons.
    • Introduced nested expect classes: AdChoicesInfo, Image, MediaContent, and MuteThisAdReason to provide a common abstraction over platform-specific types.
  • Platform Implementations:

    • Added actual implementations for NativeAdData on both Android and iOS, wrapping the corresponding classes from the Google Mobile Ads SDK (e.g., com.google.android.gms.ads.nativead.NativeAd on Android, GADNativeAd on iOS).
    • New NativeAdConverters.kt files have been added for both Android and iOS to handle the conversion from platform-specific ad objects to the common NativeAdData class.
  • Refactor Native Ad API and implement Android loading

This commit refactors the Native Ad API to simplify its usage and implements the ad loading and event handling logic for the Android platform.

  • API Refactoring (Common):

    • The load and setListeners functions in NativeAdHandler have been merged into a single load function. This function now accepts all necessary event callbacks (onLoad, onFailure, onDismissed, onShown, onImpression, onClick).
    • The show(nativeAdTemplate) function has been replaced with render(): NativeAdData. The NativeAd composable now calls render() and passes the resulting NativeAdData to the template.
    • A destroy() function has been added to the NativeAdHandler to clean up ad resources.
  • Android Implementation:

    • Implemented the load function in NativeAdHandler for Android. It uses AdLoader.Builder to request a native ad.
    • An AdListener is used to handle ad lifecycle events (failure, click, impression, shown, dismissed) and invoke the corresponding callbacks.
    • The render() function is implemented to convert the loaded com.google.android.gms.ads.nativead.NativeAd into the common NativeAdData class.
    • The destroy() function now calls nativeAd.destroy().
  • iOS Implementation (Partial):

    • Created AdLoaderDelegate and NativeAdDelegate to handle GADAdLoaderDelegateProtocol and GADNativeAdDelegateProtocol respectively.
    • The load function in the iOS NativeAdHandler has been updated to use GADAdLoader, but the implementation is incomplete (TODO).
  • Composable Updates:

    • The rememberNativeAd and NativeAd composables have been updated to reflect the new load and render API in NativeAdHandler.
    • The NativeAd composable that accepts a pre-loaded NativeAdHandler has been simplified, removing redundant listener parameters.
  • Implement Native Ad loading on iOS

This commit implements the logic for loading and handling native ads on the iOS platform. It introduces a new NativeAdLoader class to manage the ad lifecycle and integrates it with the existing NativeAdHandler.

  • New NativeAdLoader Class (iOS):

    • A new NativeAdLoader class has been created to encapsulate the native ad loading process on iOS.
    • It implements GADNativeAdLoaderDelegateProtocol to handle ad loading success and failure callbacks from the Google Mobile Ads SDK.
    • It uses suspendCancellableCoroutine to provide a modern, coroutine-based loadAd function.
    • It manages the GADNativeAd instance and its delegate to forward events like impressions, clicks, and dismissals.
  • Refactor NativeAdHandler (iOS):

    • The load function in NativeAdHandler is now implemented to use the new NativeAdLoader.
    • It passes callbacks (onLoad, onFailure, onShown, etc.) to the NativeAdLoader, which are used to update the AdState of the handler.
    • The render() function is updated to retrieve the loaded ad data from the NativeAdLoader.
    • The destroy() function is implemented to clear the ad reference.
  • File/Class Renaming:

    • The placeholder AdLoaderDelegate.kt file and class were renamed and their logic has been superseded by the more complete NativeAdLoader.
  • Add default native ad template and improve lifecycle

This commit introduces a default composable, NativeAdDefault, for rendering native ads and improves the lifecycle management of the NativeAdHandler.

  • New NativeAdDefault Composable:

    • Added a new expect composable NativeAdDefault in commonMain to provide a default UI for displaying native ads.
    • Provided actual implementations for both Android and iOS that render common native ad assets like the icon, headline, body, and call to action.
  • Lifecycle Management:

    • In rememberNativeAd, a DisposableEffect has been added to ensure that nativeAd.value.destroy() is called when the composable leaves the composition. This prevents potential resource leaks by properly cleaning up the NativeAdHandler.
  • Code Cleanup:

    • Removed TODO comments from the NativeAd composable, as the new NativeAdDefault serves as the intended default template.
  • Updated API Dump.

  • Improve KDoc for Ad Composables

This commit enhances the KDoc documentation for several public composable functions related to ads, improving clarity and developer guidance.

  • Documentation Added/Improved:
    • BannerAd.kt: Added KDoc for the BannerAd composable that accepts an adUnitId.
    • NativeAd.kt: Added KDoc for both overloads of the NativeAd composable.
    • NativeAdDefault.kt: Added KDoc for the expect and actual declarations of the NativeAdDefault composable.
    • RememberNativeAd.kt: Added comprehensive KDoc for the rememberNativeAd composable.

* Add initial support for Native Ads

This commit introduces the initial structure for displaying native ads on both Android and iOS. It includes the necessary handlers, data classes, and composable functions, although the core ad loading and display logic is not yet implemented.

*   **New API Components:**
    *   **`NativeAdData`**: A new data class in `commonMain` to hold the assets of a native ad (e.g., headline, body, advertiser).
    *   **`NativeAdHandler`**: An `expect` class in `commonMain` with `actual` implementations for Android and iOS. This class will manage the lifecycle of a native ad. The current implementation is a skeleton with `TODO()` markers.
    *   **`NativeAd` Composable**: A new composable function in `commonMain` to display a native ad. It takes a `nativeAdTemplate` composable lambda to render the ad UI.
    *   **`rememberNativeAd` Composable**: A helper function to create and remember a `NativeAdHandler` instance within a composition.

*   **Ad Unit ID:**
    *   Added `NATIVE_DEFAULT` test ad unit IDs to `AdUnitId` for both Android and iOS platforms.

Signed-off-by: Robert Jamison <65142411+robertjamison@users.noreply.github.com>

* Refactor Native Ad API and introduce `expect`/`actual` for `NativeAdData`

This commit refactors the Native Ad implementation by moving all related classes into a `nativead` subpackage. It also converts `NativeAdData` from a `data class` into an `expect` class with platform-specific `actual` implementations, providing a richer, type-safe representation of native ad assets.

*   **Package Refactoring:**
    *   All files related to Native Ads (`NativeAdHandler`, `NativeAdData`, `NativeAd.kt`, `RememberNativeAd.kt`) have been moved from `app.lexilabs.basic.ads` to a new `app.lexilabs.basic.ads.nativead` package.

*   **API Enhancement (`NativeAdData`):**
    *   `NativeAdData` in `commonMain` is now an `expect class` instead of a `data class`.
    *   It exposes a more comprehensive set of properties, including `adChoicesInfo`, `mediaContent`, and `muteThisAdReasons`.
    *   Introduced nested `expect` classes: `AdChoicesInfo`, `Image`, `MediaContent`, and `MuteThisAdReason` to provide a common abstraction over platform-specific types.

*   **Platform Implementations:**
    *   Added `actual` implementations for `NativeAdData` on both Android and iOS, wrapping the corresponding classes from the Google Mobile Ads SDK (e.g., `com.google.android.gms.ads.nativead.NativeAd` on Android, `GADNativeAd` on iOS).
    *   New `NativeAdConverters.kt` files have been added for both Android and iOS to handle the conversion from platform-specific ad objects to the common `NativeAdData` class.

Signed-off-by: Robert Jamison <65142411+robertjamison@users.noreply.github.com>

* Refactor Native Ad API and implement Android loading

This commit refactors the Native Ad API to simplify its usage and implements the ad loading and event handling logic for the Android platform.

*   **API Refactoring (Common):**
    *   The `load` and `setListeners` functions in `NativeAdHandler` have been merged into a single `load` function. This function now accepts all necessary event callbacks (`onLoad`, `onFailure`, `onDismissed`, `onShown`, `onImpression`, `onClick`).
    *   The `show(nativeAdTemplate)` function has been replaced with `render(): NativeAdData`. The `NativeAd` composable now calls `render()` and passes the resulting `NativeAdData` to the template.
    *   A `destroy()` function has been added to the `NativeAdHandler` to clean up ad resources.

*   **Android Implementation:**
    *   Implemented the `load` function in `NativeAdHandler` for Android. It uses `AdLoader.Builder` to request a native ad.
    *   An `AdListener` is used to handle ad lifecycle events (failure, click, impression, shown, dismissed) and invoke the corresponding callbacks.
    *   The `render()` function is implemented to convert the loaded `com.google.android.gms.ads.nativead.NativeAd` into the common `NativeAdData` class.
    *   The `destroy()` function now calls `nativeAd.destroy()`.

*   **iOS Implementation (Partial):**
    *   Created `AdLoaderDelegate` and `NativeAdDelegate` to handle `GADAdLoaderDelegateProtocol` and `GADNativeAdDelegateProtocol` respectively.
    *   The `load` function in the iOS `NativeAdHandler` has been updated to use `GADAdLoader`, but the implementation is incomplete (`TODO`).

*   **Composable Updates:**
    *   The `rememberNativeAd` and `NativeAd` composables have been updated to reflect the new `load` and `render` API in `NativeAdHandler`.
    *   The `NativeAd` composable that accepts a pre-loaded `NativeAdHandler` has been simplified, removing redundant listener parameters.

Signed-off-by: Robert Jamison <65142411+robertjamison@users.noreply.github.com>

* Implement Native Ad loading on iOS

This commit implements the logic for loading and handling native ads on the iOS platform. It introduces a new `NativeAdLoader` class to manage the ad lifecycle and integrates it with the existing `NativeAdHandler`.

*   **New `NativeAdLoader` Class (iOS):**
    *   A new `NativeAdLoader` class has been created to encapsulate the native ad loading process on iOS.
    *   It implements `GADNativeAdLoaderDelegateProtocol` to handle ad loading success and failure callbacks from the Google Mobile Ads SDK.
    *   It uses `suspendCancellableCoroutine` to provide a modern, coroutine-based `loadAd` function.
    *   It manages the `GADNativeAd` instance and its delegate to forward events like impressions, clicks, and dismissals.

*   **Refactor `NativeAdHandler` (iOS):**
    *   The `load` function in `NativeAdHandler` is now implemented to use the new `NativeAdLoader`.
    *   It passes callbacks (`onLoad`, `onFailure`, `onShown`, etc.) to the `NativeAdLoader`, which are used to update the `AdState` of the handler.
    *   The `render()` function is updated to retrieve the loaded ad data from the `NativeAdLoader`.
    *   The `destroy()` function is implemented to clear the ad reference.

*   **File/Class Renaming:**
    *   The placeholder `AdLoaderDelegate.kt` file and class were renamed and their logic has been superseded by the more complete `NativeAdLoader`.

Signed-off-by: Robert Jamison <65142411+robertjamison@users.noreply.github.com>

* Add default native ad template and improve lifecycle

This commit introduces a default composable, `NativeAdDefault`, for rendering native ads and improves the lifecycle management of the `NativeAdHandler`.

*   **New `NativeAdDefault` Composable:**
    *   Added a new `expect` composable `NativeAdDefault` in `commonMain` to provide a default UI for displaying native ads.
    *   Provided `actual` implementations for both Android and iOS that render common native ad assets like the icon, headline, body, and call to action.

*   **Lifecycle Management:**
    *   In `rememberNativeAd`, a `DisposableEffect` has been added to ensure that `nativeAd.value.destroy()` is called when the composable leaves the composition. This prevents potential resource leaks by properly cleaning up the `NativeAdHandler`.

*   **Code Cleanup:**
    *   Removed `TODO` comments from the `NativeAd` composable, as the new `NativeAdDefault` serves as the intended default template.

Signed-off-by: Robert Jamison <65142411+robertjamison@users.noreply.github.com>

* Updated API Dump.

Signed-off-by: Robert Jamison <65142411+robertjamison@users.noreply.github.com>

* Improve KDoc for Ad Composables

This commit enhances the KDoc documentation for several public composable functions related to ads, improving clarity and developer guidance.

*   **Documentation Added/Improved:**
    *   **`BannerAd.kt`**: Added KDoc for the `BannerAd` composable that accepts an `adUnitId`.
    *   **`NativeAd.kt`**: Added KDoc for both overloads of the `NativeAd` composable.
    *   **`NativeAdDefault.kt`**: Added KDoc for the `expect` and `actual` declarations of the `NativeAdDefault` composable.
    *   **`RememberNativeAd.kt`**: Added comprehensive KDoc for the `rememberNativeAd` composable.

Signed-off-by: Robert Jamison <65142411+robertjamison@users.noreply.github.com>

---------

Signed-off-by: Robert Jamison <65142411+robertjamison@users.noreply.github.com>
@robertjamison robertjamison self-assigned this Oct 3, 2025
@robertjamison robertjamison added documentation Improvements or additions to documentation enhancement New feature or request help wanted Extra attention is needed dependencies Pull requests that update a dependency file labels Oct 3, 2025
@robertjamison robertjamison linked an issue Oct 3, 2025 that may be closed by this pull request
@robertjamison robertjamison changed the title Native ads (#98) v1.0.0-beta04 (#98) Oct 3, 2025
This commit updates the library version to `1.0.0-beta04` and bumps the version of the iOS AdMob dependency.

*   **Version Update:**
    *   The ads library version is updated from `1.0.0-beta03` to `1.0.0-beta04`.

*   **Dependency Update:**
    *   The CocoaPods AdMob dependency (`cocoapods-admob`) is updated from `12.4.0` to `12.12.0`.

These changes are reflected in the `gradle/libs.versions.toml` file.

Signed-off-by: Robert Jamison <65142411+robertjamison@users.noreply.github.com>
@robertjamison robertjamison merged commit 12a00f9 into main Oct 3, 2025
5 checks passed
@robertjamison robertjamison deleted the v1.0.0-beta04 branch October 3, 2025 02:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file documentation Improvements or additions to documentation enhancement New feature or request help wanted Extra attention is needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Interop for Native Ads

2 participants