Merge pull request 'feat/server-thresholds' (#78) from feat/server-thresholds into master
Reviewed-on: #78
This commit was merged in pull request #78.
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -8,3 +8,5 @@ android/build/
|
|||||||
android/app/build/
|
android/app/build/
|
||||||
android/local.properties
|
android/local.properties
|
||||||
config/firebase-service-account.json
|
config/firebase-service-account.json
|
||||||
|
.*
|
||||||
|
!.gitignore
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ android {
|
|||||||
applicationId = "com.devlab.app"
|
applicationId = "com.devlab.app"
|
||||||
minSdk = 26
|
minSdk = 26
|
||||||
targetSdk = 37
|
targetSdk = 37
|
||||||
versionCode = 14
|
versionCode = 15
|
||||||
versionName = "1.8.2"
|
versionName = "1.8.3"
|
||||||
}
|
}
|
||||||
|
|
||||||
buildTypes {
|
buildTypes {
|
||||||
|
|||||||
@@ -8,4 +8,5 @@ data class AppNotification(
|
|||||||
val type: String = "info",
|
val type: String = "info",
|
||||||
val is_read: Int = 0,
|
val is_read: Int = 0,
|
||||||
val created_at: String = "",
|
val created_at: String = "",
|
||||||
|
val created_at_timestamp: Long = 0,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ class NotificationForegroundService : android.app.Service() {
|
|||||||
if (!response.success) return
|
if (!response.success) return
|
||||||
|
|
||||||
val newNotifications = response.data.filter { note ->
|
val newNotifications = response.data.filter { note ->
|
||||||
val noteTime = parseTime(note.created_at)
|
val noteTime = note.created_at_timestamp * 1000L
|
||||||
noteTime > lastCheck && note.is_read == 0
|
noteTime > lastCheck && note.is_read == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,19 +108,7 @@ class NotificationForegroundService : android.app.Service() {
|
|||||||
for (notification in newNotifications) {
|
for (notification in newNotifications) {
|
||||||
NotificationHelper.showNotification(this, notification)
|
NotificationHelper.showNotification(this, notification)
|
||||||
}
|
}
|
||||||
lastCheck = newNotifications.maxOf { parseTime(it.created_at) }
|
lastCheck = newNotifications.maxOf { it.created_at_timestamp * 1000L }
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun parseTime(time: String?): Long {
|
|
||||||
if (time == null) return 0L
|
|
||||||
return try {
|
|
||||||
val sdf = java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.US).apply {
|
|
||||||
timeZone = java.util.TimeZone.getTimeZone("UTC")
|
|
||||||
}
|
|
||||||
sdf.parse(time)?.time ?: 0L
|
|
||||||
} catch (_: Exception) {
|
|
||||||
0L
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -219,7 +219,7 @@ private fun NotificationItem(
|
|||||||
|
|
||||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||||
Text(
|
Text(
|
||||||
text = formatTime(notification.created_at),
|
text = formatTime(notification.created_at_timestamp * 1000L),
|
||||||
style = MaterialTheme.typography.labelSmall,
|
style = MaterialTheme.typography.labelSmall,
|
||||||
color = Grey500
|
color = Grey500
|
||||||
)
|
)
|
||||||
@@ -240,27 +240,17 @@ private fun NotificationItem(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun formatTime(timestamp: String?): String {
|
private fun formatTime(millis: Long): String {
|
||||||
if (timestamp == null) return ""
|
if (millis <= 0) return ""
|
||||||
return try {
|
val now = System.currentTimeMillis()
|
||||||
val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US).apply {
|
val diff = now - millis
|
||||||
timeZone = TimeZone.getTimeZone("UTC")
|
return when {
|
||||||
|
diff < 60_000 -> "Just now"
|
||||||
|
diff < 3600_000 -> "${diff / 60_000}m ago"
|
||||||
|
diff < 86400_000 -> "${diff / 3600_000}h ago"
|
||||||
|
else -> {
|
||||||
|
val sdf = SimpleDateFormat("MMM d, HH:mm", Locale.US)
|
||||||
|
sdf.format(Date(millis))
|
||||||
}
|
}
|
||||||
val date = sdf.parse(timestamp) ?: return timestamp
|
}
|
||||||
val now = Calendar.getInstance()
|
|
||||||
val then = Calendar.getInstance().apply {
|
|
||||||
this.time = date
|
|
||||||
}
|
|
||||||
|
|
||||||
val diff = now.timeInMillis - then.timeInMillis
|
|
||||||
when {
|
|
||||||
diff < 60_000 -> "Just now"
|
|
||||||
diff < 3600_000 -> "${diff / 60_000}m ago"
|
|
||||||
diff < 86400_000 -> "${diff / 3600_000}h ago"
|
|
||||||
else -> {
|
|
||||||
val out = SimpleDateFormat("MMM d, HH:mm", Locale.US)
|
|
||||||
out.format(date)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (_: Exception) { timestamp }
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -113,19 +113,8 @@ object NotificationHelper {
|
|||||||
markReadPendingIntent
|
markReadPendingIntent
|
||||||
)
|
)
|
||||||
.setAutoCancel(true)
|
.setAutoCancel(true)
|
||||||
.setWhen(parseTime(notification.created_at))
|
.setWhen(if (notification.created_at_timestamp > 0) notification.created_at_timestamp * 1000L else System.currentTimeMillis())
|
||||||
|
|
||||||
NotificationManagerCompat.from(context).notify(notification.id, built.build())
|
NotificationManagerCompat.from(context).notify(notification.id, built.build())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun parseTime(time: String?): Long {
|
|
||||||
if (time == null) return System.currentTimeMillis()
|
|
||||||
return try {
|
|
||||||
java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.US).apply {
|
|
||||||
timeZone = java.util.TimeZone.getTimeZone("UTC")
|
|
||||||
}.parse(time)?.time ?: System.currentTimeMillis()
|
|
||||||
} catch (_: Exception) {
|
|
||||||
System.currentTimeMillis()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package com.devlab.app.util
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import com.devlab.app.data.api.RetrofitClient
|
import com.devlab.app.data.api.RetrofitClient
|
||||||
import com.devlab.app.data.model.AppNotification
|
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
|
||||||
class NotificationPoller(private val context: Context) {
|
class NotificationPoller(private val context: Context) {
|
||||||
@@ -56,7 +55,7 @@ class NotificationPoller(private val context: Context) {
|
|||||||
onCountChanged?.invoke(_unreadCount)
|
onCountChanged?.invoke(_unreadCount)
|
||||||
|
|
||||||
val newNotes = response.data.filter { note ->
|
val newNotes = response.data.filter { note ->
|
||||||
val noteTime = parseTime(note.created_at)
|
val noteTime = note.created_at_timestamp * 1000L
|
||||||
noteTime > lastCheck && note.is_read == 0
|
noteTime > lastCheck && note.is_read == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,20 +64,10 @@ class NotificationPoller(private val context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (newNotes.isNotEmpty()) {
|
if (newNotes.isNotEmpty()) {
|
||||||
lastCheck = newNotes.maxOf { parseTime(it.created_at) }
|
lastCheck = newNotes.maxOf { it.created_at_timestamp * 1000L }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (_: Exception) { }
|
} catch (_: Exception) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun parseTime(time: String?): Long {
|
|
||||||
if (time == null) return 0L
|
|
||||||
return try {
|
|
||||||
val sdf = java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.US).apply {
|
|
||||||
timeZone = java.util.TimeZone.getTimeZone("UTC")
|
|
||||||
}
|
|
||||||
sdf.parse(time)?.time ?: 0L
|
|
||||||
} catch (_: Exception) { 0L }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import android.content.Context
|
|||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.work.*
|
import androidx.work.*
|
||||||
import com.devlab.app.data.api.RetrofitClient
|
import com.devlab.app.data.api.RetrofitClient
|
||||||
import com.devlab.app.data.model.AppNotification
|
|
||||||
import com.devlab.app.util.NotificationHelper
|
import com.devlab.app.util.NotificationHelper
|
||||||
import com.devlab.app.util.PreferencesManager
|
import com.devlab.app.util.PreferencesManager
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
@@ -40,7 +39,7 @@ class NotificationWorker(
|
|||||||
Log.d("NotificationWorker", "Got ${response.data.size} notifications, unread: ${response.unread_count}")
|
Log.d("NotificationWorker", "Got ${response.data.size} notifications, unread: ${response.unread_count}")
|
||||||
|
|
||||||
val newNotifications = response.data.filter { note ->
|
val newNotifications = response.data.filter { note ->
|
||||||
val noteTime = parseTime(note.created_at)
|
val noteTime = note.created_at_timestamp * 1000L
|
||||||
noteTime > lastCheck && note.is_read == 0
|
noteTime > lastCheck && note.is_read == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,7 +50,7 @@ class NotificationWorker(
|
|||||||
}
|
}
|
||||||
|
|
||||||
val maxTime = newNotifications.maxOfOrNull {
|
val maxTime = newNotifications.maxOfOrNull {
|
||||||
parseTime(it.created_at)
|
it.created_at_timestamp * 1000L
|
||||||
} ?: now
|
} ?: now
|
||||||
|
|
||||||
prefs.setLastNotificationCheck(
|
prefs.setLastNotificationCheck(
|
||||||
@@ -71,18 +70,6 @@ class NotificationWorker(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun parseTime(time: String?): Long {
|
|
||||||
if (time == null) return 0L
|
|
||||||
return try {
|
|
||||||
val sdf = java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.US).apply {
|
|
||||||
timeZone = java.util.TimeZone.getTimeZone("UTC")
|
|
||||||
}
|
|
||||||
sdf.parse(time)?.time ?: 0L
|
|
||||||
} catch (_: Exception) {
|
|
||||||
0L
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val WORK_NAME = "notification_check"
|
private const val WORK_NAME = "notification_check"
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -701,6 +701,7 @@ class ApiController
|
|||||||
|
|
||||||
$data = array_map(function ($n) {
|
$data = array_map(function ($n) {
|
||||||
$n['time_ago'] = time_ago($n['created_at'] ?? '');
|
$n['time_ago'] = time_ago($n['created_at'] ?? '');
|
||||||
|
$n['created_at_timestamp'] = $n['created_at'] ? strtotime($n['created_at']) : 0;
|
||||||
return $n;
|
return $n;
|
||||||
}, $result['data']);
|
}, $result['data']);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user