-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc42733
commit 4981152
Showing
11 changed files
with
172 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
data/src/main/kotlin/com/theapache64/stackzy/data/remote/FunFact.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
38 changes: 38 additions & 0 deletions
38
data/src/main/kotlin/com/theapache64/stackzy/data/repo/FunFactsRepo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/kotlin/com/theapache64/stackzy/ui/common/CenterBox.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/kotlin/com/theapache64/stackzy/ui/common/loading/funfact/FunFact.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
) | ||
) | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/kotlin/com/theapache64/stackzy/ui/common/loading/funfact/FunFactViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.theapache64.stackzy.ui.common.loading.funfact | ||
|
||
class FunFactViewModel { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters