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

Commit

Permalink
feat:GetNews via grpc (#1620)
Browse files Browse the repository at this point in the history
* fixed not using the same proto files as the backend

* bumped a few proto versions to the same version the server uses (not for a reasons, just to have the same version)

* added the same lints the google example package uses

* fixed not usingg the correct api url

* added the ability to convert googles protobuf timestamps to datetime

* fixed the backend using anoterh package structure making importing different

* implemented the `GetNews` endpoint
  • Loading branch information
CommanderStorm authored Sep 20, 2023
1 parent cae2b86 commit 456f51e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 50 deletions.
15 changes: 15 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 app.tum.campus.api.CampusGrpc
import app.tum.campus.api.GetNewsRequest
import app.tum.campus.api.GetNewsSourcesRequest
import app.tum.campus.api.GetUpdateNoteRequest
import de.tum.`in`.tumcampusapp.BuildConfig
import de.tum.`in`.tumcampusapp.component.ui.news.model.News
import de.tum.`in`.tumcampusapp.component.ui.news.model.NewsSources
import de.tum.`in`.tumcampusapp.component.ui.updatenote.model.UpdateNote
import io.grpc.ManagedChannelBuilder
Expand Down Expand Up @@ -57,6 +59,19 @@ class BackendClient private constructor() {
)
}

