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

Commit

Permalink
feat:grpc newssources (#1610)
Browse files Browse the repository at this point in the history
* migrated updatenote to grpc

* refactored the singelton

* formatting fix

* Formatting fix

* removed unnessesary doAsync usage

* migrated the source download to use grpc

* fixed linting newlines

---------

Co-authored-by: Tobias Jungmann <[email protected]>
Co-authored-by: Tobias Jungmann <[email protected]>
  • Loading branch information
3 people authored Sep 18, 2023
1 parent 598fd74 commit 387078c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
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 +45,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 @@ -57,6 +57,10 @@
import static de.tum.in.tumcampusapp.api.app.TUMCabeClient.API_NEWS;
import static de.tum.in.tumcampusapp.api.app.TUMCabeClient.API_NOTIFICATIONS;
import static de.tum.in.tumcampusapp.api.app.TUMCabeClient.API_OPENING_HOURS;
import static de.tum.in.tumcampusapp.api.app.TUMCabeClient.API_ROOM_FINDER;
import static de.tum.in.tumcampusapp.api.app.TUMCabeClient.API_ROOM_FINDER_AVAILABLE_MAPS;
import static de.tum.in.tumcampusapp.api.app.TUMCabeClient.API_ROOM_FINDER_COORDINATES;
import static de.tum.in.tumcampusapp.api.app.TUMCabeClient.API_ROOM_FINDER_SCHEDULE;
import static de.tum.in.tumcampusapp.api.app.TUMCabeClient.API_STUDY_ROOMS;
import static de.tum.in.tumcampusapp.api.app.TUMCabeClient.API_TICKET;

Expand Down Expand Up @@ -115,9 +119,6 @@ public interface TUMCabeAPIService {
@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 @@ -223,12 +223,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,16 @@ 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 387078c

Please sign in to comment.