Skip to content

Commit

Permalink
➕ add funfact data layer
Browse files Browse the repository at this point in the history
  • Loading branch information
theapache64 committed Oct 22, 2021
1 parent fc42733 commit 4981152
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,8 @@ interface ApiInterface {
@Query("except_v_code") exceptVersionCode : Int
): Flow<Resource<Result>>

@Read("SELECT *")
@GET(NetworkModule.TABLE_FUN_FACTS)
fun getFunFacts() : Flow<Resource<List<FunFact>>>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.theapache64.stackzy.data.remote
import com.squareup.moshi.JsonClass

import com.squareup.moshi.Json

@JsonClass(generateAdapter = true)
data class FunFact(
@Json(name = "id")
val id: Int, // 1
@Json(name = "fun_fact")
val funFact: String // lorem ipsum
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.theapache64.stackzy.data.repo

import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import com.theapache64.stackzy.data.remote.ApiInterface
import com.theapache64.stackzy.data.remote.FunFact
import java.util.prefs.Preferences
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
open class FunFactsRepo @Inject constructor(
private val apiInterface: ApiInterface,
private val moshi: Moshi,
private val pref: Preferences
) {
companion object {
private const val KEY_FUN_FACTS = "FunFacts"
}

private val funFactsJsonAdapter by lazy {
val listMyData = Types.newParameterizedType(List::class.java, FunFact::class.java)
moshi.adapter<List<FunFact>>(listMyData)
}

fun getRemoteFunFacts() =
apiInterface.getFunFacts()

fun getLocalFunFacts(): Set<FunFact>? {
val funFactsJson = pref.get(KEY_FUN_FACTS, null)
return funFactsJsonAdapter.fromJson(funFactsJson)?.toSet()
}

fun saveFunFactsToLocal(data: List<FunFact>) {
pref.put(KEY_FUN_FACTS, funFactsJsonAdapter.toJson(data))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class NetworkModule {
const val TABLE_UNTRACKED_LIBS = "untracked_libs"
const val TABLE_RESULTS = "results"
const val TABLE_CONFIG = "config"
const val TABLE_FUN_FACTS = "fun_facts"
}

@Singleton
Expand All @@ -34,6 +35,10 @@ class NetworkModule {
sheetName = TABLE_CATEGORIES,
"id", "name"
)
.addSheet(
sheetName = TABLE_FUN_FACTS,
"id", "fun_fact"
)
.addSheet(
sheetName = TABLE_LIBRARIES,
"id", "name", "package_name", "category", "website"
Expand Down
23 changes: 23 additions & 0 deletions src/main/kotlin/com/theapache64/stackzy/ui/common/CenterBox.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.theapache64.stackzy.ui.common

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier

/**
* Only used for debugging/preview purpose
*/
@Composable
fun CenterBox(
content: @Composable BoxScope.() -> Unit
) {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
content()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.theapache64.stackzy.data.repo.ConfigRepo

/**
* To show a rotating icon at the center and blinking text at the bottom of the screen
Expand Down Expand Up @@ -41,15 +44,20 @@ fun LoadingAnimation(message: String) {
Box(
modifier = Modifier.fillMaxSize()
) {
Image(
modifier = Modifier
.rotate(animatedRotation)
.align(Alignment.Center)
.size(50.dp),
colorFilter = ColorFilter.tint(MaterialTheme.colors.primary),
painter = painterResource("drawables/loading.png"),
contentDescription = ""
)
Column(
modifier = Modifier.align(Alignment.Center)
) {
Image(
modifier = Modifier
.rotate(animatedRotation)
.size(50.dp),
colorFilter = ColorFilter.tint(MaterialTheme.colors.primary),
painter = painterResource("drawables/loading.png"),
contentDescription = ""
)


}

LoadingText(
modifier = Modifier.align(Alignment.BottomCenter),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.theapache64.stackzy.ui.common.loading.funfact

import androidx.compose.foundation.clickable
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.window.singleWindowApplication
import com.theapache64.stackzy.data.remote.FunFact
import com.theapache64.stackzy.ui.common.CenterBox
import com.theapache64.stackzy.util.PureRandom


@Composable
fun FunFact(funFacts: Set<FunFact>) {
val pureRandom = remember { PureRandom(funFacts) }
var currentFunFact by remember { mutableStateOf(pureRandom.get()) }

Text(
text = currentFunFact.funFact,
modifier = Modifier.clickable {
currentFunFact = pureRandom.get()
}
)
}


fun main(args: Array<String>) = singleWindowApplication {
CenterBox {
FunFact(
setOf(
FunFact(id = 1, "A"),
FunFact(id = 2, "B"),
FunFact(id = 3, "C"),
FunFact(id = 4, "D"),
FunFact(id = 5, "E"),
)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.theapache64.stackzy.ui.common.loading.funfact

class FunFactViewModel {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.theapache64.stackzy.App
import com.theapache64.stackzy.data.remote.Config
import com.theapache64.stackzy.data.repo.AdbRepo
import com.theapache64.stackzy.data.repo.ConfigRepo
import com.theapache64.stackzy.data.repo.FunFactsRepo
import com.theapache64.stackzy.data.repo.LibrariesRepo
import com.theapache64.stackzy.data.util.calladapter.flow.Resource
import com.toxicbakery.logging.Arbor
Expand All @@ -21,7 +22,8 @@ import javax.inject.Inject
class SplashViewModel @Inject constructor(
private val librariesRepo: LibrariesRepo,
private val adbRepo: AdbRepo,
private val configRepo: ConfigRepo
private val configRepo: ConfigRepo,
private val funFactsRepo: FunFactsRepo
) {

private lateinit var viewModelScope: CoroutineScope
Expand All @@ -47,9 +49,11 @@ class SplashViewModel @Inject constructor(
try {
syncAndCacheLibraries { // first cache libs
syncAndStoreConfig { // then sync config
checkAndFixAdb { // then check adb
checkJdk {
_isSyncFinished.value = true // all done
syncAndStoreFunFacts {
checkAndFixAdb { // then check adb
checkJdk {
_isSyncFinished.value = true // all done
}
}
}
}
Expand All @@ -70,6 +74,25 @@ class SplashViewModel @Inject constructor(
}
}

private suspend fun syncAndStoreFunFacts(
onSynced: suspend () -> Unit
) {
funFactsRepo.getRemoteFunFacts().collect { resource ->
when (resource) {
is Resource.Loading -> {
_syncMsg.value = "Getting some fun facts for you..."
}
is Resource.Success -> {
val funFacts = resource.data
funFactsRepo.saveFunFactsToLocal(funFacts)
onSynced()
}
is Resource.Error -> {
_syncFailedMsg.value = resource.errorData
}
}
}
}

private suspend fun syncAndCacheLibraries(
onSuccess: suspend () -> Unit
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/theapache64/stackzy/util/ColorUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ object ColorUtil {
private val pureRandom = PureRandom(colorSet)

fun getRandomColor(): Color {
return pureRandom.take()
return pureRandom.get()
}

}
4 changes: 2 additions & 2 deletions src/main/kotlin/com/theapache64/stackzy/util/PureRandom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class PureRandom<T>(
) {
private val takenItems = mutableSetOf<T>()

fun take(): T {
fun get(): T {

if (takenItems.size >= items.size) {
// all items taken so clear the list
Expand All @@ -18,7 +18,7 @@ class PureRandom<T>(
val takenItem = items.random()
if (takenItems.contains(takenItem)) {
// already taken
return take()
return get()
}
takenItems.add(takenItem)
return takenItem
Expand Down

0 comments on commit 4981152

Please sign in to comment.