/**
* getNewsSources calls @callback with an UpdateNote currently in the api
* On error, errorCallback is called with an error message.
*/
fun getNews(lastNewsID: Int, callback: (List<News>) -> Unit, errorCallback: (message: io.grpc.StatusRuntimeException) -> Unit) {
val request = GetNewsRequest.newBuilder().setLastNewsId(lastNewsID).build()
val response = stub.getNews(request)
response.runCatching { get() }.fold(
onSuccess = { callback(News.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 @@ -16,11 +16,7 @@
import de.tum.in.tumcampusapp.component.ui.barrierfree.model.BarrierFreeMoreInfo;
import de.tum.in.tumcampusapp.component.ui.cafeteria.model.Cafeteria;
import de.tum.in.tumcampusapp.component.ui.chat.model.ChatMember;
import de.tum.in.tumcampusapp.component.ui.chat.model.ChatMessage;
import de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoom;
import de.tum.in.tumcampusapp.component.ui.news.model.News;
import de.tum.in.tumcampusapp.component.ui.news.model.NewsAlert;
import de.tum.in.tumcampusapp.component.ui.news.model.NewsSources;
import de.tum.in.tumcampusapp.component.ui.openinghour.model.Location;
import de.tum.in.tumcampusapp.component.ui.studyroom.model.StudyRoomGroup;
import de.tum.in.tumcampusapp.component.ui.ticket.model.Event;
Expand All @@ -38,7 +34,6 @@
import retrofit2.http.GET;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Part;
import retrofit2.http.Path;

Expand All @@ -47,7 +42,6 @@
import static de.tum.in.tumcampusapp.api.app.TUMCabeClient.API_BARRIER_FREE_MORE_INFO;
import static de.tum.in.tumcampusapp.api.app.TUMCabeClient.API_CAFETERIAS;
import static de.tum.in.tumcampusapp.api.app.TUMCabeClient.API_CHAT_MEMBERS;
import static de.tum.in.tumcampusapp.api.app.TUMCabeClient.API_CHAT_ROOMS;
import static de.tum.in.tumcampusapp.api.app.TUMCabeClient.API_DEVICE;
import static de.tum.in.tumcampusapp.api.app.TUMCabeClient.API_EVENTS;
import static de.tum.in.tumcampusapp.api.app.TUMCabeClient.API_FEEDBACK;
Expand All @@ -57,10 +51,6 @@
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 @@ -116,9 +106,6 @@ public interface TUMCabeAPIService {
@GET(API_KINOS + "{lastId}")
Flowable<List<Kino>> getKinos(@Path("lastId") String lastId);

@GET(API_NEWS + "{lastNewsId}")
Call<List<News>> getNews(@Path("lastNewsId") String lastNewsId);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,6 @@ public Flowable<List<Kino>> getKinos(String lastId) {
return service.getKinos(lastId);
}

public List<News> getNews(String lastNewsId) throws IOException {
return service.getNews(lastNewsId)
.execute()
.body();
}

public Observable<NewsAlert> getNewsAlert() {
return service.getNewsAlert();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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
import de.tum.`in`.tumcampusapp.component.notifications.ProvidesNotifications
Expand All @@ -15,7 +14,6 @@ import de.tum.`in`.tumcampusapp.database.TcaDb
import de.tum.`in`.tumcampusapp.utils.Utils
import de.tum.`in`.tumcampusapp.utils.sync.SyncManager
import org.joda.time.DateTime
import java.io.IOException
import javax.inject.Inject

private const val TIME_TO_SYNC = 86400
Expand Down Expand Up @@ -69,26 +67,20 @@ class NewsController @Inject constructor(
val latestNews = newsDao.last
val latestNewsDate = latestNews?.date ?: DateTime.now()

// Delete all too old items
newsDao.cleanUp()

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

val api = TUMCabeClient.getInstance(context)
try {
val news = api.getNews(getLastId())
if (news != null) {
newsDao.insert(news)
}
showNewsNotification(news, latestNewsDate)
} catch (e: IOException) {
Utils.log(e)
return
}
client.getNews(
if (force === CacheControl.USE_CACHE) getLastIdOrZero() else 0,
{
newsDao.cleanUpOldNews()
it.forEach { news -> newsDao.insert(news) }
showNewsNotification(it, latestNewsDate)
},
{ Utils.log(it) }
)

// Finish sync
sync.replaceIntoDb(this)
Expand All @@ -108,10 +100,8 @@ class NewsController @Inject constructor(
val provider = NewsNotificationProvider(context, newNews)
val notification = provider.buildNotification()

if (notification != null) {
val scheduler = NotificationScheduler(context)
scheduler.schedule(notification)
}
val scheduler = NotificationScheduler(context)
scheduler.schedule(notification)
}

/**
Expand All @@ -129,8 +119,8 @@ class NewsController @Inject constructor(
return newsDao.getAll(ids.toTypedArray(), selectedNewspread)
}

private fun getLastId(): String {
return newsDao.last?.id ?: ""
private fun getLastIdOrZero(): Int {
return newsDao.last?.id?.toInt() ?: 0
}

fun setDismissed(id: String, d: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public interface NewsDao {

@Query("DELETE FROM news WHERE date < date('now','-3 month')")
void cleanUp();
void cleanUpOldNews();

@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(News news);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import android.net.Uri
import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.RoomWarnings
import com.google.gson.annotations.SerializedName
import app.tum.campus.api.GetNewsReply
import de.tum.`in`.tumcampusapp.component.ui.tufilm.KinoActivity
import de.tum.`in`.tumcampusapp.utils.Const
import de.tum.`in`.tumcampusapp.utils.DateTimeUtils
import de.tum.`in`.tumcampusapp.utils.toDateTime
import org.joda.time.DateTime

/**
Expand All @@ -26,7 +27,6 @@ import org.joda.time.DateTime
@SuppressWarnings(RoomWarnings.DEFAULT_CONSTRUCTOR)
data class News(
@PrimaryKey
@SerializedName("news")
var id: String = "",
var title: String = "",
var link: String = "",
Expand All @@ -52,4 +52,20 @@ data class News(
if (link.isBlank()) null else Intent(Intent.ACTION_VIEW, Uri.parse(link))
}
}

companion object {
fun fromProto(it: GetNewsReply): List<News> {
return it.newsList.map {
News(
id = it.id.toString(),
title = it.title,
link = it.link,
src = it.source,
image = it.imageUrl,
date = it.date.toDateTime(),
created = it.created.toDateTime()
)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package de.tum.in.tumcampusapp.database.dao;

import net.danlew.android.joda.JodaTimeAndroid;

import org.joda.time.DateTime;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -67,7 +65,7 @@ public void cleanUpOldTest() {

// before testing, make sure all items are there
assertThat(dao.getAll(new Integer[]{123}, 123)).hasSize(4);
dao.cleanUp();
dao.cleanUpOldNews();
assertThat(dao.getAll(new Integer[]{123}, 123)).hasSize(0);
}

Expand All @@ -85,7 +83,7 @@ public void cleanUpNothingTest() {

// before testing, make sure all items are there
assertThat(dao.getAll(new Integer[]{123}, 123)).hasSize(4);
dao.cleanUp();
dao.cleanUpOldNews();
assertThat(dao.getAll(new Integer[]{123}, 123)).hasSize(4);
}

Expand All @@ -103,7 +101,7 @@ public void cleanUpMixedTest() {

// before testing, make sure all items are there
assertThat(dao.getAll(new Integer[]{123}, 123)).hasSize(4);
dao.cleanUp();
dao.cleanUpOldNews();
assertThat(dao.getAll(new Integer[]{123}, 123)).hasSize(2);
}

Expand Down

0 comments on commit 456f51e

Please sign in to comment.