Skip to content

Instantly share code, notes, and snippets.

View marcelpinto's full-sized avatar

Marcel Pintó Biescas marcelpinto

View GitHub Profile
class NetworkFactoryTest {
private val baseUrl = "/"
private val testData = TestData("hello!")
private val testDataJson = "{\"name\":\"${testData.name}\"}"
private val mockWebServer = MockWebServer()
private val httpUrl = mockWebServer.url(baseUrl)
private val moshi = createMoshi()
@marcelpinto
marcelpinto / FirebaseLiveData.kt
Last active October 14, 2018 11:55
Example of Firebase LiveData mechanism
class FirebaseLiveData<T>(
private val reference: DatabaseReference,
private val resourceType: Class<T>
) : LiveData<Resource<T>>(), ValueEventListener {
override fun onActive() {
super.onActive()
if (value == null) {
value = Resource.loading()
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
homeRecycler.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
homeRecycler.adapter = adapter
homeViewModel.homePage.observe(this, Observer { resource ->
resource?.apply {
when (resource.status) {
SUCCESS -> {
data class HomePage(
val locationInfo: LocationInfo? = null,
val travelInfo: TravelInfo? = null,
val weddingLocation: WeddingLocation? = null,
val weddingInfo: WeddingInfo? = null,
val weddingDate: WeddingDate? = null,
val welcome: WelcomeItem? = null
)
@marcelpinto
marcelpinto / MyViewModel.kt
Created April 2, 2019 23:05
Gist for ViewData best practice blog
data class MyViewData(val text: String)
class MyViewModel(application: Application): AndroidViewModel(application) {
private val _myViewData = MutableLiveData<MyViewData>()
val myViewData: LiveData<MyViewData> = _myViewData
fun updateViewData() {
val value = getDistance()
@marcelpinto
marcelpinto / MyViewModel.kt
Created April 2, 2019 23:06
Gist for ViewData best practice blog - Good way
data class MyViewData(val value: Int, val textId: Int, val labelId: Int)
class MyViewModel: ViewModel() {
private val _myViewData = MutableLiveData<MyViewData>()
val myViewData: LiveData<MyViewData> = _myViewData
fun updateViewData() {
val value = getDistance()
@marcelpinto
marcelpinto / MyViewModel.kt
Created April 2, 2019 23:07
Gist for ViewData best practice blog - Best way
sealed class MyViewData {
abstract val distance: Int
data class Km(override val distance: Int): MyViewData()
data class Miles(override val distance: Int): MyViewData()
}
class MyViewModel: ViewModel() {
abstract class BaseSlice(val context: Context, val sliceUri: Uri) {
/**
* @return the slice implementation to be used by SliceProvider
*/
abstract fun buildSlice(): Slice
/**
* Call refresh to notify the SliceProvider to load again.
*/
import androidx.compose.Composable
import androidx.compose.unaryPlus
import androidx.ui.core.Text
import androidx.ui.core.dp
import androidx.ui.core.sp
import androidx.ui.foundation.shape.corner.RoundedCornerShape
import androidx.ui.graphics.Color
import androidx.ui.layout.*
import androidx.ui.material.MaterialTheme
import androidx.ui.material.surface.Card
@marcelpinto
marcelpinto / MyScreen.kt
Last active August 26, 2022 08:59
AppWidgetHost example
/**
* Copyright 2022 Google LLC.
* SPDX-License-Identifier: Apache-2.0
*/
@Composable
fun MyScreen(glanceAppWidget: GlanceAppWidget) {
val state = rememberAppWidgetHostState()
// the available size for the widget
val size = DpSize(200.dp, 200.dp)