Giter Site home page Giter Site logo

shaishavgandhi / rxads Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 0.0 250 KB

RxJava wrapper for Play Services Ads

Home Page: https://shaishavgandhi05.github.io/RxAds/rxads/

License: Apache License 2.0

Kotlin 100.00%
rxjava2 admob android rxjava-wrapper ads play-services

rxads's Introduction

RxAds

Travis

RxJava wrapper for Play Services Ads. The library currently supports loading Native Ads (Native Content, Native Install, Native Custom Template) and Interstitial Ads.

Philosophy

RxAds' only aim is to make ad loading reactive. The library does not provide ways to convert clicks to ads as streams or to provide various streams for it's varied callback.

However, Play Services Ads is a big SDK and it's not the aim of this library to replicate all functionality. Hence, RxAds builds on existing functionality and allows you to load ads using reactive streams.

For example:

AdLoader adLoader = new AdLoader.Builder(context, "ca-app-pub-3940256099942544/2247696110")
    .forContentAd(new OnContentAdLoadedListener() {
        @Override
        public void onContentAdLoaded(NativeContentAd contentAd) {
            // Show the content ad.
        }
    })
    .withAdListener(new AdListener() {
        @Override
        public void onAdFailedToLoad(int errorCode) {
            // Handle the failure by logging, altering the UI, and so on.
        }
    })
    .withNativeAdOptions(new NativeAdOptions.Builder()
            // Methods in the NativeAdOptions.Builder class can be
            // used here to specify individual options settings.
            .build())
    .build();

adLoader.loadAd(new PublisherAdRequest.Builder().build());

can easily become

Disposable disposable = new RxAdLoader(context, "ad_unit_id") // Initialize same as AdLoader.Builder
                // Chain any other options from Play Services Ads
                .withNativeAdOptions(new NativeAdOptions.Builder().build())
                .loadNativeContentAd(new AdRequest.Builder().build()) // Load ad
                .subscribe(...); 

You can keep chaining your custom listeners and just call loadAd(args...) when you're done.

Usage

Native Install Ad

RxAdLoader adLoader = new RxAdLoader(this, "ad_unit_id");
adLoader.loadInstallAd(new AdRequest.Builder().build())
        .subscribeWith(new DisposableSingleObserver<NativeAppInstallAd>() {
            @Override
            public void onSuccess(NativeAppInstallAd nativeAppInstallAd) {
                // Set to view
            }

            @Override
            public void onError(Throwable e) {
                // Handle error
            }
         });

Native Content Ad

RxAdLoader adLoader = new RxAdLoader(this, "ad_unit_id");
adLoader.loadNativeContentAd(new AdRequest.Builder().build())
        .subscribeWith(new DisposableSingleObserver<NativeContentAd>() {
            @Override
            public void onSuccess(NativeContentAd nativeContentAd) {
                // Set to view                
            }

            @Override
            public void onError(Throwable e) {
                // Handle error
            }
        });

Interstitial Ad

Java

RxInterstitialAd ad = new RxInterstitialAd(this);
ad.loadAd("ad_unit_id", new AdRequest.Builder().build())
        .subscribeWith(new DisposableSingleObserver<InterstitialAd>() {
            @Override
            public void onSuccess(InterstitialAd interstitialAd) {
                interstitialAd.show();
            }

            @Override
            public void onError(Throwable e) {

            }
        });

Kotlin Extensions

The library is written in Kotlin and it has Kotlin extensions for most APIs

Interstitial Ad

val ad = InterstitialAd(this)
ad.adUnitId = "ad_unit_id"
ad.asSingle(AdRequest.Builder().build())
        .subscribe({ interstitialAd -> 
            interstitialAd.show()
        }, { throwable -> 
            // Handle error
        })

Native Install Ad

AdLoader.Builder(this, "ad_unit_id")
                .loadInstallAd(AdRequest.Builder().build())
                .subscribe({ installAd ->
                    // Set to view
                    }, { e ->
                    // Handle error
                })

Error Handling

If you've used the Play Services Ads SDK, the error handling leaves much to be desired. It returns an error code, which you must then search for. RxAds makes it easy and generates a human readable error message actually telling you what happened.

Download

Maven Central

implementation 'com.shaishavgandhi.rxads:rxads:x.y.z'

Before Play Services 15.0, projects importing multiple Play Services library had to have the same version number to avoid runtime crashes. If you haven't yet migrated to 15.0 or above, you can compile your project by excluding com.google.android.gmsfrom this library as such:

implementation ('com.shaishavgandhi.rxads:rxads:x.y.z') {
    exclude group: 'com.google.android.gms'
}

After the Play Services 15 release, this is not a problem. RxAds will have a corresponding release for every Play Services Ads release to give you the latest functionality.

Snapshots of the development version are available in Sonatype's snapshots repository.

Contributing

Contributions are welcome! We're always looking to expand the usecases and would love to accept PRs.

If you would like to contribute code you can do so through GitHub by forking the repository and sending a pull request.

Make sure your code compiles by running ./gradlew clean build.

License

Copyright 2018 Shaishav Gandhi.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

rxads's People

Contributors

shaishavgandhi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

rxads's Issues

Figure out exporting sources

Right now, you can hardly see any source code when you import RxAds. Probably something different with Kotlin libraries

Java Sample

Add a sample in the sample app that shows the API called in Java

Add support for loading multiple ads.

So, there's already an API exposed by the AdLoader
public void loadAds (AdRequest adRequest, int maxNumberOfAds)
We can expose this API via an Observable rather than the normal Single.

However, this only supports AdRequest and not PublisherAdRequest. But we can simulate that behavior by just calling loadAd() multiple times and exposing an Observable instead of a Single

  • Support loadAds(adRequest, count)
  • Retrofit older APIs with count support

Fix Tests

The current test suite makes actual requests to the Google Ad Server, which let's say is flakey at best. For Interstitial Ads, this wasn't a problem but is a bigger problem with the native ads suite. Should probably think about mocking the behavior and actually mocking ads being returned from the ad server

More sophisticated sample

Add different screens(possibly) for different types of ads so user can easily load it and see behavior.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.