Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
migrated the source download to use grpc
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Sep 16, 2023
1 parent cbe2dd3 commit 9ccfbaf
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 24 deletions.
14 changes: 14 additions & 0 deletions app/src/main/java/de/tum/in/tumcampusapp/api/app/BackendClient.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package de.tum.`in`.tumcampusapp.api.app

import com.google.protobuf.Empty
import de.tum.`in`.tumcampusapp.BuildConfig
import de.tum.`in`.tumcampusapp.api.backend.CampusGrpc
import de.tum.`in`.tumcampusapp.api.backend.GetUpdateNoteRequest
import de.tum.`in`.tumcampusapp.component.ui.news.model.NewsSources
import de.tum.`in`.tumcampusapp.component.ui.updatenote.model.UpdateNote
import io.grpc.ManagedChannelBuilder
import io.grpc.Metadata
Expand Down Expand Up @@ -44,6 +46,18 @@ class BackendClient private constructor() {
)
}

/**
* getNewsSources calls @callback with an UpdateNote currently in the api
* On error, errorCallback is called with an error message.
*/
fun getNewsSources(callback: (List<NewsSources>) -> Unit, errorCallback: (message: io.grpc.StatusRuntimeException) -> Unit) {
val response = stub.getNewsSources(Empty.getDefaultInstance())
response.runCatching { get() }.fold(
onSuccess = { callback(NewsSources.fromProto(it)) },
onFailure = { errorCallback(getStatus(it)) }
)
}

companion object {
@Volatile
private var instance: BackendClient? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,6 @@ Call<List<RoomFinderSchedule>> fetchSchedule(@Path("roomId") String archId,
@GET(API_NEWS + "{lastNewsId}")
Call<List<News>> getNews(@Path("lastNewsId") String lastNewsId);

@GET(API_NEWS + "sources")
Call<List<NewsSources>> getNewsSources();

@GET(API_NEWS + "alert")
Observable<NewsAlert> getNewsAlert();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,6 @@ public List<News> getNews(String lastNewsId) throws IOException {
.body();
}

public List<NewsSources> getNewsSources() throws IOException {
return service.getNewsSources()
.execute()
.body();
}

public Observable<NewsAlert> getNewsAlert() {
return service.getNewsAlert();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.tum.`in`.tumcampusapp.component.ui.news

import android.content.Context
import de.tum.`in`.tumcampusapp.api.app.BackendClient
import de.tum.`in`.tumcampusapp.api.app.TUMCabeClient
import de.tum.`in`.tumcampusapp.api.tumonline.CacheControl
import de.tum.`in`.tumcampusapp.component.notifications.NotificationScheduler
Expand Down Expand Up @@ -71,20 +72,13 @@ class NewsController @Inject constructor(
// Delete all too old items
newsDao.cleanUp()

val api = TUMCabeClient.getInstance(context)
val client = BackendClient.getInstance()
client.getNewsSources(
{ it.forEach { source -> newsSourcesDao.insert(source) } },
{ Utils.log(it) }
)

// Load all news sources
try {
val sources = api.newsSources
if (sources != null) {
newsSourcesDao.insert(sources)
}
} catch (e: IOException) {
Utils.log(e)
return
}

// Load all news since the last sync
val api = TUMCabeClient.getInstance(context)
try {
val news = api.getNews(getLastId())
if (news != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.RoomWarnings
import com.google.gson.annotations.SerializedName
import de.tum.`in`.tumcampusapp.api.backend.NewsSourceReply

/**
* This class contains information about the source of a [News] item.
Expand All @@ -26,4 +27,14 @@ data class NewsSources(

val isNewspread: Boolean
get() = setOf(7, 8, 9, 13).contains(id)

companion object {
fun fromProto(it: NewsSourceReply): List<NewsSources> {
return it.sourcesList.map { NewsSources(
id = it.source.toInt(),
title = it.title,
icon = it.icon
) }
}
}
}
4 changes: 2 additions & 2 deletions app/src/main/proto/backend.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import "google/protobuf/timestamp.proto";

service Campus {
rpc GetTopNews (google.protobuf.Empty) returns (GetTopNewsReply) { }
rpc GetNewsSources (google.protobuf.Empty) returns (NewsSourceArray) { }
rpc GetNewsSources (google.protobuf.Empty) returns (NewsSourceReply) { }
rpc GetUpdateNote (GetUpdateNoteRequest) returns (GetUpdateNoteReply) { }
}

message NewsSourceArray {
message NewsSourceReply {
repeated NewsSource sources = 1;
}

Expand Down

0 comments on commit 9ccfbaf

Please sign in to comment.