diff --git a/app/build.gradle b/app/build.gradle index 3dfd0e5..fe99a3f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -62,11 +62,13 @@ dependencies { // Navigation implementation "androidx.navigation:navigation-compose:2.5.3" - // ViewModel - implementation "androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha07" + // ViewModel and livedata + implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1' + implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1' + implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1" + implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1" implementation 'androidx.core:core-ktx:1.7.0' - implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1' implementation 'androidx.activity:activity-compose:1.3.1' implementation "androidx.compose.ui:ui:$compose_version" implementation "androidx.compose.ui:ui-tooling-preview:$compose_version" diff --git a/app/src/main/java/com/mitchelbv/thuis_c/database/DatabaseEntities.kt b/app/src/main/java/com/mitchelbv/thuis_c/database/DatabaseEntities.kt new file mode 100644 index 0000000..4907e02 --- /dev/null +++ b/app/src/main/java/com/mitchelbv/thuis_c/database/DatabaseEntities.kt @@ -0,0 +1,32 @@ +package com.mitchelbv.thuis_c.database + +import androidx.room.Entity +import androidx.room.PrimaryKey +import com.mitchelbv.thuis_c.ui.feyenoord.Match + +@Entity +data class DatabaseMatches constructor( + @PrimaryKey(autoGenerate = true) + val id: Int, + val home_team: String, + val away_team: String, + val date: String, + val begin_time: String, + val end_time: String, + val home_image: String, + val away_image: String +) + +fun List.asDomainModel(): List { + return map { + Match( + home_team = it.home_team, + away_team = it.away_team, + date = it.date, + begin_time = it.begin_time, + end_time = it.end_time, + home_image = it.home_image, + away_image = it.away_image + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/mitchelbv/thuis_c/database/Room.kt b/app/src/main/java/com/mitchelbv/thuis_c/database/Room.kt new file mode 100644 index 0000000..c013c04 --- /dev/null +++ b/app/src/main/java/com/mitchelbv/thuis_c/database/Room.kt @@ -0,0 +1,42 @@ +package com.mitchelbv.thuis_c.database + +import android.content.Context +import androidx.lifecycle.LiveData +import androidx.room.* +import kotlinx.coroutines.withContext + +@Dao +interface MatchDao { + @Query("select * from databasematches") + fun getMatches(): LiveData> + + @Query("select * from databasematches order by id ASC") + fun getUpcomingMatch(): LiveData + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insertAll(matches: List) +} + +@Database(entities = [DatabaseMatches::class], version = 1) +abstract class MatchesDatabase : RoomDatabase() { + abstract val matchDao: MatchDao + + companion object { + @Volatile + private lateinit var INSTANCE: MatchesDatabase + + fun getDatabase(context: Context): MatchesDatabase { + synchronized(MatchesDatabase::class.java) { + if (!::INSTANCE.isInitialized) { + INSTANCE = Room.databaseBuilder( + context.applicationContext, + MatchesDatabase::class.java, + "matches" + ).build() + } + } + return INSTANCE + } + } +} + 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/network/feyenoord/FeyenoordService.kt b/app/src/main/java/com/mitchelbv/thuis_c/network/feyenoord/FeyenoordService.kt index c3c92cf..71a77da 100644 --- a/app/src/main/java/com/mitchelbv/thuis_c/network/feyenoord/FeyenoordService.kt +++ b/app/src/main/java/com/mitchelbv/thuis_c/network/feyenoord/FeyenoordService.kt @@ -1,7 +1,6 @@ package com.mitchelbv.thuis_c.network.feyenoord import com.mitchelbv.thuis_c.network.feyenoord.responses.FeyenoordEventResponse -import retrofit2.Call import retrofit2.http.GET interface FeyenoordService { diff --git a/app/src/main/java/com/mitchelbv/thuis_c/network/feyenoord/responses/FeyenoordEventResponse.kt b/app/src/main/java/com/mitchelbv/thuis_c/network/feyenoord/responses/FeyenoordEventResponse.kt index 2f47f26..e873d12 100644 --- a/app/src/main/java/com/mitchelbv/thuis_c/network/feyenoord/responses/FeyenoordEventResponse.kt +++ b/app/src/main/java/com/mitchelbv/thuis_c/network/feyenoord/responses/FeyenoordEventResponse.kt @@ -1,12 +1,62 @@ package com.mitchelbv.thuis_c.network.feyenoord.responses import com.google.gson.annotations.SerializedName +import com.mitchelbv.thuis_c.database.DatabaseMatches +import com.mitchelbv.thuis_c.ui.feyenoord.Match data class FeyenoordEventResponse( - @SerializedName("HeaderItem") var HeaderItem: HeaderItem? = HeaderItem(), @SerializedName("TabItems") var TabItems: ArrayList = arrayListOf(), @SerializedName("CrossSellInfo") var CrossSellInfo: String? = null +) -) \ No newline at end of file +fun notNullHandler(test: String?): String { + if (!test.isNullOrEmpty()) { + return test; + } + return "" +} + +fun FeyenoordEventResponse.asDomainModel(): List { + val tempMatches = mutableListOf() + val test2 = TabItems.filter { it.CategoryId == 2 } + test2.forEach { items -> + for (item in items.Items) { + tempMatches.add( + Match( + home_team = notNullHandler(item.NameHomeTeam), + away_team = notNullHandler(item.NameAwayTeam), + date = notNullHandler(item.EventStartDateTimeFormatted?.Full), + begin_time = notNullHandler(item.EventStartDateTimeFormatted?.Time), + end_time = notNullHandler(item.EventEndDateTimeFormatted?.Time), + away_image = notNullHandler(item.AwayImageUrl), + home_image = notNullHandler(item.HomeImageUrl) + ) + ) + } + } + return tempMatches +} + +fun FeyenoordEventResponse.asDatabaseModel(): List { + val tempMatches = mutableListOf() + val test2 = TabItems.filter { it.CategoryId == 2 } + test2.forEach { items -> + for (item in items.Items) { + tempMatches.add( + DatabaseMatches( + id = item.EventId!!, + home_team = notNullHandler(item.NameHomeTeam), + away_team = notNullHandler(item.NameAwayTeam), + date = notNullHandler(item.EventStartDateTimeFormatted?.Full), + begin_time = notNullHandler(item.EventStartDateTimeFormatted?.Time), + end_time = notNullHandler(item.EventEndDateTimeFormatted?.Time), + away_image = notNullHandler(item.AwayImageUrl), + home_image = notNullHandler(item.HomeImageUrl) + ) + ) + } + } + return tempMatches +} \ No newline at end of file diff --git a/app/src/main/java/com/mitchelbv/thuis_c/repository/FeyenoordRepository.kt b/app/src/main/java/com/mitchelbv/thuis_c/repository/FeyenoordRepository.kt new file mode 100644 index 0000000..a825e57 --- /dev/null +++ b/app/src/main/java/com/mitchelbv/thuis_c/repository/FeyenoordRepository.kt @@ -0,0 +1,25 @@ +package com.mitchelbv.thuis_c.repository + +import androidx.lifecycle.LiveData +import androidx.lifecycle.Transformations +import com.mitchelbv.thuis_c.database.MatchesDatabase +import com.mitchelbv.thuis_c.database.asDomainModel +import com.mitchelbv.thuis_c.network.feyenoord.FeyenoordRetrofitHelper +import com.mitchelbv.thuis_c.network.feyenoord.responses.asDatabaseModel +import com.mitchelbv.thuis_c.ui.feyenoord.Match +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext + + +class FeyenoordRepository(private val database: MatchesDatabase) { + val matches: LiveData> = Transformations.map(database.matchDao.getMatches()) { + it.asDomainModel() + } + + suspend fun refreshMatches() { + withContext(Dispatchers.IO) { + val matches = FeyenoordRetrofitHelper.feyenoord.getEvents() + database.matchDao.insertAll(matches.asDatabaseModel()) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/mitchelbv/thuis_c/ui/feyenoord/FeyenoordView.kt b/app/src/main/java/com/mitchelbv/thuis_c/ui/feyenoord/FeyenoordView.kt index 88b550b..22caaa5 100644 --- a/app/src/main/java/com/mitchelbv/thuis_c/ui/feyenoord/FeyenoordView.kt +++ b/app/src/main/java/com/mitchelbv/thuis_c/ui/feyenoord/FeyenoordView.kt @@ -42,7 +42,9 @@ fun MatchList(matches: List, modifier: Modifier) { @Composable fun TeamNameWithLogo(team_name: String, team_logo_url: String, modifier: Modifier) { - Column(modifier = modifier.size(128.dp).fillMaxWidth(), Arrangement.Center, Alignment.CenterHorizontally) { + Column(modifier = modifier + .size(128.dp) + .fillMaxWidth(), Arrangement.Center, Alignment.CenterHorizontally) { AsyncImage( model = team_logo_url, contentDescription = team_name, @@ -50,7 +52,9 @@ fun TeamNameWithLogo(team_name: String, team_logo_url: String, modifier: Modifie contentScale = ContentScale.Fit, modifier = modifier.size(64.dp) ) - Text(text = team_name, modifier = modifier.fillMaxWidth().wrapContentWidth(Alignment.CenterHorizontally)) + Text(text = team_name, modifier = modifier + .fillMaxWidth() + .wrapContentWidth(Alignment.CenterHorizontally)) } } 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..2743106 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 @@ -1,57 +1,61 @@ package com.mitchelbv.thuis_c.ui.feyenoord -import android.util.Log +import android.app.Application import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.mitchelbv.thuis_c.network.feyenoord.FeyenoordRetrofitHelper -import com.mitchelbv.thuis_c.network.feyenoord.FeyenoordService -import com.mitchelbv.thuis_c.network.feyenoord.responses.FeyenoordEventResponse -import kotlinx.coroutines.Dispatchers +import com.mitchelbv.thuis_c.database.MatchesDatabase.Companion.getDatabase +import com.mitchelbv.thuis_c.repository.FeyenoordRepository import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch -import okhttp3.ResponseBody -import retrofit2.Call -import retrofit2.Callback -import retrofit2.Response data class FeyenoordUiState( val matches: List = listOf() ) -class FeyenoordViewModel : ViewModel() { +class FeyenoordViewModel(application: Application) : ViewModel() { private val _uiState = MutableStateFlow(FeyenoordUiState()) val uiState: StateFlow = _uiState.asStateFlow() + private val feyenoordRepository = FeyenoordRepository(getDatabase(application)) + val matches = feyenoordRepository.matches + init { - fillMatches() + refreshMatchesFromRepository() } - private fun fillMatches() { - // 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() - for (tabItem in apiResponse.TabItems) { - val test = arrayOf(tabItem).filter { it.CategoryId == 2 } - test.forEach { - for (item in it.Items) { - tempMatches.add(Match( - home_team = item.NameHomeTeam!!, - away_team = item.NameAwayTeam!!, - date = item.EventStartDateTimeFormatted?.Full!!, - begin_time = item.EventStartDateTimeFormatted?.Time!!, - end_time = item.EventEndDateTimeFormatted?.Time!!, - away_image = item.AwayImageUrl!!, - home_image = item.HomeImageUrl!! - )) - } - } - } - _uiState.value = FeyenoordUiState(matches = tempMatches) + private fun refreshMatchesFromRepository() { + viewModelScope.launch { + feyenoordRepository.refreshMatches() + _uiState.value = FeyenoordUiState(matches = matches.value!!) } } + +// private fun fillMatches() { +// // 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() +// for (tabItem in apiResponse.TabItems) { +// val test = arrayOf(tabItem).filter { it.CategoryId == 2 } +// test.forEach { +// for (item in it.Items) { +// tempMatches.add(Match( +// home_team = item.NameHomeTeam!!, +// away_team = item.NameAwayTeam!!, +// date = item.EventStartDateTimeFormatted?.Full!!, +// begin_time = item.EventStartDateTimeFormatted?.Time!!, +// end_time = item.EventEndDateTimeFormatted?.Time!!, +// away_image = item.AwayImageUrl!!, +// home_image = item.HomeImageUrl!! +// )) +// } +// } +// } +// _uiState.value = FeyenoordUiState(matches = tempMatches) +// } +// } } \ No newline at end of file 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