Skip to content

Commit

Permalink
refactoring/#31-state-pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinokurov Kirill committed Apr 19, 2019
1 parent 728e5d4 commit de91d74
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 53 deletions.
29 changes: 0 additions & 29 deletions .idea/codeStyles/Project.xml

This file was deleted.

4 changes: 4 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions app/src/main/java/my/neomer/sixtyseconds/ads/AdMobAdProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package my.neomer.sixtyseconds.ads;

import android.content.Context;
import android.net.sip.SipSession;
import android.util.Log;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;

public class AdMobAdProvider extends AdListener implements IAdsProvider {
private static final String LOG_TAG = "AdMobAdProvider";

//private static final String ADMOB_APP_KEY = "ca-app-pub-5078878060587689~8320307873";
private static final String ADMOB_APP_KEY = "ca-app-pub-3940256099942544~3347511713"; // Sample adMob app
//private static final String ADMOB_BETWEEN_ACTIVITY_KEY = "ca-app-pub-5078878060587689/9345738647";
private static final String ADMOB_BETWEEN_ACTIVITY_KEY = "ca-app-pub-3940256099942544/1033173712"; // Sample InterstitialAd

private InterstitialAd interstitialAd;
private IRewardedAdResultListener resultListener;

@Override
public void initialize(Context context) {
MobileAds.initialize(context, ADMOB_APP_KEY);
interstitialAd = new InterstitialAd(context);
interstitialAd.setAdUnitId(ADMOB_BETWEEN_ACTIVITY_KEY);
interstitialAd.loadAd(new AdRequest.Builder().build());
interstitialAd.setAdListener(this);
}

@Override
public void onAdClosed() {
Log.d(LOG_TAG, "Ad closed. Reload...");
interstitialAd.loadAd(new AdRequest.Builder().build());
if (resultListener != null) {
resultListener.onAdComplete();
}
}

@Override
public void onAdFailedToLoad(int i) {
Log.e(LOG_TAG, "Failed to load ad! Error: " + i);
interstitialAd.loadAd(new AdRequest.Builder().build());
if (resultListener != null) {
resultListener.onAdLoadFailed();
}
}

@Override
public void onAdClicked() {
Log.d(LOG_TAG, "Ad clicked!");
interstitialAd.loadAd(new AdRequest.Builder().build());
if (resultListener != null) {
resultListener.onAdClick();
}
}

@Override
public void onAdImpression() {
Log.d(LOG_TAG, "Ad impressed!");
interstitialAd.loadAd(new AdRequest.Builder().build());
if (resultListener != null) {
resultListener.onAdClick();
}
}

@Override
public boolean showInterstitialAd(IRewardedAdResultListener listener) {
resultListener = listener;
if (interstitialAd.isLoaded()) {
interstitialAd.show();
return true;
}
return false;
}

@Override
public boolean showRewardedAd(IRewardedAdResultListener listener) {
resultListener = listener;
return false;
}
}
13 changes: 13 additions & 0 deletions app/src/main/java/my/neomer/sixtyseconds/ads/IAdsProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package my.neomer.sixtyseconds.ads;

import android.content.Context;

public interface IAdsProvider {

void initialize(Context context);

boolean showInterstitialAd(IRewardedAdResultListener listener);

boolean showRewardedAd(IRewardedAdResultListener listener);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package my.neomer.sixtyseconds.ads;

public interface IRewardedAdResultListener {

void onAdClick();

void onAdComplete();

void onAdLoadFailed();

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public int getAdsSkipped() {
public void setAdsSkipped(int adsSkipped) {
this.adsSkipped = adsSkipped;
}
public void adsSkipped() { this.adsSkipped++; }
public Question.Difficulty getDifficulty() {
return difficulty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.google.android.gms.ads.MobileAds;

import my.neomer.sixtyseconds.R;
import my.neomer.sixtyseconds.ads.AdMobAdProvider;
import my.neomer.sixtyseconds.ads.IAdsProvider;
import my.neomer.sixtyseconds.transport.HttpQuestionProvider;
import my.neomer.sixtyseconds.transport.IQuestionProvider;

Expand All @@ -20,9 +22,6 @@ public class ApplicationResources {

private static final ApplicationResources ourInstance = new ApplicationResources();
private static final int MAX_STREAMS = 1;
private static final String ADMOB_APP_KEY = "ca-app-pub-5078878060587689~8320307873";
private static final String ADMOB_BETWEEN_ACTIVITY_KEY = "ca-app-pub-5078878060587689/9345738647";
private InterstitialAd interstitialAd;

public static ApplicationResources getInstance() {
return ourInstance;
Expand All @@ -34,6 +33,7 @@ private ApplicationResources() {
//endregion

private IQuestionProvider questionProvider = new HttpQuestionProvider();
private IAdsProvider adsProvider = new AdMobAdProvider();

private SoundPool soundPool;
private int timeIsUpSoundId;
Expand All @@ -60,16 +60,7 @@ public void loadSounds(@NonNull Context context, @Nullable SoundPool.OnLoadCompl
}

public void loadAds(@NonNull Context context) {
MobileAds.initialize(context, ADMOB_APP_KEY);
interstitialAd = new InterstitialAd(context);
interstitialAd.setAdUnitId(ADMOB_BETWEEN_ACTIVITY_KEY);
interstitialAd.loadAd(new AdRequest.Builder().build());
interstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
interstitialAd.loadAd(new AdRequest.Builder().build());
}
});
adsProvider.initialize(context);
}

public void playClickSound() {
Expand All @@ -89,7 +80,7 @@ public IQuestionProvider getQuestionProvider() {
return questionProvider;
}

public InterstitialAd getAdsProvider() {
return interstitialAd;
public IAdsProvider getAdsProvider() {
return adsProvider;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import android.os.Parcel;
import android.os.Parcelable;

import my.neomer.sixtyseconds.ads.IRewardedAdResultListener;
import my.neomer.sixtyseconds.helpers.ApplicationResources;

public class AdsDisplayingState extends BaseState {
public class AdsDisplayingState extends BaseState implements IRewardedAdResultListener {

private static final String LOG_TAG = "AdsDisplayingState";
private static final int ADS_SKIP_COUNT = 3;
private static final int ADS_SKIP_COUNT = 0;

//region Parcelable

Expand Down Expand Up @@ -37,10 +38,8 @@ public AdsDisplayingState() {

@Override
public void start() {
if (ApplicationResources.getInstance().getAdsProvider().isLoaded() && getGameContext().getAdsSkipped() >= ADS_SKIP_COUNT) {
getGameContext().setAdsSkipped(0);
ApplicationResources.getInstance().getAdsProvider().show();
} else {
if (getGameContext().getAdsSkipped() < ADS_SKIP_COUNT || !ApplicationResources.getInstance().getAdsProvider().showInterstitialAd(this)) {
getGameContext().adsSkipped();
finish();
}
}
Expand All @@ -54,4 +53,26 @@ public void pause() {
public void proceed() {

}

//region IRewardedAdResultListener implementation

@Override
public void onAdClick() {
getGameContext().setAdsSkipped(0);
finish();
}

@Override
public void onAdComplete() {
getGameContext().setAdsSkipped(0);
finish();
}

@Override
public void onAdLoadFailed() {
getGameContext().adsSkipped();
finish();
}

//endregion
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.android.tools.build:gradle:3.4.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Mar 28 18:29:56 SAMT 2019
#Fri Apr 19 08:10:26 SAMT 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip

0 comments on commit de91d74

Please sign in to comment.