diff --git a/app/src/main/java/com/mitchelbv/thuis_c/network/feyenoord/FeyenoordRetrofitHelper.kt b/app/src/main/java/com/mitchelbv/thuis_c/network/feyenoord/FeyenoordRetrofitHelper.kt index 2cac154..13fb1b1 100644 --- a/app/src/main/java/com/mitchelbv/thuis_c/network/feyenoord/FeyenoordRetrofitHelper.kt +++ b/app/src/main/java/com/mitchelbv/thuis_c/network/feyenoord/FeyenoordRetrofitHelper.kt @@ -6,10 +6,10 @@ import retrofit2.converter.gson.GsonConverterFactory object FeyenoordRetrofitHelper { val baseUrl = "https://tickets-api.feyenoord.nl/api/" - fun getInstance(): Retrofit { - return Retrofit.Builder() - .baseUrl(baseUrl) - .addConverterFactory(GsonConverterFactory.create()) - .build() - } + private val retrofit = Retrofit.Builder() + .baseUrl(baseUrl) + .addConverterFactory(GsonConverterFactory.create()) + .build() + + val feyenoord = retrofit.create(FeyenoordService::class.java) } \ No newline at end of file diff --git a/app/src/main/java/com/mitchelbv/thuis_c/ui/feyenoord/FeyenoordViewModel.kt b/app/src/main/java/com/mitchelbv/thuis_c/ui/feyenoord/FeyenoordViewModel.kt index 3940fbb..191c01d 100644 --- a/app/src/main/java/com/mitchelbv/thuis_c/ui/feyenoord/FeyenoordViewModel.kt +++ b/app/src/main/java/com/mitchelbv/thuis_c/ui/feyenoord/FeyenoordViewModel.kt @@ -32,9 +32,7 @@ class FeyenoordViewModel : ViewModel() { // TODO Get data from the internet val tempMatches = mutableListOf() viewModelScope.launch(Dispatchers.IO) { - val fApiClient = - FeyenoordRetrofitHelper.getInstance().create(FeyenoordService::class.java) - val apiResponse = fApiClient.getEvents() + val apiResponse = FeyenoordRetrofitHelper.feyenoord.getEvents() for (tabItem in apiResponse.TabItems) { val test = arrayOf(tabItem).filter { it.CategoryId == 2 } test.forEach { diff --git a/app/src/main/java/com/mitchelbv/thuis_c/ui/home/Home.kt b/app/src/main/java/com/mitchelbv/thuis_c/ui/home/Home.kt index 386ae87..fc4bdcf 100644 --- a/app/src/main/java/com/mitchelbv/thuis_c/ui/home/Home.kt +++ b/app/src/main/java/com/mitchelbv/thuis_c/ui/home/Home.kt @@ -13,6 +13,5 @@ import com.mitchelbv.thuis_c.ui.theme.Typography fun HomeScreen(navController: NavHostController) { Column(horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) { Text("De thuis app!", style = Typography.bodySmall) - } } \ No newline at end of file diff --git a/app/src/main/java/com/mitchelbv/thuis_c/ui/home/HomeViewModel.kt b/app/src/main/java/com/mitchelbv/thuis_c/ui/home/HomeViewModel.kt new file mode 100644 index 0000000..68da1da --- /dev/null +++ b/app/src/main/java/com/mitchelbv/thuis_c/ui/home/HomeViewModel.kt @@ -0,0 +1,49 @@ +package com.mitchelbv.thuis_c.ui.home + +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.mitchelbv.thuis_c.Screen +import com.mitchelbv.thuis_c.network.feyenoord.FeyenoordRetrofitHelper +import com.mitchelbv.thuis_c.network.feyenoord.FeyenoordService +import com.mitchelbv.thuis_c.ui.feyenoord.Match +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.launch + +data class HomeUiState( + val upcomingMatch: Match? = null +) + +class HomeViewModel : ViewModel() { + private val _uiState = MutableStateFlow(HomeUiState()) + val uiState: StateFlow = _uiState.asStateFlow() + + init { + getUpcomingMatch() + } + + fun notNullHandler(test: String?): String { + if (!test.isNullOrEmpty()) { + return test; + } + return "" + } + + private fun getUpcomingMatch() { + viewModelScope.launch { + val apiResponse = FeyenoordRetrofitHelper.feyenoord.getEvents().HeaderItem!! + val match = Match( + home_team = notNullHandler(apiResponse.NameHomeTeam), + away_team = notNullHandler(apiResponse.NameAwayTeam), + date = notNullHandler(apiResponse.EventStartDateTimeFormatted?.Full), + begin_time = notNullHandler(apiResponse.EventStartDateTimeFormatted?.Time), + end_time = notNullHandler(apiResponse.EventEndDateTimeFormatted?.Time), + away_image = notNullHandler(apiResponse.AwayImageUrl), + home_image = notNullHandler(apiResponse.HomeImageUrl) + ) + _uiState.value = HomeUiState(upcomingMatch = match) + } + } +} \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 114c58b..84f9815 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,7 +1,7 @@ Thuis - Feyenoord + Wedstrijden Recepten Home \ No newline at end of file