Skip to content

Commit

Permalink
#14: Everything working
Browse files Browse the repository at this point in the history
  • Loading branch information
Entreco committed Mar 24, 2021
1 parent b1504f4 commit e7c6982
Show file tree
Hide file tree
Showing 26 changed files with 245 additions and 140 deletions.
1 change: 0 additions & 1 deletion android/DartsScorecard/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.firebase.crashlytics'
apply from: '../scripts/android_common.gradle'
apply from: '../scripts/versioning.gradle'
Expand Down
1 change: 0 additions & 1 deletion android/DartsScorecard/app/release/output.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package nl.entreco.dartsscorecard.base

import android.app.Activity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import android.view.MenuItem
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import android.view.MenuItem
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import nl.entreco.dartsscorecard.App
import nl.entreco.dartsscorecard.R
import nl.entreco.dartsscorecard.di.viewmodel.ViewModelComponent
Expand All @@ -19,7 +19,7 @@ import nl.entreco.dartsscorecard.faq.WtfActivity
*/
abstract class ViewModelActivity : AppCompatActivity() {

private val styler by lazy { Styler( this) }
private val styler by lazy { Styler(this) }

val Activity.app: App
get() = application as App
Expand All @@ -28,7 +28,8 @@ abstract class ViewModelActivity : AppCompatActivity() {
@Suppress("UNCHECKED_CAST")
inline fun <reified VM : ViewModel> viewModelProvider(
mode: LazyThreadSafetyMode = LazyThreadSafetyMode.NONE,
crossinline provider: () -> VM) = lazy(mode) {
crossinline provider: () -> VM,
) = lazy(mode) {
ViewModelProvider(this, object : ViewModelProvider.Factory {
override fun <T1 : ViewModel> create(aClass: Class<T1>) =
provider() as T1
Expand All @@ -37,7 +38,8 @@ abstract class ViewModelActivity : AppCompatActivity() {

inline fun <reified VM> componentProvider(
mode: LazyThreadSafetyMode = LazyThreadSafetyMode.NONE,
crossinline provider: (ViewModelComponent) -> VM) = lazy(mode) {
crossinline provider: (ViewModelComponent) -> VM,
) = lazy(mode) {
val component = app.appComponent.plus(ViewModelModule(this))
provider(component)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package nl.entreco.dartsscorecard.beta
import android.view.View
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.bottomsheet.BottomSheetBehavior
import kotlinx.android.synthetic.main.include_beta_detail.view.voteFab
import nl.entreco.dartsscorecard.databinding.ActivityBetaBinding
import kotlin.math.abs
import kotlin.math.max
Expand All @@ -15,7 +14,7 @@ class BetaAnimator(binding: ActivityBetaBinding) {

private val behaviour: BottomSheetBehavior<View> = BottomSheetBehavior.from(binding.sheet)
private val appBar = binding.includeToolbar.betaAppbar
private val animator = BetaAnimatorHandler(appBar, binding.includeToolbar.toolbar, binding.sheet, binding.sheet.voteFab)
private val animator = BetaAnimatorHandler(appBar, binding.includeToolbar.toolbar, binding.sheet, binding.includeBetaDetail.voteFab)
internal var toggler: Toggler? = null
internal var swapper: Swapper? = null

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package nl.entreco.dartsscorecard.di.play

import dagger.Subcomponent
import nl.entreco.dartsscorecard.dynamic.DynamicInstaller
import nl.entreco.dartsscorecard.dynamic.Installer
import nl.entreco.dartsscorecard.play.Play01Navigator
import nl.entreco.dartsscorecard.play.Play01ViewModel
import nl.entreco.dartsscorecard.play.input.InputViewModel
Expand All @@ -22,6 +24,7 @@ interface Play01Component {
fun inputViewModel(): InputViewModel
fun statViewModel(): LiveStatViewModel
fun finishUsecase(): GetFinishUsecase
fun installer() : Installer
fun billing() : BillingRepo
fun ads(): AdViewModel
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import com.google.android.play.core.splitinstall.SplitInstallRequest
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import dagger.Module
import dagger.Provides
import nl.entreco.dartsscorecard.R
import nl.entreco.dartsscorecard.archive.ArchiveJobService
import nl.entreco.dartsscorecard.archive.ArchiveServiceLauncher
import nl.entreco.dartsscorecard.dynamic.DynamicInstaller
import nl.entreco.dartsscorecard.dynamic.Installer
import nl.entreco.dartsscorecard.dynamic.NoInstaller
import nl.entreco.dartsscorecard.dynamic.NoMusicRepository
import nl.entreco.dartsscorecard.dynamic.NoSoundRepository
import nl.entreco.dartsscorecard.dynamic.SoundModuleProvider
Expand Down Expand Up @@ -40,7 +42,6 @@ class Play01Module(
private val listener: (MakePurchaseResponse) -> Unit,
) {


@Provides
@Play01Scope
fun provideBillingService(logger: Logger): BillingRepo {
Expand All @@ -61,37 +62,45 @@ class Play01Module(

@Provides
@Play01Scope
fun provideSplitInstallRequest(@ActivityScope context: Context) = SplitInstallRequest.newBuilder()
fun provideSplitInstallRequest() = SplitInstallRequest.newBuilder()
.addModule(SOUNDS)
.build()

@Provides
@Play01Scope
fun provideSoundRepository(
@ActivityScope context: Context,
splitInstallRequest: SplitInstallRequest,
splitInstallManager: SplitInstallManager,
prefs: AudioPrefRepository,
logger: Logger,
): SoundRepository {
return if (splitInstallManager.installedModules.contains(SOUNDS)) {
val provider = Class.forName(DYNAMIC_PROVIDER).kotlin.objectInstance as SoundModuleProvider
provider.provideSoundRepository(context, prefs)
} else NoSoundRepository(logger, splitInstallRequest, splitInstallManager)
val provider = Class.forName(DYNAMIC_PROVIDER).kotlin.objectInstance as? SoundModuleProvider
val updatedContext = context.createPackageContext(context.packageName, 0)
provider?.provideSoundRepository(updatedContext.applicationContext, prefs) ?: NoSoundRepository()
} else NoSoundRepository()
}

@Provides
@Play01Scope
fun provideMusicRepository(
@ActivityScope context: Context,
splitInstallRequest: SplitInstallRequest,
splitInstallManager: SplitInstallManager,
logger: Logger,
): MusicRepository {
return if (splitInstallManager.installedModules.contains(SOUNDS)) {
val provider = Class.forName(DYNAMIC_PROVIDER).kotlin.objectInstance as SoundModuleProvider
provider.provideMusicRepository(context)
} else NoMusicRepository(logger, splitInstallRequest, splitInstallManager)
val provider = Class.forName(DYNAMIC_PROVIDER).kotlin.objectInstance as? SoundModuleProvider
val updatedContext = context.createPackageContext(context.packageName, 0)
provider?.provideMusicRepository(updatedContext.applicationContext) ?: NoMusicRepository()
} else NoMusicRepository()
}

@Provides
@Play01Scope
fun provideDynamicInstaller(
splitInstallRequest: SplitInstallRequest,
splitInstallManager: SplitInstallManager,
): Installer {
return if (splitInstallManager.installedModules.contains(SOUNDS)) NoInstaller()
else DynamicInstaller(splitInstallRequest, splitInstallManager)
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package nl.entreco.dartsscorecard.dynamic

import com.google.android.play.core.ktx.status
import com.google.android.play.core.splitinstall.SplitInstallManager
import com.google.android.play.core.splitinstall.SplitInstallRequest
import com.google.android.play.core.splitinstall.SplitInstallSessionState
import com.google.android.play.core.splitinstall.SplitInstallStateUpdatedListener
import com.google.android.play.core.splitinstall.model.SplitInstallSessionStatus

class DynamicInstaller(
private val request: SplitInstallRequest,
private val manager: SplitInstallManager,
) : Installer {

private var mySessionId = 0

override fun install(callback: SoundInstalledCallback) {
val installListener = InstallListener(callback)
manager.registerListener(installListener)
manager.startInstall(request)
.addOnCompleteListener { }
.addOnSuccessListener { mySessionId = it }
.addOnFailureListener { callback.onError() }
}
}

class InstallListener(
private val callback: SoundInstalledCallback,
) : SplitInstallStateUpdatedListener {

override fun onStateUpdate(state: SplitInstallSessionState) {
when (state.status()) {
SplitInstallSessionStatus.DOWNLOADING -> {
val totalBytes = state.totalBytesToDownload()
val bytes = state.bytesDownloaded()
callback.onProgress(bytes, totalBytes)
}
SplitInstallSessionStatus.INSTALLED -> {

// After a module is installed, you can start accessing its content or
// fire an intent to start an activity in the installed module.
// For other use cases, see access code and resources from installed modules.

// If the request is an on demand module for an Android Instant App
// running on Android 8.0 (API level 26) or higher, you need to
// update the app context using the SplitInstallHelper API.
callback.onComplete()
}
SplitInstallSessionStatus.CANCELED,
SplitInstallSessionStatus.CANCELING,
SplitInstallSessionStatus.DOWNLOADED,
SplitInstallSessionStatus.FAILED,
SplitInstallSessionStatus.INSTALLING,
SplitInstallSessionStatus.PENDING,
SplitInstallSessionStatus.REQUIRES_USER_CONFIRMATION,
SplitInstallSessionStatus.UNKNOWN -> {
callback.onDebug(state.status())
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package nl.entreco.dartsscorecard.dynamic

interface Installer {
fun install(callback: SoundInstalledCallback)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package nl.entreco.dartsscorecard.dynamic

class NoInstaller : Installer {
override fun install(callback: SoundInstalledCallback) {}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,14 @@
package nl.entreco.dartsscorecard.dynamic

import com.google.android.play.core.internal.r
import com.google.android.play.core.splitinstall.SplitInstallManager
import com.google.android.play.core.splitinstall.SplitInstallRequest
import nl.entreco.domain.mastercaller.MusicRepository
import nl.entreco.liblog.Logger

class NoMusicRepository: MusicRepository {

/*************************************************************************
*
* ONWARD CONFIDENTIAL
* __________________
*
* [2021] ONWARD
* All Rights Reserved.
*
*/
class NoMusicRepository(
private val logger: Logger,
private val request: SplitInstallRequest,
private val manager: SplitInstallManager
) : MusicRepository {
override fun play() {}

override fun play() {
logger.i("Music play")
manager.startInstall(request)
.addOnCompleteListener{ logger.i("Music - installed") }
.addOnSuccessListener { logger.i("Music - loading") }
.addOnFailureListener { logger.i("Music - error: ${it.localizedMessage}") }
}
override fun pause() {}

override fun pause() {
logger.i("Music pause")
}
override fun resume() {}

override fun resume() {
logger.i("Music resume")
}

override fun stop() {
logger.i("Music stop")
}
override fun stop() {}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
package nl.entreco.dartsscorecard.dynamic

import com.google.android.play.core.splitinstall.SplitInstallManager
import com.google.android.play.core.splitinstall.SplitInstallRequest
import nl.entreco.domain.mastercaller.Sound
import nl.entreco.domain.mastercaller.SoundRepository
import nl.entreco.liblog.Logger

/*************************************************************************
*
* ONWARD CONFIDENTIAL
* __________________
*
* [2021] ONWARD
* All Rights Reserved.
*
*/
class NoSoundRepository(
private val logger: Logger,
private val request: SplitInstallRequest,
private val manager: SplitInstallManager
) : SoundRepository {
class NoSoundRepository : SoundRepository {

override fun play(sound: Sound) {
logger.i("Sound play: $sound")
manager.startInstall(request)
.addOnCompleteListener{ logger.i("Sound - installed") }
.addOnSuccessListener { logger.i("Sound - loading") }
.addOnFailureListener { logger.i("Sound - error: ${it.localizedMessage}") }
}
override fun play(sound: Sound) {}

override fun release() {
logger.i("Sound release")
}
override fun release() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package nl.entreco.dartsscorecard.dynamic

interface SoundInstalledCallback {
fun onComplete()
fun onProgress(bytes: Long, totalBytes: Long)
fun onError()
fun onDebug(status: Int)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ import nl.entreco.domain.mastercaller.MusicRepository
import nl.entreco.domain.mastercaller.SoundRepository
import nl.entreco.domain.repository.AudioPrefRepository

/*************************************************************************
*
* ONWARD CONFIDENTIAL
* __________________
*
* [2021] ONWARD
* All Rights Reserved.
*
*/
interface SoundModuleProvider {
fun provideSoundRepository(context: Context, @Play01Scope prefs: AudioPrefRepository): SoundRepository
fun provideMusicRepository(context: Context): MusicRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.graphics.Color
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import com.google.android.play.core.splitcompat.SplitCompat
import nl.entreco.dartsscorecard.R
import nl.entreco.dartsscorecard.base.ViewModelActivity
import nl.entreco.dartsscorecard.databinding.ActivityLaunchBinding
Expand Down
Loading

0 comments on commit e7c6982

Please sign in to comment.