Skip to content

Commit

Permalink
Merge branch 'feature/30-player-profile' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Entreco committed May 21, 2018
2 parents 8211926 + 6253b06 commit 306d8f4
Show file tree
Hide file tree
Showing 267 changed files with 3,738 additions and 1,125 deletions.
7 changes: 5 additions & 2 deletions android/DartsScorecard/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dependencies {
kapt "android.arch.persistence.room:compiler:$room"

// Implementation
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlinVersion"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
implementation "com.google.dagger:dagger:$daggerVersion"
implementation "com.android.support:appcompat-v7:$support"
implementation "com.android.support:design:$support"
Expand All @@ -51,11 +51,14 @@ dependencies {
transitive = true
}

// Leaks
debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakCanary"
releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$leakCanary"

// Test
testImplementation "junit:junit:$junit"
testImplementation "org.mockito:mockito-core:$mockito"
testImplementation "com.nhaarman:mockito-kotlin-kt1.1:$mockitoKotlin"
testImplementation "com.github.dpreussler:android-tdd-utils:$tddUtils"

// AndroidTest
androidTestImplementation "com.android.support.test:runner:$espressoRunner"
Expand Down
3 changes: 3 additions & 0 deletions android/DartsScorecard/app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,8 @@
-dontwarn okio.**
-dontnote okio.**

# OkHttp
-dontwarn okhttp3.**

# Billing
-keep class com.android.vending.billing.**
2 changes: 2 additions & 0 deletions android/DartsScorecard/app/src/debug/res/values/keys.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- AdMob -->
<string name="app_id">ca-app-pub-3793327349392749~1846337901</string>
<string name="setup_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
<string name="setup_interstitial_unit_id">ca-app-pub-3940256099942544/1033173712</string>
</resources>
5 changes: 5 additions & 0 deletions android/DartsScorecard/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
android:name=".profile.edit.EditPlayerNameActivity"
android:parentActivityName=".profile.view.ProfileActivity" />

<service android:name=".archive.ArchiveJobService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:description="@string/archive_service"
android:exported="true" />

</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package nl.entreco.dartsscorecard

import android.app.Application
import android.os.StrictMode
import com.google.android.gms.ads.MobileAds
import com.squareup.leakcanary.LeakCanary
import nl.entreco.dartsscorecard.di.application.AppComponent
import nl.entreco.dartsscorecard.di.application.AppModule
import nl.entreco.dartsscorecard.di.application.DaggerAppComponent


/**
* Created by Entreco on 14/11/2017.
*/
Expand All @@ -15,11 +18,38 @@ class App : Application() {

override fun onCreate() {
super.onCreate()
appComponent.inject(this)
initDagger()
initStrictMode()
initLeakCanary()
initAdMob()
}

private fun initDagger() {
appComponent.inject(this)
}

private fun initStrictMode() {
if (BuildConfig.DEBUG) {
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.penaltyFlashScreen()
.build())
StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder()
.detectAll()
.penaltyLog()
.build())
}
}

private fun initLeakCanary() {
if (LeakCanary.isInAnalyzerProcess(this)) return
LeakCanary.install(this)
}

