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

👽 Implement Träwelling API changes #360

Merged
merged 1 commit into from
Jun 7, 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
7 changes: 5 additions & 2 deletions app/src/main/kotlin/de/hbch/traewelling/api/ApiService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,11 @@ interface CheckInService {
@Path("statusId") statusId: Int
): Call<Unit>

@GET("activeEvents")
fun getActiveEvents(): Call<Data<List<Event>>>
@GET("events")
suspend fun getEvents(
@Query("timestamp") timestamp: ZonedDateTime = ZonedDateTime.now(),
@Query("upcoming") upcoming: Boolean = false
): Data<List<Event>>
}

interface TravelService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ import androidx.compose.ui.text.font.FontWeight
import com.google.gson.annotations.SerializedName
import de.hbch.traewelling.R
import de.hbch.traewelling.api.models.event.Event
import de.hbch.traewelling.api.models.user.LightUser
import de.hbch.traewelling.util.extractUsernames
import java.time.ZonedDateTime

data class Status(
val id: Int,
val body: String?,
val createdAt: ZonedDateTime,
val profilePicture: String?,
@SerializedName("user") val userId: Int,
val username: String,
val visibility: StatusVisibility,
val business: StatusBusiness,
var likes: Int?,
Expand All @@ -25,12 +23,13 @@ data class Status(
@SerializedName("train") val journey: Journey,
val event: Event?,
val client: ApiClient?,
@SerializedName("bodyMentions") val mentions: List<UserMention>
@SerializedName("bodyMentions") val mentions: List<UserMention>,
@SerializedName("userDetails") val user: LightUser
) {
fun getStatusText(): String {
var statusBody = body ?: ""

if (username == "ErikUden") {
if (user.username == "ErikUden") {
statusBody += "\n🍑"
}

Expand Down
12 changes: 10 additions & 2 deletions app/src/main/kotlin/de/hbch/traewelling/api/models/user/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ data class User(
@SerializedName("displayName") val name: String,
@SerializedName("username") val username: String,
@SerializedName("profilePicture") val avatarUrl: String,
@SerializedName("trainDistance") val distance: Int,
@SerializedName("trainDuration") val duration: Int,
@SerializedName("totalDistance") val distance: Int,
@SerializedName("totalDuration") val duration: Int,
@SerializedName("points") val points: Int,
@SerializedName("mastodonUrl") val mastodonUrl: String?,
@SerializedName("privateProfile") val privateProfile: Boolean,
Expand All @@ -23,3 +23,11 @@ data class User(
) {
val averageSpeed: Double get() = (distance / 1000.0) / (duration / 60.0)
}

data class LightUser(
@SerializedName("id") val id: Int,
@SerializedName("displayName") val name: String,
@SerializedName("username") val username: String,
@SerializedName("profilePicture") val avatarUrl: String,
@SerializedName("mastodonUrl") val mastodonUrl: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ fun TraewelldroidNavHost(
statusLoaded = { status ->
val menuItems = mutableListOf<ComposeMenuItem>()
if (loggedInUserViewModel.loggedInUser.value != null) {
if (status.userId == loggedInUserViewModel.loggedInUser.value?.id) {
if (status.user.id == loggedInUserViewModel.loggedInUser.value?.id) {
menuItems.add(
ComposeMenuItem(
R.string.title_share,
Expand Down
31 changes: 11 additions & 20 deletions app/src/main/kotlin/de/hbch/traewelling/shared/EventViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,16 @@ class EventViewModel : ViewModel() {

val activeEvents: MutableLiveData<List<Event>> = MutableLiveData(listOf())

fun activeEvents() {
TraewellingApi
.checkInService
.getActiveEvents()
.enqueue(object : Callback<Data<List<Event>>> {
override fun onResponse(
call: Call<Data<List<Event>>>,
response: Response<Data<List<Event>>>
) {
if (response.isSuccessful) {
val data = response.body()?.data ?: return
activeEvents.postValue(data)
return
}
}

override fun onFailure(call: Call<Data<List<Event>>>, t: Throwable) {
Logger.captureException(t)
}
})
suspend fun activeEvents() {
activeEvents.postValue(
try {
TraewellingApi
.checkInService
.getEvents()
.data
} catch (_: Exception) {
listOf()
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,15 @@ private fun CoTraveller(
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
ProfilePicture(
name = status.username,
url = status.profilePicture ?: "",
name = status.user.username,
url = status.user.avatarUrl,
modifier = Modifier
.width(32.dp)
.height(32.dp)
)
Column {
Text(
text = "@${status.username}",
text = "@${status.user.username}",
fontWeight = FontWeight.ExtraBold
)
Text(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ fun CheckInCard(
modifier = Modifier.fillMaxWidth(),
status = status,
isOwnStatus =
(loggedInUserViewModel?.loggedInUser?.value?.id ?: -1) == status.userId,
(loggedInUserViewModel?.loggedInUser?.value?.id ?: -1) == status.user.id,
displayLongDate = displayLongDate,
checkInCardViewModel = checkInCardViewModel,
userSelected = userSelected,
Expand Down Expand Up @@ -503,20 +503,20 @@ private fun CheckInCardFooter(
else
getLocalTimeString(date = status.createdAt)
ProfilePicture(
name = status.username,
url = status.profilePicture ?: "",
name = status.user.username,
url = status.user.avatarUrl,
modifier = Modifier
.height(24.dp)
.width(24.dp)
.padding(end = 2.dp)
)
Text(
modifier = alignmentModifier
.clickable { userSelected(status.username) }
.clickable { userSelected(status.user.username) }
.padding(2.dp),
text = stringResource(
id = R.string.check_in_user_time,
status.username,
status.user.username,
dateString
),
textAlign = TextAlign.End,
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/kotlin/de/hbch/traewelling/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ import de.hbch.traewelling.util.publishStationShortcuts
import io.getunleash.UnleashClient
import io.getunleash.UnleashConfig
import io.getunleash.polling.PollingModes
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
Expand Down Expand Up @@ -121,7 +125,11 @@ class MainActivity : ComponentActivity()
emojiPackItemAdapter = EmojiPackItemAdapter.get(this)
TraewellingApi.jwt = secureStorage.getObject(SharedValues.SS_JWT, String::class.java)!!
SharedValues.TRAVELYNX_TOKEN = secureStorage.getObject(SharedValues.SS_TRAVELYNX_TOKEN, String::class.java) ?: ""
eventViewModel.activeEvents()

val coroutineScope = CoroutineScope(Dispatchers.IO)
coroutineScope.launch {
eventViewModel.activeEvents()
}

enableEdgeToEdge()
WindowCompat.setDecorFitsSystemWindows(window, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ fun StatusDetail(
StatusTags(
statusId = statusId,
modifier = Modifier.fillMaxWidth(),
isOwnStatus = (loggedInUserViewModel?.loggedInUser?.value?.id ?: -1) == status?.userId,
isOwnStatus = (loggedInUserViewModel?.loggedInUser?.value?.id ?: -1) == status?.user?.id,
defaultVisibility = loggedInUserViewModel?.defaultStatusVisibility ?: StatusVisibility.PUBLIC
)
status?.likes?.let {
Expand Down
Loading