generated from greyhairredbear/android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Peter Laggner
committed
Oct 12, 2021
1 parent
c8187a0
commit 659985e
Showing
10 changed files
with
114 additions
and
93 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
apiclient/src/main/java/com/greyhairredbear/racooin/apiclient/CoingeckoApiClient.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
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
33 changes: 33 additions & 0 deletions
33
app/src/main/java/com/greyhairredbear/racooin/ui/MainScreen.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,33 @@ | ||
package com.greyhairredbear.racooin.ui | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
import com.greyhairredbear.racooin.core.interfaces.Resource | ||
import com.greyhairredbear.racooin.core.model.CryptoCurrencyRate | ||
|
||
@Composable | ||
fun MainScreen( | ||
screenViewModel: MainViewModel = viewModel() | ||
) { | ||
val uiState by screenViewModel.uiState.collectAsState() | ||
Column(horizontalAlignment = Alignment.CenterHorizontally) { | ||
when (uiState) { | ||
is Resource.Success -> { | ||
(uiState as Resource.Success).data.forEach { | ||
Text(text = "1 ${it.cryptoCurrency.name} = ${it.fiatBalance.balance} ${it.fiatBalance.fiatCurrency}") | ||
} | ||
} | ||
is Resource.Error -> { | ||
Text(text = "Error") | ||
} | ||
is Resource.Loading -> { | ||
Text(text = "Loading") | ||
} | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
app/src/main/java/com/greyhairredbear/racooin/ui/MainViewModel.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,40 @@ | ||
package com.greyhairredbear.racooin.ui | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import arrow.core.computations.either | ||
import com.greyhairredbear.racooin.core.interfaces.ApiClient | ||
import com.greyhairredbear.racooin.core.interfaces.Persistence | ||
import com.greyhairredbear.racooin.core.interfaces.Resource | ||
import com.greyhairredbear.racooin.core.model.ApiClientError | ||
import com.greyhairredbear.racooin.core.model.CryptoCurrencyRate | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
|
||
@HiltViewModel | ||
class MainViewModel @Inject constructor( | ||
private val apiClient: ApiClient, | ||
private val persistence: Persistence, | ||
) : ViewModel() { | ||
private val _uiState = MutableStateFlow<Resource<List<CryptoCurrencyRate>>>(Resource.Loading) | ||
val uiState: StateFlow<Resource<List<CryptoCurrencyRate>>> = _uiState | ||
|
||
init { | ||
viewModelScope.launch(Dispatchers.IO) { | ||
val test = either<ApiClientError, List<CryptoCurrencyRate>> { | ||
val currencyRateResult = apiClient.fetchCurrencyRates().bind() | ||
currencyRateResult | ||
} | ||
|
||
test.fold( | ||
ifRight = { _uiState.value = Resource.Success(it) }, | ||
ifLeft = { _uiState.value = Resource.Error("failed api call") }, | ||
) | ||
} | ||
} | ||
} |
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
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