Skip to content

Commit

Permalink
renamed package
Browse files Browse the repository at this point in the history
changed return type for data from data store
  • Loading branch information
piyushpradeepkumar committed Dec 27, 2020
1 parent 91a8d79 commit d776ca9
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import androidx.datastore.preferences.core.preferencesKey
import androidx.datastore.preferences.createDataStore
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map

Expand All @@ -28,9 +28,7 @@ object DataStoreHelper {
)

activity.lifecycleScope.launchWhenCreated {
getHasCompletedTutorial()?.collect {
LevelInfo.hasPlayedTutorial = it
}
LevelInfo.hasPlayedTutorial = getHasCompletedTutorial()
}
}

Expand All @@ -40,10 +38,8 @@ object DataStoreHelper {
}
}

private fun getHasCompletedTutorial(): Flow<Boolean> {
return dataStore?.data?.map { preference ->
preference[HAS_COMPLETED_LEVEL_ZERO] ?: false
} ?: flowOf(false)
private suspend fun getHasCompletedTutorial(): Boolean {
return dataStore?.data?.first()?.get(HAS_COMPLETED_LEVEL_ZERO) ?: false
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import android.view.View
import android.widget.FrameLayout
import com.thelumierguy.astroadventures.utils.CustomLifeCycleOwner

open class BaseCustomView(context: Context, attributeSet: AttributeSet? = null) :
View(context, attributeSet) {
open class BaseCustomView(
context: Context,
attributeSet: AttributeSet? = null,
) : View(context, attributeSet) {

protected val lifeCycleOwner by lazy { CustomLifeCycleOwner() }

private var isActive = false
override fun onAttachedToWindow() {
super.onAttachedToWindow()
lifeCycleOwner.startListening()
Expand All @@ -20,13 +23,24 @@ open class BaseCustomView(context: Context, attributeSet: AttributeSet? = null)
super.onDetachedFromWindow()
lifeCycleOwner.stopListening()
}

override fun onVisibilityAggregated(isVisible: Boolean) {
isActive = isVisible
super.onVisibilityAggregated(isVisible)
}

fun executeIfActive(block: () -> Unit) {
if (isActive)
block()
}
}

open class BaseCustomViewGroup(context: Context, attributeSet: AttributeSet? = null) :
open class BaseFrameLayout(context: Context, attributeSet: AttributeSet? = null) :
FrameLayout(context, attributeSet) {

protected val lifeCycleOwner by lazy { CustomLifeCycleOwner() }

private var isActive = false
override fun onAttachedToWindow() {
super.onAttachedToWindow()
lifeCycleOwner.startListening()
Expand All @@ -36,4 +50,14 @@ open class BaseCustomViewGroup(context: Context, attributeSet: AttributeSet? = n
super.onDetachedFromWindow()
lifeCycleOwner.stopListening()
}

override fun onVisibilityAggregated(isVisible: Boolean) {
isActive = isVisible
super.onVisibilityAggregated(isVisible)
}

fun executeIfActive(block: () -> Unit) {
if (isActive)
block()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.core.content.res.ResourcesCompat
import com.thelumierguy.astroadventures.R
import com.thelumierguy.astroadventures.data.SoftBodyObject
import com.thelumierguy.astroadventures.data.SoftBodyObjectType
import com.thelumierguy.astroadventures.ui.base.BaseCustomView
import com.thelumierguy.astroadventures.utils.SoundManager
import com.thelumierguy.astroadventures.utils.forEachMutableSafe
import com.thelumierguy.astroadventures.utils.forEachSafe
import java.util.*

class BulletView(context: Context, attributeSet: AttributeSet? = null) :
BaseCustomView(context, attributeSet) {
View(context, attributeSet) {

private var fireSoundManager: SoundManager? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import androidx.core.content.res.ResourcesCompat
import com.thelumierguy.astroadventures.R
import com.thelumierguy.astroadventures.data.DropType
Expand All @@ -18,7 +19,7 @@ import java.util.*
import kotlin.random.Random

class DropsView(context: Context, attributeSet: AttributeSet? = null) :
BaseCustomView(context, attributeSet) {
View(context, attributeSet) {

private var fireSoundManager: SoundManager? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.channels.ticker
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch
import java.util.*


Expand Down Expand Up @@ -97,15 +96,17 @@ class EnemyClusterView(context: Context, attributeSet: AttributeSet? = null) :
*/
private fun startTranslating() {
translateJob.cancel()
translateJob = lifeCycleOwner.customViewLifeCycleScope.launch {
translateJob = lifeCycleOwner.customViewLifeCycleScope.launchWhenCreated {
enemyTimerFlow.collect {
enemyList.checkIfYReached(measuredHeight) { hasReachedMax ->
if (hasReachedMax) {
resetEnemies()
}
if (enemyList.isNotEmpty()) {
translateEnemy(System.currentTimeMillis())
invalidate()
executeIfActive {
enemyList.checkIfYReached(measuredHeight) { hasReachedMax ->
if (hasReachedMax) {
resetEnemies()
}
if (enemyList.isNotEmpty()) {
translateEnemy(System.currentTimeMillis())
invalidate()
}
}
}
}
Expand All @@ -116,13 +117,15 @@ class EnemyClusterView(context: Context, attributeSet: AttributeSet? = null) :
private fun fireCanon() {
if (shouldEmitObjects()) {
firingJob.cancel()
firingJob = lifeCycleOwner.customViewLifeCycleScope.launch {
firingJob = lifeCycleOwner.customViewLifeCycleScope.launchWhenCreated {
ticker(1000, 200).receiveAsFlow().collect {
if (enemyList.isNotEmpty()) {
val enemyList = enemyList.random()
val enemy = enemyList.enemyList.findLast { it.isVisible }
enemy?.let {
enemyDetailsCallback?.onCanonReady(enemy.enemyX, enemy.enemyY)
executeIfActive {
if (enemyList.isNotEmpty()) {
val enemyList = enemyList.random()
val enemy = enemyList.enemyList.findLast { it.isVisible }
enemy?.let {
enemyDetailsCallback?.onCanonReady(enemy.enemyX, enemy.enemyY)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import kotlin.math.roundToInt
import kotlin.math.sin


class PlayerHealthView constructor(context: Context, attributeSet: AttributeSet? = null) :
BaseCustomView(context, attributeSet) {
class PlayerHealthView constructor(
context: Context,
attributeSet: AttributeSet? = null,
) : BaseCustomView(context, attributeSet) {

private val heartPaint by lazy {
Paint().apply {
Expand Down Expand Up @@ -73,7 +75,7 @@ class PlayerHealthView constructor(context: Context, attributeSet: AttributeSet?


private fun startObservingHealth() {
lifeCycleOwner.customViewLifeCycleScope.launch {
lifeCycleOwner.customViewLifeCycleScope.launchWhenCreated {
PlayerHealthInfo.getPlayerHealthFlow().collect { life ->
launch {
if (life <= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.widget.FrameLayout
import androidx.core.content.res.ResourcesCompat
import com.thelumierguy.astroadventures.R
import com.thelumierguy.astroadventures.data.GlobalCounter
import com.thelumierguy.astroadventures.utils.CustomLifeCycleOwner
import com.thelumierguy.astroadventures.ui.base.BaseFrameLayout
import com.thelumierguy.astroadventures.utils.forEachSafe
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
Expand All @@ -18,12 +17,10 @@ import kotlin.random.Random


class StarsBackgroundView(context: Context, attributeSet: AttributeSet? = null) :
FrameLayout(context, attributeSet) {
BaseFrameLayout(context, attributeSet) {

private var enableWarp: Boolean = false

private val lifeCycleOwner by lazy { CustomLifeCycleOwner() }

override fun onAttachedToWindow() {
super.onAttachedToWindow()
lifeCycleOwner.startListening()
Expand Down Expand Up @@ -79,16 +76,18 @@ class StarsBackgroundView(context: Context, attributeSet: AttributeSet? = null)

private fun startObservingTimer() {
GlobalCounter.starsBackgroundTimerFlow.onEach {
if (enableWarp) {
trailsList.forEachSafe { trails, iterator ->
trails.translate()
}
} else {
starsList.forEachSafe { stars, iterator ->
stars.translate()
executeIfActive {
if (enableWarp) {
trailsList.forEachSafe { trails, iterator ->
trails.translate()
}
} else {
starsList.forEachSafe { stars, iterator ->
stars.translate()
}
}
invalidate()
}
invalidate()
}.launchIn(lifeCycleOwner.customViewLifeCycleScope)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thelumierguy.astroadventures.ui.base
package com.thelumierguy.astroadventures.ui.menu.views.scoreview

import android.animation.Animator
import android.animation.ValueAnimator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ class CustomLifeCycleOwner : LifecycleOwner {
init {
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
}


}
4 changes: 2 additions & 2 deletions app/src/main/res/layout/level_complete_scene.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
tools:text="200" />


<com.thelumierguy.astroadventures.ui.base.AnimatedTextView
<com.thelumierguy.astroadventures.ui.menu.views.scoreview.AnimatedTextView
android:id="@+id/bulletCountValueView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -135,7 +135,7 @@
tools:text="200" />


<com.thelumierguy.astroadventures.ui.base.AnimatedTextView
<com.thelumierguy.astroadventures.ui.menu.views.scoreview.AnimatedTextView
android:id="@+id/totalScoreValueView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/level_complete_scoreboard_scene.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
tools:text="200" />


<com.thelumierguy.astroadventures.ui.base.AnimatedTextView
<com.thelumierguy.astroadventures.ui.menu.views.scoreview.AnimatedTextView
android:id="@+id/bulletCountValueView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -135,7 +135,7 @@
tools:text="200" />


<com.thelumierguy.astroadventures.ui.base.AnimatedTextView
<com.thelumierguy.astroadventures.ui.menu.views.scoreview.AnimatedTextView
android:id="@+id/totalScoreValueView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
Binary file modified astro_adventures.apk
Binary file not shown.

0 comments on commit d776ca9

Please sign in to comment.