Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show new version of new release in ze drawer #357

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package de.berlindroid.zeapp.zeservices.github

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import retrofit2.http.GET
import retrofit2.http.Query

interface GitHubApi {

@Serializable
data class Contributor(
@SerialName(value = "login")
val login: String,

@SerialName(value = "contributions")
val contributions: Int,

@SerialName(value = "html_url")
val url: String,

@SerialName(value = "avatar_url")
val imageUrl: String,
)

@Serializable
data class Release(
@SerialName(value = "url")
val url: String,

@SerialName(value = "name")
val name: String,

@SerialName(value = "tag_name")
val tagName: String,
)

@GET("contributors")
suspend fun getContributors(@Query("page") page: Int): List<Contributor>

@GET("releases")
suspend fun getReleases(@Query("per_page") pageSize: Int = 30): List<Release>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package de.berlindroid.zeapp.zeservices.github

import de.berlindroid.zeapp.zeui.zeabout.Contributor
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import javax.inject.Inject

class ZeContributorsService @Inject constructor(
private val githubApi: GitHubApi,
) {
fun contributors(page: Int): Flow<List<Contributor>> = flow {
val contributors = githubApi.getContributors(page)

emit(
contributors.map { Contributor(it.login, it.url, it.imageUrl, it.contributions) },
)
}.flowOn(Dispatchers.IO)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package de.berlindroid.zeapp.zeservices.github

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import retrofit2.Retrofit
import retrofit2.converter.kotlinx.serialization.asConverterFactory

@Module
@InstallIn(SingletonComponent::class)
object ZeGitHubModule {

private val json = Json {
ignoreUnknownKeys = true
}

private val retrofit = Retrofit.Builder()
.baseUrl("https://api.github.com/repos/gdg-berlin-android/zebadge/")
.addConverterFactory(json.asConverterFactory("application/json".toMediaType()))
.build()

@Provides
fun gitHubApiService(): GitHubApi = retrofit.create(GitHubApi::class.java)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package de.berlindroid.zeapp.zeservices.github

import de.berlindroid.zeapp.BuildConfig
import okio.IOException
import retrofit2.HttpException
import timber.log.Timber
import javax.inject.Inject

class ZeReleaseService @Inject constructor(
private val githubApi: GitHubApi,
) {
suspend fun getLatestRelease() = try {
// We only need the latest release
githubApi.getReleases(pageSize = 1).firstOrNull()
} catch (ioException: IOException) {
Timber.w("Failed to get latest version", ioException)
null
} catch (httpException: HttpException) {
Timber.w("Failed to get latest version", httpException)
null
}

suspend fun getNewRelease(): Int? =
try {
getLatestRelease()?.tagName?.toInt()?.takeIf {
it > BuildConfig.VERSION_CODE
}
} catch (nfe: NumberFormatException) {
null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package de.berlindroid.zeapp.zeui.zeabout
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import de.berlindroid.zeapp.zeservices.ZeContributorsService
import de.berlindroid.zeapp.zeservices.github.ZeContributorsService
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import androidx.compose.material3.NavigationDrawerItem
import androidx.compose.material3.NavigationDrawerItemDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.vector.ImageVector
Expand All @@ -26,6 +28,7 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import de.berlindroid.zeapp.R
import de.berlindroid.zeapp.zeui.zetheme.ZeBlack
import de.berlindroid.zeapp.zeui.zetheme.ZeWhite
Expand All @@ -43,6 +46,9 @@ internal fun ZeDrawerContent(
onCloseDrawer: () -> Unit = {},
onTitleClick: () -> Unit = {},
) {
val viewModel: ZeDrawerViewModel = hiltViewModel()
val uiState by viewModel.uiState.collectAsState()

@Composable
fun NavDrawerItem(
text: String,
Expand Down Expand Up @@ -173,6 +179,14 @@ internal fun ZeDrawerContent(
)
}

uiState.newReleaseVersion?.let { version ->
item {
Text(
stringResource(id = R.string.ze_navdrawer_new_release, version),
modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),
)
}
}
item {
NavDrawerItem(
text = stringResource(id = R.string.ze_navdrawer_open_release_page),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package de.berlindroid.zeapp.zeui.zehome

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import de.berlindroid.zeapp.zeservices.github.ZeReleaseService
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class ZeDrawerViewModel @Inject constructor(
private val releaseService: ZeReleaseService,
) : ViewModel() {

private val _uiState = MutableStateFlow(UiState())
val uiState = _uiState.asStateFlow()

init {
checkForNewRelease()
}

private fun checkForNewRelease() {
viewModelScope.launch {
val newReleaseVersion = releaseService.getNewRelease()
_uiState.update {
it.copy(
newReleaseVersion = newReleaseVersion,
)
}
}
}

data class UiState(
val newReleaseVersion: Int? = null, // Version of a new release, in case there is one
)
}
1 change: 1 addition & 0 deletions zeapp/android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<string name="ze_navdrawer_save_all_pages">Save all pages to badge</string>
<string name="ze_navdrawer_update_config">Update config on badge</string>
<string name="ze_navdrawer_send_random_page">Send random page to badge</string>
<string name="ze_navdrawer_new_release">✨ New release available: %d</string>
<string name="ze_navdrawer_open_release_page">Open release page</string>
<string name="ze_navdrawer_contributors">Contributors</string>
<string name="ze_navdrawer_open_source">Open Source</string>
Expand Down