Skip to content

Commit

Permalink
I hate this kotlin error
Browse files Browse the repository at this point in the history
  • Loading branch information
ForceTower committed Oct 23, 2024
1 parent fcece7e commit a222c10
Show file tree
Hide file tree
Showing 18 changed files with 3,501 additions and 76 deletions.
3,301 changes: 3,301 additions & 0 deletions app/schemas/com.forcetower.uefs.core.storage.database.UDatabase/57.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ object FirestoreModule {
@Named(Discipline.COLLECTION)
fun provideDisciplineCollection(firestore: FirebaseFirestore): CollectionReference = firestore.collection(Discipline.COLLECTION)

@Provides
@Reusable
@Named(UMessage.COLLECTION)
fun provideUnesMessagesCollection(firestore: FirebaseFirestore): CollectionReference = firestore.collection(UMessage.COLLECTION)

@Provides
@Reusable
@Named(ClassStatsData.STATS_CONTRIBUTION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import timber.log.Timber
import java.time.OffsetDateTime

@Module
@InstallIn(SingletonComponent::class)
Expand Down Expand Up @@ -171,6 +172,7 @@ object NetworkModule {
fun provideGson(): Gson {
return GsonBuilder()
.registerTypeAdapter(ZonedDateTime::class.java, ObjectUtils.ZDT_DESERIALIZER)
.registerTypeAdapter(OffsetDateTime::class.java, ObjectUtils.ODT_DESERIALIZER)
.registerTypeAdapter(ZonedDateTime::class.java, ObjectUtils.ZDT_SERIALIZER)
.serializeNulls()
.create()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.forcetower.uefs.core.model.edge.sync

import com.google.gson.annotations.SerializedName
import java.time.OffsetDateTime

data class PublicAppMessage(
@SerializedName("id")
val id: String,
@SerializedName("title")
val title: String,
@SerializedName("content")
val content: String,
@SerializedName("imageUrl")
val imageUrl: String?,
@SerializedName("clickableLink")
val clickableLink: String?,
@SerializedName("createdAt")
val createdAt: OffsetDateTime
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,13 @@

package com.forcetower.uefs.core.model.service

import com.google.firebase.firestore.ServerTimestamp
import java.util.Date
import java.time.LocalDateTime

data class UMessage(
var id: String = "",
var title: String = "",
var message: String = "",
var image: String? = null,
@ServerTimestamp
var createdAt: Date? = null,
var link: String? = null,
var institution: String? = null
) {
companion object {
const val COLLECTION = "unes_messages"
}
}
val id: String,
val title: String,
val message: String,
val image: String?,
val createdAt: LocalDateTime,
val link: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.forcetower.uefs.core.model.unes

import androidx.room.Entity
import androidx.room.PrimaryKey
import java.time.ZonedDateTime

@Entity
data class EdgeAppMessage(
@PrimaryKey
val id: String,
val title: String,
val content: String,
val imageUrl: String?,
val clickableLink: String?,
val createdAt: ZonedDateTime
)
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import com.forcetower.uefs.core.model.unes.Contributor
import com.forcetower.uefs.core.model.unes.Course
import com.forcetower.uefs.core.model.unes.Discipline
import com.forcetower.uefs.core.model.unes.EdgeAccessToken
import com.forcetower.uefs.core.model.unes.EdgeAppMessage
import com.forcetower.uefs.core.model.unes.EdgeParadoxSearchableItem
import com.forcetower.uefs.core.model.unes.EdgeServiceAccount
import com.forcetower.uefs.core.model.unes.EvaluationEntity
Expand Down Expand Up @@ -83,6 +84,7 @@ import com.forcetower.uefs.core.storage.database.dao.DisciplineDao
import com.forcetower.uefs.core.storage.database.dao.DisciplineServiceDao
import com.forcetower.uefs.core.storage.database.dao.DocumentDao
import com.forcetower.uefs.core.storage.database.dao.EdgeAccessTokenDao
import com.forcetower.uefs.core.storage.database.dao.EdgeAppMessageDao
import com.forcetower.uefs.core.storage.database.dao.EdgeParadoxSearchableItemDao
import com.forcetower.uefs.core.storage.database.dao.EdgeServiceAccountDao
import com.forcetower.uefs.core.storage.database.dao.EvaluationEntitiesDao
Expand Down Expand Up @@ -146,14 +148,16 @@ import com.forcetower.uefs.core.util.Converters
ClassGroupTeacher::class,
EdgeAccessToken::class,
EdgeServiceAccount::class,
EdgeParadoxSearchableItem::class
EdgeParadoxSearchableItem::class,
EdgeAppMessage::class
],
version = 56,
version = 57,
exportSchema = true,
autoMigrations = [
AutoMigration(from = 53, to = 54),
AutoMigration(from = 54, to = 55),
AutoMigration(from = 55, to = 56)
AutoMigration(from = 55, to = 56),
AutoMigration(from = 56, to = 57),
]
)
@TypeConverters(value = [Converters::class])
Expand Down Expand Up @@ -198,4 +202,5 @@ abstract class UDatabase : RoomDatabase() {
abstract val edgeAccessToken: EdgeAccessTokenDao
abstract val edgeServiceAccount: EdgeServiceAccountDao
abstract val edgeParadoxSearchableItem: EdgeParadoxSearchableItemDao
abstract val edgeMessages: EdgeAppMessageDao
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.forcetower.uefs.core.storage.database.dao

import androidx.room.Dao
import androidx.room.Query
import com.forcetower.core.database.BaseDao
import com.forcetower.uefs.core.model.unes.EdgeAppMessage
import kotlinx.coroutines.flow.Flow

@Dao
abstract class EdgeAppMessageDao : BaseDao<EdgeAppMessage>() {
@Query("SELECT * FROM EdgeAppMessage ORDER BY createdAt DESC")
abstract fun getAll(): Flow<List<EdgeAppMessage>>

@Query("DELETE FROM EdgeAppMessage")
abstract suspend fun deleteAll()
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.forcetower.uefs.core.model.edge.auth.EmailLinkConfirmDTO
import com.forcetower.uefs.core.model.edge.auth.LinkEmailResponseDTO
import com.forcetower.uefs.core.model.edge.auth.RegisterPasskeyCredential
import com.forcetower.uefs.core.model.edge.auth.RegisterPasskeyStart
import com.forcetower.uefs.core.model.edge.sync.PublicAppMessage
import com.forcetower.uefs.core.model.edge.sync.PublicDisciplineData
import com.forcetower.uefs.core.model.edge.sync.PublicPlatformMessage
import retrofit2.Response
Expand Down Expand Up @@ -54,6 +55,9 @@ interface EdgeService {
@GET("messages/platform")
suspend fun messages(): ServiceResponseWrapper<List<PublicPlatformMessage>>

@GET("messages/app/public")
suspend fun appMessages(): ServiceResponseWrapper<List<PublicAppMessage>>

@GET("disciplines/current")
suspend fun disciplines(): ServiceResponseWrapper<PublicDisciplineData>
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.forcetower.sagres.SagresNavigator
import com.forcetower.sagres.database.model.SagresCredential
import com.forcetower.sagres.operation.Status
import com.forcetower.uefs.core.model.service.UMessage
import com.forcetower.uefs.core.model.unes.EdgeAppMessage
import com.forcetower.uefs.core.model.unes.Message
import com.forcetower.uefs.core.model.unes.defineInDatabase
import com.forcetower.uefs.core.storage.database.UDatabase
Expand All @@ -57,7 +58,6 @@ class MessagesRepository @Inject constructor(
private val client: OkHttpClient,
private val database: UDatabase,
private val cookieSessionRepository: CookieSessionRepository,
@Named(UMessage.COLLECTION) private val collection: CollectionReference,
@Named("flagSnowpiercerEnabled") private val snowpiercerEnabled: Boolean,
@Named("webViewUA") private val agent: String
) {
Expand Down Expand Up @@ -139,23 +139,7 @@ class MessagesRepository @Inject constructor(
}
}

fun getUnesMessages(): LiveData<List<UMessage>> {
val result = MutableLiveData<List<UMessage>>()
val institution = SagresNavigator.instance.getSelectedInstitution()
collection.orderBy("createdAt", Query.Direction.DESCENDING).addSnapshotListener { snapshot, exception ->
if (exception != null) {
Timber.e(exception)
} else if (snapshot != null) {
val list = snapshot.documents.mapNotNull {
it.toObject(UMessage::class.java)?.apply {
id = it.id
val replaced = message.replace("\\n", "\n")
message = replaced
}
}.filter { it.institution == null || it.institution == institution }
result.postValue(list)
}
}
return result
fun getUnesMessages(): Flow<List<EdgeAppMessage>> {
return database.edgeMessages.getAll()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.forcetower.uefs.core.storage.repository.cloud

import androidx.room.withTransaction
import com.forcetower.uefs.core.model.unes.EdgeAppMessage
import com.forcetower.uefs.core.storage.database.UDatabase
import com.forcetower.uefs.core.storage.network.EdgeService
import dagger.Reusable
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.withContext
import java.time.ZoneId
import javax.inject.Inject

@Reusable
class EdgeMessageRepository @Inject constructor(
private val service: EdgeService,
private val database: UDatabase
) {
suspend fun syncDataIfNeeded() {
database.edgeAccessToken.require() ?: return
messages()
}

fun getAll(): Flow<List<EdgeAppMessage>> {
return database.edgeMessages.getAll()
}

private suspend fun messages() {
val messages = service.appMessages().data
val converted = messages.map {
EdgeAppMessage(
it.id,
it.title,
it.content,
it.imageUrl,
it.clickableLink,
it.createdAt.atZoneSameInstant(ZoneId.systemDefault())
)
}
withContext(Dispatchers.IO) {
database.withTransaction {
database.edgeMessages.deleteAll()
database.edgeMessages.insertAllIgnore(converted)
}
}
}
}
16 changes: 15 additions & 1 deletion app/src/main/java/com/forcetower/uefs/core/util/ObjectUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.google.gson.JsonParseException
import com.google.gson.JsonPrimitive
import com.google.gson.JsonSerializer
import java.time.Instant
import java.time.OffsetDateTime
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
Expand All @@ -51,7 +52,7 @@ object ObjectUtils {
try {
val parser = DateTimeFormatter.ofPattern(pattern).withZone(ZoneId.of(BuildConfig.SIECOMP_TIMEZONE))
return@JsonDeserializer ZonedDateTime.parse(jsonPrimitive.asString, parser)
} catch (t: Throwable) { }
} catch (_: Throwable) { }
}
}

Expand All @@ -66,6 +67,19 @@ object ObjectUtils {
throw JsonParseException("Unable to parse ZonedDateTime")
}

@JvmStatic
val ODT_DESERIALIZER: JsonDeserializer<OffsetDateTime> = JsonDeserializer { json, _, _ ->
if (json.isJsonPrimitive && json.asJsonPrimitive.isString) {
return@JsonDeserializer runCatching {
OffsetDateTime.parse(json.asJsonPrimitive.asString)
}.getOrElse {
throw JsonParseException("Unable to parse OffsetDateTime", it)
}
}

throw JsonParseException("Unable to parse OffsetDateTime")
}

@SuppressLint("ConstantLocale")
val ZDT_SERIALIZER: JsonSerializer<ZonedDateTime> = JsonSerializer { src, _, _ ->
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
package com.forcetower.uefs.feature.messages

import android.view.View
import com.forcetower.uefs.core.model.service.UMessage
import com.forcetower.uefs.core.model.unes.EdgeAppMessage
import com.forcetower.uefs.core.model.unes.Message

interface MessagesActions {
fun onMessageClick(message: String?)
fun onMessageLongClick(view: View, message: Message?): Boolean
fun onUNESMessageLongClick(view: View, message: UMessage?): Boolean
fun onUNESMessageLongClick(view: View, message: EdgeAppMessage?): Boolean
fun onMessageShare(view: View, pos: Int)
fun onPortalMessageClick(message: Message?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import com.forcetower.uefs.feature.shared.extensions.capitalized
import com.forcetower.uefs.feature.shared.extensions.formatMonthYear
import com.forcetower.uefs.feature.shared.extensions.formatTimeWithoutSeconds
import java.text.SimpleDateFormat
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.util.Calendar
import java.util.Date
import java.util.Locale
Expand Down Expand Up @@ -127,6 +129,15 @@ fun dateNumberFromDate(tv: TextView, date: Date?) {
}
}

@BindingAdapter("dateNumberFromZonedDate")
fun dateNumberFromDate(tv: TextView, date: ZonedDateTime?) {
if (date == null) {
tv.text = "??"
} else {
tv.text = "${date.toLocalDateTime().dayOfMonth}"
}
}

@BindingAdapter("monthFromDate")
fun monthFromDate(tv: TextView, date: Date?) {
if (date == null) {
Expand All @@ -136,6 +147,15 @@ fun monthFromDate(tv: TextView, date: Date?) {
}
}

@BindingAdapter("monthFromZonedDate")
fun monthFromDate(tv: TextView, date: ZonedDateTime?) {
if (date == null) {
tv.text = "?? ????"
} else {
tv.text = DateTimeFormatter.ofPattern("MMMM yyyy").withLocale(Locale.getDefault()).format(date.toLocalDateTime())
}
}

@BindingAdapter("hourFromDate")
fun hourFromDate(tv: TextView, date: Date?) {
if (date == null) {
Expand All @@ -144,3 +164,12 @@ fun hourFromDate(tv: TextView, date: Date?) {
tv.text = date.time.formatTimeWithoutSeconds()
}
}

@BindingAdapter("hourFromZonedDate")
fun hourFromDate(tv: TextView, date: ZonedDateTime?) {
if (date == null) {
tv.text = "??:??"
} else {
tv.text = DateTimeFormatter.ofPattern("HH:mm").withLocale(Locale.getDefault()).format(date)
}
}
Loading

0 comments on commit a222c10

Please sign in to comment.