Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(control): Mobile Ads (Banner and Interstitial) #3288

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

ndonkoHenri
Copy link
Collaborator

@ndonkoHenri ndonkoHenri commented May 14, 2024

Closes #286

Test Code:

import flet as ft
import flet.ads as ads


def main(page: ft.Page):
    page.horizontal_alignment = ft.CrossAxisAlignment.CENTER

    id_interstitial = (
        "ca-app-pub-3940256099942544/1033173712"
        if page.platform == ft.PagePlatform.ANDROID
        else "ca-app-pub-3940256099942544/4411468910"
    )

    id_banner = (
        "ca-app-pub-3940256099942544/6300978111"
        if page.platform == ft.PagePlatform.ANDROID
        else "ca-app-pub-3940256099942544/2934735716"
    )

    def handle_interstitial_close(e):
        nonlocal iad
        print("InterstitialAd closed")
        page.overlay.remove(e.control)
        page.overlay.append(iad := get_new_interstitial_ad())
        page.update()

    def get_new_interstitial_ad():
        return ads.InterstitialAd(
            unit_id=id_interstitial,
            on_load=lambda e: print("InterstitialAd loaded"),
            on_error=lambda e: print("InterstitialAd error", e.data),
            on_open=lambda e: print("InterstitialAd opened"),
            on_close=handle_interstitial_close,
            on_impression=lambda e: print("InterstitialAd impression"),
            on_click=lambda e: print("InterstitialAd clicked"),
        )

    def display_new_banner_ad():
        page.add(
            ft.Container(
                content=ads.BannerAd(
                    unit_id=id_banner,
                    on_click=lambda e: print("BannerAd clicked"),
                    on_load=lambda e: print("BannerAd loaded"),
                    on_error=lambda e: print("BannerAd error", e.data),
                    on_open=lambda e: print("BannerAd opened"),
                    on_close=lambda e: print("BannerAd closed"),
                    on_impression=lambda e: print("BannerAd impression"),
                    on_will_dismiss=lambda e: print("BannerAd will dismiss"),
                ),
                width=320,
                height=50,
                bgcolor=ft.colors.TRANSPARENT,
            )
        )

    page.overlay.append(iad := get_new_interstitial_ad())
    page.appbar = ft.AppBar(
        adaptive=True,
        title=ft.Text("Mobile Ads Playground"),
        bgcolor=ft.colors.LIGHT_BLUE_300,
    )
    page.add(
        ft.OutlinedButton("Show InterstitialAd", on_click=lambda e: iad.show()),
        ft.OutlinedButton("Show BannerAd", on_click=lambda e: display_new_banner_ad()),
    )


ft.app(main)

Summary by Sourcery

This pull request adds support for mobile ads (Banner and Interstitial) to the Flet framework. It includes new classes for managing these ads, updates to the main Dart file for initialization, and modifications to the pubspec.yaml to include the new package. Initial documentation for the new controls is also provided.

  • New Features:
    • Introduced support for mobile ads, including Banner and Interstitial ads, in the Flet framework.
    • Added new classes for handling mobile ads: BannerAd, InterstitialAd, and NativeAd with associated event handlers for load, error, open, close, impression, click, and will dismiss events.
  • Enhancements:
    • Updated the main Dart file to initialize the new mobile ads package and ensure it is properly set up.
    • Modified the pubspec.yaml to include the new flet_mobile_ads package.
  • Documentation:
    • Added initial documentation for the new mobile ads controls in the README.md file of the flet_mobile_ads package.

# Conflicts:
#	client/lib/main.dart
#	client/pubspec.lock
#	client/pubspec.yaml
# Conflicts:
#	client/pubspec.lock
@zox47
Copy link

zox47 commented Jun 18, 2024

Can you add (Rewarded interstitial - Interstitial Video - App Open)

@ndonkoHenri ndonkoHenri changed the title Mobile Ads feat(control): Mobile Ads (Banner and Interstitial) Jul 26, 2024
@ndonkoHenri ndonkoHenri marked this pull request as ready for review July 26, 2024 14:11
Copy link
Contributor

sourcery-ai bot commented Jul 26, 2024

Reviewer's Guide by Sourcery

This pull request introduces support for mobile ads (Banner and Interstitial) in the application. It includes the creation of new classes for handling different types of ads, integration of the flet_mobile_ads package into the main client application, and implementation of controls for handling these ads in Flutter. The changes ensure that the ads are properly initialized and managed within the application.

File-Level Changes

Files Changes
sdk/python/packages/flet-core/src/flet_core/ads/native.py
sdk/python/packages/flet-core/src/flet_core/ads/base_ad.py
sdk/python/packages/flet-core/src/flet_core/ads/interstitial.py
sdk/python/packages/flet-core/src/flet_core/ads/banner.py
Added new classes for handling different types of ads (Native, Base, Interstitial, Banner) with properties and event handlers.
packages/flet_mobile_ads/lib/src/native.dart
packages/flet_mobile_ads/lib/src/banner.dart
packages/flet_mobile_ads/lib/src/interstitial.dart
packages/flet_mobile_ads/lib/src/create_control.dart
Implemented controls for handling native, banner, and interstitial ads in Flutter, and created a control factory.
client/lib/main.dart
client/pubspec.yaml
Integrated flet_mobile_ads package into the main client application, including initialization and control creation.

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ndonkoHenri - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟡 Documentation: 1 issue found

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

client/lib/main.dart Show resolved Hide resolved
packages/flet_mobile_ads/lib/src/banner.dart Show resolved Hide resolved
packages/flet_mobile_ads/CHANGELOG.md Outdated Show resolved Hide resolved
@ndonkoHenri
Copy link
Collaborator Author

ndonkoHenri commented Jul 30, 2024

Waiting on #3345 to finalize
(the unit ID for the ads is passed as property, but however needs to be added to iOS .plist and Android .xml equally)

@zox47
Copy link

zox47 commented Aug 17, 2024

.

Waiting on #3345 to finalize (the unit ID for the ads is passed as property, but however needs to be added to iOS .plist and Android .xml equally)

please can you add native ads in documentation?

@ndonkoHenri
Copy link
Collaborator Author

I faced some issues while trying to add Native ads (forgot which one though).
It's files are present in this PR, but the Control can't be accessed nor used as its not fully ready-for-use.

For a start, we will have just Banner and Interstitial ads. Native might come up later on...

ndonkoHenri and others added 4 commits November 7, 2024 03:05
# Conflicts:
#	client/android/app/src/main/AndroidManifest.xml
#	client/lib/main.dart
#	client/pubspec.lock
#	sdk/python/packages/flet-core/src/flet_core/__init__.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Google Mobile and Web Ads integration
2 participants