private fun initAdMob() {
MobileAds.initialize(this, "ca-app-pub-3793327349392749~1846337901")
Thread {
MobileAds.initialize(this, resources.getString(R.string.app_id))
}.start()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package nl.entreco.dartsscorecard

import android.util.Log
import nl.entreco.domain.Logger
import nl.entreco.domain.common.log.Logger

/**
* Created by Entreco on 27/11/2017.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package nl.entreco.dartsscorecard.ad

import android.databinding.BindingAdapter
import android.view.View
import com.google.android.gms.ads.AdView

/**
* Created by Entreco on 29/12/2017.
*/
abstract class AdBindings {
companion object {
@JvmStatic
@BindingAdapter("viewModel")
fun loadAd(view: AdView, viewModel: AdViewModel?) {
viewModel?.provideAdd(view)
}

@JvmStatic
@BindingAdapter("showAd")
fun showAd(view: View, show: Boolean) {
view.visibility = if (show) View.VISIBLE else View.GONE
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package nl.entreco.dartsscorecard.ad

import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView
import javax.inject.Inject
import javax.inject.Named

/**
* Created by Entreco on 29/12/2017.
*/
class AdLoader @Inject constructor() : AdListener() {

interface AdListener {
fun onAdLoaded()
fun onAdFailed()
}

private val adRequest by lazy { AdRequest.Builder().build() }
private var listener: AdListener? = null

override fun onAdLoaded() {
super.onAdLoaded()
listener?.onAdLoaded()
}

override fun onAdFailedToLoad(p0: Int) {
super.onAdFailedToLoad(p0)
listener?.onAdFailed()
}

fun loadAd(view: AdView, adListener: AdListener) {
listener = adListener
view.adListener = this
view.loadAd(adRequest)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package nl.entreco.dartsscorecard.ad

import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.LifecycleObserver
import android.arch.lifecycle.OnLifecycleEvent
import android.databinding.ObservableBoolean
import com.google.android.gms.ads.AdView
import nl.entreco.dartsscorecard.BuildConfig
import nl.entreco.dartsscorecard.base.BaseViewModel
import nl.entreco.dartsscorecard.di.viewmodel.ActivityScope
import nl.entreco.domain.ad.FetchPurchasedItemsResponse
import nl.entreco.domain.ad.FetchPurchasedItemsUsecase
import nl.entreco.domain.common.log.Logger
import nl.entreco.domain.purchases.connect.ConnectToBillingUsecase
import java.util.concurrent.atomic.AtomicBoolean
import javax.inject.Inject
import javax.inject.Named

@ActivityScope
class AdViewModel @Inject constructor(
@ActivityScope private val lifeCycle: Lifecycle,
@ActivityScope private val connectToBillingUsecase: ConnectToBillingUsecase,
private val fetchPurchasedItemsUsecase: FetchPurchasedItemsUsecase,
private val adLoader: AdLoader,
private val interstitialLoader: InterstitialLoader,
private val logger: Logger,
@Named("debugMode") private val debug: Boolean = BuildConfig.DEBUG) : BaseViewModel(), LifecycleObserver {

val showAd = ObservableBoolean(false)
private var serveAds = AtomicBoolean(false) // Let's give the user no Ads by default

init {
lifeCycle.addObserver(this)
}

fun provideAdd(view: AdView) {
if(!debug) {
adLoader.loadAd(view, object : AdLoader.AdListener {
override fun onAdLoaded() {
showAd.set(serveAds.get())
}

override fun onAdFailed() {
showAd.set(false)
}
})
}
}

fun provideInterstitial() {
if (!debug && serveAds.get()) {
interstitialLoader.showInterstitial()
}
}

private fun checkIfUserHasPurchasedItems() {
connectToBillingUsecase.bind { connected ->
if (connected) {
fetchPurchasedItemsUsecase.exec(onPurchasesRetrieved(), onPurchasesError())
}
}
}

private fun onPurchasesRetrieved(): (FetchPurchasedItemsResponse) -> Unit {
return { response ->
serveAds.set(response.serveAds)
}
}

private fun onPurchasesError(): (Throwable) -> Unit {
return { err ->
logger.w("Unable to fetchPurchasedItems, $err")
}
}

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun bind() {
checkIfUserHasPurchasedItems()
}

@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun unbind() {
connectToBillingUsecase.unbind()
}

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun destroy() {
lifeCycle.removeObserver(this)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package nl.entreco.dartsscorecard.ad

import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.InterstitialAd
import javax.inject.Inject
import javax.inject.Named


class InterstitialLoader @Inject constructor(
@Named("interstitialId") private val interstitialId: String,
private val interstitial: InterstitialAd) : AdListener() {

private val adRequest: AdRequest by lazy { AdRequest.Builder().build() }

init {
interstitial.adUnitId = interstitialId
interstitial.adListener = this
interstitial.loadAd(adRequest)
}

fun showInterstitial() {
if (interstitial.isLoaded) {
interstitial.show()
}
}

override fun onAdClosed() {
super.onAdClosed()
interstitial.loadAd(adRequest)
}
}
Loading

0 comments on commit 306d8f4

Please sign in to comment.