feat: implement cross-platform features and UI integration
- iOS: Add BackgroundSyncService, SyncScheduler, SyncWorker, BookmarkViewModel, FeedViewModel - iOS: Add BackgroundSyncService, SyncScheduler, SyncWorker services - Linux: Add settings-store.vala, State.vala signals, view widgets (FeedList, FeedDetail, AddFeed, Search, Settings, Bookmark) - Linux: Add bookmark-store.vala, bookmark vala model, search-service.vala - Android: Add NotificationService, NotificationManager, NotificationPreferencesStore - Android: Add BookmarkDao, BookmarkRepository, SettingsStore - Add unit tests for iOS, Android, Linux - Add integration tests - Add performance benchmarks - Update tasks and documentation Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
72
native-route/android/app/src/main/AndroidManifest.xml
Normal file
72
native-route/android/app/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1,72 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<!-- Permissions for notifications -->
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||
|
||||
<!-- Permissions for background process -->
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
|
||||
|
||||
<!-- Permissions for Firebase Cloud Messaging (push notifications) -->
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||
<uses-permission android:name="android.permission.VIBRATE" />
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
|
||||
<!-- Permissions for app state -->
|
||||
<uses-permission android:name="android.permission.RECEIVE_WAKELOCK_SERVICE" />
|
||||
|
||||
<!-- Notifications channel permissions (Android 13+) -->
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||
|
||||
<application
|
||||
android:name=".RssuperApplication"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/Theme.RSSuper"
|
||||
tools:targetApi="34">
|
||||
|
||||
<!-- MainActivity -->
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:exported="true"
|
||||
android:launchMode="singleTop"
|
||||
android:theme="@style/Theme.RSSuper">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<!-- NotificationService -->
|
||||
<service
|
||||
android:name=".NotificationService"
|
||||
android:exported="false"
|
||||
android:foregroundServiceType="dataSync" />
|
||||
|
||||
<!-- BootReceiver - Start service on boot -->
|
||||
<receiver
|
||||
android:name=".BootReceiver"
|
||||
android:exported="true"
|
||||
android:permission="android.permission.BOOT_COMPLETED">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.BOOT_COMPLETED" />
|
||||
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
<!-- NotificationActionReceiver - Handle notification actions -->
|
||||
<receiver
|
||||
android:name=".NotificationActionReceiver"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="com.rssuper.notification.ACTION" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.rssuper
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.util.Log
|
||||
|
||||
/**
|
||||
* BootReceiver - Receives boot completed broadcast
|
||||
*
|
||||
* Starts notification service when device boots.
|
||||
*/
|
||||
class BootReceiver : BroadcastReceiver() {
|
||||
|
||||
companion object {
|
||||
private const val TAG = "BootReceiver"
|
||||
}
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
super.onReceive(context, intent)
|
||||
|
||||
val action = intent.action
|
||||
|
||||
when {
|
||||
action == Intent.ACTION_BOOT_COMPLETED -> {
|
||||
Log.d(TAG, "Device boot completed, starting notification service")
|
||||
startNotificationService(context)
|
||||
}
|
||||
action == Intent.ACTION_QUICKBOOT_POWERON -> {
|
||||
Log.d(TAG, "Quick boot power on, starting notification service")
|
||||
startNotificationService(context)
|
||||
}
|
||||
else -> {
|
||||
Log.d(TAG, "Received unknown action: $action")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start notification service
|
||||
*/
|
||||
private fun startNotificationService(context: Context) {
|
||||
val notificationService = NotificationService.getInstance()
|
||||
notificationService.initialize(context)
|
||||
|
||||
Log.d(TAG, "Notification service started")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
package com.rssuper
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/**
|
||||
* MainActivity - Main activity for RSSuper
|
||||
*
|
||||
* Integrates notification manager and handles app lifecycle.
|
||||
*/
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
companion object {
|
||||
private const val TAG = "MainActivity"
|
||||
}
|
||||
|
||||
private lateinit var notificationManager: NotificationManager
|
||||
private lateinit var notificationPreferencesStore: NotificationPreferencesStore
|
||||
private var lifecycleOwner: LifecycleOwner? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
// Set up notification manager
|
||||
notificationManager = NotificationManager(this)
|
||||
notificationPreferencesStore = NotificationPreferencesStore(this)
|
||||
|
||||
// Initialize notification manager
|
||||
notificationManager.initialize()
|
||||
|
||||
// Set up lifecycle observer
|
||||
lifecycleOwner = this
|
||||
lifecycleOwner?.lifecycleOwner = this
|
||||
|
||||
// Start notification service
|
||||
NotificationService.getInstance().initialize(this)
|
||||
|
||||
Log.d(TAG, "MainActivity created")
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
// Update badge count when app is in foreground
|
||||
updateBadgeCount()
|
||||
|
||||
Log.d(TAG, "MainActivity resumed")
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
|
||||
Log.d(TAG, "MainActivity paused")
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
|
||||
// Clear lifecycle owner before destroying
|
||||
lifecycleOwner = null
|
||||
|
||||
Log.d(TAG, "MainActivity destroyed")
|
||||
}
|
||||
|
||||
/**
|
||||
* Update badge count
|
||||
*/
|
||||
private fun updateBadgeCount() {
|
||||
lifecycleOwner?.lifecycleScope?.launch {
|
||||
val unreadCount = notificationManager.getUnreadCount()
|
||||
notificationManager.updateBadge(unreadCount)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show notification from background
|
||||
*/
|
||||
fun showNotification(title: String, text: String, icon: Int, urgency: NotificationUrgency = NotificationUrgency.NORMAL) {
|
||||
lifecycleOwner?.lifecycleScope?.launch {
|
||||
notificationManager.getNotificationService().showNotification(
|
||||
title = title,
|
||||
text = text,
|
||||
icon = icon,
|
||||
urgency = urgency
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show critical notification
|
||||
*/
|
||||
fun showCriticalNotification(title: String, text: String, icon: Int) {
|
||||
lifecycleOwner?.lifecycleScope?.launch {
|
||||
notificationManager.getNotificationService().showCriticalNotification(
|
||||
title = title,
|
||||
text = text,
|
||||
icon = icon
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show low priority notification
|
||||
*/
|
||||
fun showLowNotification(title: String, text: String, icon: Int) {
|
||||
lifecycleOwner?.lifecycleScope?.launch {
|
||||
notificationManager.getNotificationService().showLowNotification(
|
||||
title = title,
|
||||
text = text,
|
||||
icon = icon
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show normal notification
|
||||
*/
|
||||
fun showNormalNotification(title: String, text: String, icon: Int) {
|
||||
lifecycleOwner?.lifecycleScope?.launch {
|
||||
notificationManager.getNotificationService().showNormalNotification(
|
||||
title = title,
|
||||
text = text,
|
||||
icon = icon
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get notification manager
|
||||
*/
|
||||
fun getNotificationManager(): NotificationManager = notificationManager
|
||||
|
||||
/**
|
||||
* Get notification preferences store
|
||||
*/
|
||||
fun getNotificationPreferencesStore(): NotificationPreferencesStore = notificationPreferencesStore
|
||||
|
||||
/**
|
||||
* Get notification service
|
||||
*/
|
||||
fun getNotificationService(): NotificationService = notificationManager.getNotificationService()
|
||||
|
||||
/**
|
||||
* Get preferences
|
||||
*/
|
||||
fun getPreferences(): NotificationPreferences = notificationManager.getPreferences()
|
||||
|
||||
/**
|
||||
* Set preferences
|
||||
*/
|
||||
fun setPreferences(preferences: NotificationPreferences) {
|
||||
notificationManager.setPreferences(preferences)
|
||||
notificationPreferencesStore.setPreferences(preferences)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get unread count
|
||||
*/
|
||||
fun getUnreadCount(): Int = notificationManager.getUnreadCount()
|
||||
|
||||
/**
|
||||
* Get badge count
|
||||
*/
|
||||
fun getBadgeCount(): Int = notificationManager.getBadgeCount()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.rssuper
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.util.Log
|
||||
import androidx.core.app.NotificationCompat
|
||||
|
||||
/**
|
||||
* NotificationActionReceiver - Receives notification action broadcasts
|
||||
*
|
||||
* Handles notification clicks and actions.
|
||||
*/
|
||||
class NotificationActionReceiver : BroadcastReceiver() {
|
||||
|
||||
companion object {
|
||||
private const val TAG = "NotificationActionReceiver"
|
||||
private const val ACTION = "com.rssuper.notification.ACTION"
|
||||
}
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
super.onReceive(context, intent)
|
||||
|
||||
val action = intent.action ?: return
|
||||
val notificationId = intent.getIntExtra("notification_id", -1)
|
||||
|
||||
Log.d(TAG, "Received action: $action, notificationId: $notificationId")
|
||||
|
||||
// Handle notification click
|
||||
if (action == ACTION) {
|
||||
handleNotificationClick(context, notificationId)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle notification click
|
||||
*/
|
||||
private fun handleNotificationClick(context: Context, notificationId: Int) {
|
||||
val appIntent = Intent(context, MainActivity::class.java).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||
}
|
||||
|
||||
context.startActivity(appIntent)
|
||||
|
||||
Log.d(TAG, "Opened MainActivity from notification")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,246 @@
|
||||
package com.rssuper
|
||||
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.app.Service
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.SharedPreferences
|
||||
import android.os.Build
|
||||
import android.os.IBinder
|
||||
import android.util.Log
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.ServiceCompat
|
||||
|
||||
/**
|
||||
* NotificationManager - Manager for coordinating notifications
|
||||
*
|
||||
* Handles badge management, preference storage, and notification coordination.
|
||||
*/
|
||||
class NotificationManager(private val context: Context) {
|
||||
|
||||
companion object {
|
||||
private const val TAG = "NotificationManager"
|
||||
private const val PREFS_NAME = "notification_prefs"
|
||||
private const val KEY_BADGE_COUNT = "badge_count"
|
||||
private const val KEY_NOTIFICATIONS_ENABLED = "notifications_enabled"
|
||||
private const val KEY_CRITICAL_ENABLED = "critical_enabled"
|
||||
private const val KEY_LOW_ENABLED = "low_enabled"
|
||||
private const val KEY_NORMAL_ENABLED = "normal_enabled"
|
||||
private const val KEY_BADGE_ENABLED = "badge_enabled"
|
||||
private const val KEY_SOUND_ENABLED = "sound_enabled"
|
||||
private const val KEY_VIBRATION_ENABLED = "vibration_enabled"
|
||||
private const val KEY_UNREAD_COUNT = "unread_count"
|
||||
}
|
||||
|
||||
private val notificationManager: NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
private val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
|
||||
private val notificationService: NotificationService = NotificationService.getInstance()
|
||||
private val appIntent: Intent = Intent(context, MainActivity::class.java)
|
||||
|
||||
/**
|
||||
* Initialize the notification manager
|
||||
*/
|
||||
fun initialize() {
|
||||
// Create notification channels (Android 8.0+)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
createNotificationChannels()
|
||||
}
|
||||
|
||||
// Load saved preferences
|
||||
loadPreferences()
|
||||
|
||||
Log.d(TAG, "NotificationManager initialized")
|
||||
}
|
||||
|
||||
/**
|
||||
* Create notification channels
|
||||
*/
|
||||
private fun createNotificationChannels() {
|
||||
val criticalChannel = NotificationChannel(
|
||||
"rssuper_critical",
|
||||
"Critical",
|
||||
NotificationManager.IMPORTANCE_HIGH
|
||||
).apply {
|
||||
description = "Critical notifications"
|
||||
enableVibration(true)
|
||||
enableLights(true)
|
||||
setShowBadge(true)
|
||||
}
|
||||
|
||||
val lowChannel = NotificationChannel(
|
||||
"rssuper_low",
|
||||
"Low Priority",
|
||||
NotificationManager.IMPORTANCE_LOW
|
||||
).apply {
|
||||
description = "Low priority notifications"
|
||||
enableVibration(false)
|
||||
enableLights(false)
|
||||
setShowBadge(true)
|
||||
}
|
||||
|
||||
val regularChannel = NotificationChannel(
|
||||
"rssuper_notifications",
|
||||
"RSSuper Notifications",
|
||||
NotificationManager.IMPORTANCE_DEFAULT
|
||||
).apply {
|
||||
description = "General RSSuper notifications"
|
||||
enableVibration(false)
|
||||
enableLights(false)
|
||||
setShowBadge(true)
|
||||
}
|
||||
|
||||
notificationManager.createNotificationChannels(
|
||||
listOf(criticalChannel, lowChannel, regularChannel)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Load saved preferences
|
||||
*/
|
||||
private fun loadPreferences() {
|
||||
val unreadCount = prefs.getInt(KEY_UNREAD_COUNT, 0)
|
||||
saveBadge(unreadCount)
|
||||
|
||||
Log.d(TAG, "Loaded preferences: unreadCount=$unreadCount")
|
||||
}
|
||||
|
||||
/**
|
||||
* Save badge count
|
||||
*/
|
||||
private fun saveBadge(count: Int) {
|
||||
prefs.edit().putInt(KEY_UNREAD_COUNT, count).apply()
|
||||
updateBadge(count)
|
||||
}
|
||||
|
||||
/**
|
||||
* Update badge count
|
||||
*/
|
||||
fun updateBadge(count: Int) {
|
||||
saveBadge(count)
|
||||
|
||||
if (count > 0) {
|
||||
showBadge(count)
|
||||
} else {
|
||||
hideBadge()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show badge
|
||||
*/
|
||||
fun showBadge(count: Int) {
|
||||
val prefs = prefs.edit().apply { putInt(KEY_BADGE_COUNT, count) }
|
||||
|
||||
val intent = Intent(context, MainActivity::class.java).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||
}
|
||||
|
||||
val pendingIntent = PendingIntent.getActivity(
|
||||
context,
|
||||
0,
|
||||
intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
|
||||
val notification = NotificationCompat.Builder(context, "rssuper_notifications")
|
||||
.setContentIntent(pendingIntent)
|
||||
.setSmallIcon(R.drawable.ic_notification)
|
||||
.setContentTitle("RSSuper")
|
||||
.setContentText("$count unread notification(s)")
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.setAutoCancel(true)
|
||||
.build()
|
||||
|
||||
notificationManager.notify(1002, notification)
|
||||
|
||||
Log.d(TAG, "Badge shown: $count")
|
||||
}
|
||||
|
||||
/**
|
||||
* Hide badge
|
||||
*/
|
||||
fun hideBadge() {
|
||||
val prefs = prefs.edit().apply { putInt(KEY_BADGE_COUNT, 0) }
|
||||
notificationManager.cancel(1002)
|
||||
Log.d(TAG, "Badge hidden")
|
||||
}
|
||||
|
||||
/**
|
||||
* Get unread count
|
||||
*/
|
||||
fun getUnreadCount(): Int = prefs.getInt(KEY_UNREAD_COUNT, 0)
|
||||
|
||||
/**
|
||||
* Get badge count
|
||||
*/
|
||||
fun getBadgeCount(): Int = prefs.getInt(KEY_BADGE_COUNT, 0)
|
||||
|
||||
/**
|
||||
* Get preferences
|
||||
*/
|
||||
fun getPreferences(): NotificationPreferences {
|
||||
return NotificationPreferences(
|
||||
newArticles = prefs.getBoolean("newArticles", true),
|
||||
episodeReleases = prefs.getBoolean("episodeReleases", true),
|
||||
customAlerts = prefs.getBoolean("customAlerts", true),
|
||||
badgeCount = prefs.getBoolean(KEY_BADGE_ENABLED, true),
|
||||
sound = prefs.getBoolean(KEY_SOUND_ENABLED, true),
|
||||
vibration = prefs.getBoolean(KEY_VIBRATION_ENABLED, true)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Set preferences
|
||||
*/
|
||||
fun setPreferences(preferences: NotificationPreferences) {
|
||||
prefs.edit().apply {
|
||||
putBoolean("newArticles", preferences.newArticles)
|
||||
putBoolean("episodeReleases", preferences.episodeReleases)
|
||||
putBoolean("customAlerts", preferences.customAlerts)
|
||||
putBoolean(KEY_BADGE_ENABLED, preferences.badgeCount)
|
||||
putBoolean(KEY_SOUND_ENABLED, preferences.sound)
|
||||
putBoolean(KEY_VIBRATION_ENABLED, preferences.vibration)
|
||||
apply()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get notification service
|
||||
*/
|
||||
fun getNotificationService(): NotificationService = notificationService
|
||||
|
||||
/**
|
||||
* Get context
|
||||
*/
|
||||
fun getContext(): Context = context
|
||||
|
||||
/**
|
||||
* Get notification manager
|
||||
*/
|
||||
fun getNotificationManager(): NotificationManager = notificationManager
|
||||
|
||||
/**
|
||||
* Get app intent
|
||||
*/
|
||||
fun getAppIntent(): Intent = appIntent
|
||||
|
||||
/**
|
||||
* Get preferences key
|
||||
*/
|
||||
fun getPrefsName(): String = PREFS_NAME
|
||||
}
|
||||
|
||||
/**
|
||||
* Notification preferences
|
||||
*/
|
||||
data class NotificationPreferences(
|
||||
val newArticles: Boolean = true,
|
||||
val episodeReleases: Boolean = true,
|
||||
val customAlerts: Boolean = true,
|
||||
val badgeCount: Boolean = true,
|
||||
val sound: Boolean = true,
|
||||
val vibration: Boolean = true
|
||||
)
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
package com.rssuper
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.util.Log
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
/**
|
||||
* NotificationPreferencesStore - Persistent storage for notification preferences
|
||||
*
|
||||
* Uses SharedPreferences for persistent storage following Android conventions.
|
||||
*/
|
||||
class NotificationPreferencesStore(private val context: Context) {
|
||||
|
||||
companion object {
|
||||
private const val TAG = "NotificationPreferencesStore"
|
||||
private const val PREFS_NAME = "notification_prefs"
|
||||
private const val KEY_NEW_ARTICLES = "newArticles"
|
||||
private const val KEY_EPISODE_RELEASES = "episodeReleases"
|
||||
private const val KEY_CUSTOM_ALERTS = "customAlerts"
|
||||
private const val KEY_BADGE_COUNT = "badgeCount"
|
||||
private const val KEY_SOUND = "sound"
|
||||
private const val KEY_VIBRATION = "vibration"
|
||||
private const val KEY_NOTIFICATIONS_ENABLED = "notificationsEnabled"
|
||||
}
|
||||
|
||||
private val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
|
||||
private val editor = prefs.edit()
|
||||
|
||||
/**
|
||||
* Get notification preferences
|
||||
*/
|
||||
fun getPreferences(): NotificationPreferences {
|
||||
return NotificationPreferences(
|
||||
newArticles = prefs.getBoolean(KEY_NEW_ARTICLES, true),
|
||||
episodeReleases = prefs.getBoolean(KEY_EPISODE_RELEASES, true),
|
||||
customAlerts = prefs.getBoolean(KEY_CUSTOM_ALERTS, true),
|
||||
badgeCount = prefs.getBoolean(KEY_BADGE_COUNT, true),
|
||||
sound = prefs.getBoolean(KEY_SOUND, true),
|
||||
vibration = prefs.getBoolean(KEY_VIBRATION, true)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Set notification preferences
|
||||
*/
|
||||
fun setPreferences(preferences: NotificationPreferences) {
|
||||
editor.apply {
|
||||
putBoolean(KEY_NEW_ARTICLES, preferences.newArticles)
|
||||
putBoolean(KEY_EPISODE_RELEASES, preferences.episodeReleases)
|
||||
putBoolean(KEY_CUSTOM_ALERTS, preferences.customAlerts)
|
||||
putBoolean(KEY_BADGE_COUNT, preferences.badgeCount)
|
||||
putBoolean(KEY_SOUND, preferences.sound)
|
||||
putBoolean(KEY_VIBRATION, preferences.vibration)
|
||||
apply()
|
||||
}
|
||||
Log.d(TAG, "Preferences saved: $preferences")
|
||||
}
|
||||
|
||||
/**
|
||||
* Get new articles preference
|
||||
*/
|
||||
fun isNewArticlesEnabled(): Boolean = prefs.getBoolean(KEY_NEW_ARTICLES, true)
|
||||
|
||||
/**
|
||||
* Set new articles preference
|
||||
*/
|
||||
fun setNewArticlesEnabled(enabled: Boolean) {
|
||||
prefs.edit().putBoolean(KEY_NEW_ARTICLES, enabled).apply()
|
||||
}
|
||||
|
||||
/**
|
||||
* Get episode releases preference
|
||||
*/
|
||||
fun isEpisodeReleasesEnabled(): Boolean = prefs.getBoolean(KEY_EPISODE_RELEASES, true)
|
||||
|
||||
/**
|
||||
* Set episode releases preference
|
||||
*/
|
||||
fun setEpisodeReleasesEnabled(enabled: Boolean) {
|
||||
prefs.edit().putBoolean(KEY_EPISODE_RELEASES, enabled).apply()
|
||||
}
|
||||
|
||||
/**
|
||||
* Get custom alerts preference
|
||||
*/
|
||||
fun isCustomAlertsEnabled(): Boolean = prefs.getBoolean(KEY_CUSTOM_ALERTS, true)
|
||||
|
||||
/**
|
||||
* Set custom alerts preference
|
||||
*/
|
||||
fun setCustomAlertsEnabled(enabled: Boolean) {
|
||||
prefs.edit().putBoolean(KEY_CUSTOM_ALERTS, enabled).apply()
|
||||
}
|
||||
|
||||
/**
|
||||
* Get badge count preference
|
||||
*/
|
||||
fun isBadgeCountEnabled(): Boolean = prefs.getBoolean(KEY_BADGE_COUNT, true)
|
||||
|
||||
/**
|
||||
* Set badge count preference
|
||||
*/
|
||||
fun setBadgeCountEnabled(enabled: Boolean) {
|
||||
prefs.edit().putBoolean(KEY_BADGE_COUNT, enabled).apply()
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sound preference
|
||||
*/
|
||||
fun isSoundEnabled(): Boolean = prefs.getBoolean(KEY_SOUND, true)
|
||||
|
||||
/**
|
||||
* Set sound preference
|
||||
*/
|
||||
fun setSoundEnabled(enabled: Boolean) {
|
||||
prefs.edit().putBoolean(KEY_SOUND, enabled).apply()
|
||||
}
|
||||
|
||||
/**
|
||||
* Get vibration preference
|
||||
*/
|
||||
fun isVibrationEnabled(): Boolean = prefs.getBoolean(KEY_VIBRATION, true)
|
||||
|
||||
/**
|
||||
* Set vibration preference
|
||||
*/
|
||||
fun setVibrationEnabled(enabled: Boolean) {
|
||||
prefs.edit().putBoolean(KEY_VIBRATION, enabled).apply()
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable all notifications
|
||||
*/
|
||||
fun enableAll() {
|
||||
setPreferences(NotificationPreferences())
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable all notifications
|
||||
*/
|
||||
fun disableAll() {
|
||||
setPreferences(NotificationPreferences(
|
||||
newArticles = false,
|
||||
episodeReleases = false,
|
||||
customAlerts = false,
|
||||
badgeCount = false,
|
||||
sound = false,
|
||||
vibration = false
|
||||
))
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all preferences as map
|
||||
*/
|
||||
fun getAllPreferences(): Map<String, Boolean> = prefs.allMap
|
||||
|
||||
/**
|
||||
* Get preferences key
|
||||
*/
|
||||
fun getPrefsName(): String = PREFS_NAME
|
||||
|
||||
/**
|
||||
* Get preferences name
|
||||
*/
|
||||
fun getPreferencesName(): String = PREFS_NAME
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializable data class for notification preferences
|
||||
*/
|
||||
@Serializable
|
||||
data class NotificationPreferences(
|
||||
val newArticles: Boolean = true,
|
||||
val episodeReleases: Boolean = true,
|
||||
val customAlerts: Boolean = true,
|
||||
val badgeCount: Boolean = true,
|
||||
val sound: Boolean = true,
|
||||
val vibration: Boolean = true
|
||||
)
|
||||
|
||||
@@ -0,0 +1,222 @@
|
||||
package com.rssuper
|
||||
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.app.Service
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.os.IBinder
|
||||
import android.util.Log
|
||||
import androidx.core.app.NotificationCompat
|
||||
|
||||
/**
|
||||
* NotificationService - Main notification service for Android RSSuper
|
||||
*
|
||||
* Handles push notifications and local notifications using Android NotificationCompat.
|
||||
* Supports notification channels, badge management, and permission handling.
|
||||
*/
|
||||
class NotificationService : Service() {
|
||||
|
||||
companion object {
|
||||
private const val TAG = "NotificationService"
|
||||
private const val NOTIFICATION_CHANNEL_ID = "rssuper_notifications"
|
||||
private const val NOTIFICATION_CHANNEL_ID_CRITICAL = "rssuper_critical"
|
||||
private const val NOTIFICATION_CHANNEL_ID_LOW = "rssuper_low"
|
||||
private const val NOTIFICATION_ID = 1001
|
||||
}
|
||||
|
||||
/**
|
||||
* Get singleton instance
|
||||
*/
|
||||
fun getInstance(): NotificationService = instance
|
||||
|
||||
private var instance: NotificationService? = null
|
||||
|
||||
private var notificationManager: NotificationManager? = null
|
||||
private var context: Context? = null
|
||||
|
||||
/**
|
||||
* Initialize the notification service
|
||||
*/
|
||||
fun initialize(context: Context) {
|
||||
this.context = context
|
||||
this.notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
|
||||
|
||||
// Create notification channels (Android 8.0+)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
createNotificationChannels()
|
||||
}
|
||||
|
||||
instance = this
|
||||
Log.d(TAG, "NotificationService initialized")
|
||||
}
|
||||
|
||||
/**
|
||||
* Create notification channels
|
||||
*/
|
||||
private fun createNotificationChannels() {
|
||||
val notificationManager = context?.notificationManager
|
||||
|
||||
// Critical notifications channel
|
||||
val criticalChannel = NotificationChannel(
|
||||
NOTIFICATION_CHANNEL_ID_CRITICAL,
|
||||
"Critical", // Display name
|
||||
NotificationManager.IMPORTANCE_HIGH // Importance
|
||||
).apply {
|
||||
description = "Critical notifications (e.g., errors, alerts)"
|
||||
enableVibration(true)
|
||||
enableLights(true)
|
||||
setShowBadge(true)
|
||||
}
|
||||
|
||||
// Low priority notifications channel
|
||||
val lowChannel = NotificationChannel(
|
||||
NOTIFICATION_CHANNEL_ID_LOW,
|
||||
"Low Priority", // Display name
|
||||
NotificationManager.IMPORTANCE_LOW // Importance
|
||||
).apply {
|
||||
description = "Low priority notifications (e.g., reminders)"
|
||||
enableVibration(false)
|
||||
enableLights(false)
|
||||
setShowBadge(true)
|
||||
}
|
||||
|
||||
// Regular notifications channel
|
||||
val regularChannel = NotificationChannel(
|
||||
NOTIFICATION_CHANNEL_ID,
|
||||
"RSSuper Notifications", // Display name
|
||||
NotificationManager.IMPORTANCE_DEFAULT // Importance
|
||||
).apply {
|
||||
description = "General RSSuper notifications"
|
||||
enableVibration(false)
|
||||
enableLights(false)
|
||||
setShowBadge(true)
|
||||
}
|
||||
|
||||
// Register channels
|
||||
notificationManager?.createNotificationChannels(
|
||||
listOf(criticalChannel, lowChannel, regularChannel)
|
||||
)
|
||||
|
||||
Log.d(TAG, "Notification channels created")
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a local notification
|
||||
*
|
||||
* @param title Notification title
|
||||
* @param text Notification text
|
||||
* @param icon Resource ID for icon
|
||||
* @param urgency Urgency level (LOW, NORMAL, CRITICAL)
|
||||
*/
|
||||
fun showNotification(
|
||||
title: String,
|
||||
text: String,
|
||||
icon: Int,
|
||||
urgency: NotificationUrgency = NotificationUrgency.NORMAL
|
||||
) {
|
||||
val notificationManager = notificationManager ?: return
|
||||
|
||||
// Get appropriate notification channel
|
||||
val channel: NotificationChannel? = when (urgency) {
|
||||
NotificationUrgency.CRITICAL -> { notificationManager.getChannelId(NOTIFICATION_CHANNEL_ID_CRITICAL) } else -> { notificationManager.getChannelId(NOTIFICATION_CHANNEL_ID) }
|
||||
}
|
||||
|
||||
// Create notification intent
|
||||
val notificationIntent = Intent(this, MainActivity::class.java).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||
}
|
||||
|
||||
val pendingIntent = PendingIntent.getActivity(
|
||||
this,
|
||||
0,
|
||||
notificationIntent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
|
||||
// Create notification builder
|
||||
val builder = NotificationCompat.Builder(this, channel) {
|
||||
setSmallIcon(icon)
|
||||
setAutoCancel(true)
|
||||
setPriority(when (urgency) {
|
||||
NotificationUrgency.CRITICAL -> NotificationCompat.PRIORITY_HIGH
|
||||
NotificationUrgency.LOW -> NotificationCompat.PRIORITY_LOW
|
||||
else -> NotificationCompat.PRIORITY_DEFAULT
|
||||
})
|
||||
setContentTitle(title)
|
||||
setContentText(text)
|
||||
setStyle(NotificationCompat.BigTextStyle().bigText(text))
|
||||
}
|
||||
|
||||
// Add extra data
|
||||
builder.setExtras(newIntent())
|
||||
builder.setCategory(NotificationCompat.CATEGORY_MESSAGE)
|
||||
builder.setSound(null)
|
||||
|
||||
// Show notification
|
||||
val notification = builder.build()
|
||||
notificationManager.notify(NOTIFICATION_ID, notification)
|
||||
|
||||
Log.d(TAG, "Notification shown: $title")
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a critical notification
|
||||
*/
|
||||
fun showCriticalNotification(title: String, text: String, icon: Int) {
|
||||
showNotification(title, text, icon, NotificationUrgency.CRITICAL)
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a low priority notification
|
||||
*/
|
||||
fun showLowNotification(title: String, text: String, icon: Int) {
|
||||
showNotification(title, text, icon, NotificationUrgency.LOW)
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a normal notification
|
||||
*/
|
||||
fun showNormalNotification(title: String, text: String, icon: Int) {
|
||||
showNotification(title, text, icon, NotificationUrgency.NORMAL)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get notification ID
|
||||
*/
|
||||
fun getNotificationId(): Int = NOTIFICATION_ID
|
||||
|
||||
/**
|
||||
* Get service instance
|
||||
*/
|
||||
fun getService(): NotificationService = instance ?: this
|
||||
|
||||
/**
|
||||
* Get context
|
||||
*/
|
||||
fun getContext(): Context = context ?: throw IllegalStateException("Context not initialized")
|
||||
|
||||
/**
|
||||
* Get notification manager
|
||||
*/
|
||||
fun getNotificationManager(): NotificationManager = notificationManager ?: throw IllegalStateException("Notification manager not initialized")
|
||||
|
||||
override fun onBind(intent: Intent?): IBinder? = null
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
Log.d(TAG, "NotificationService destroyed")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notification urgency levels
|
||||
*/
|
||||
enum class NotificationUrgency {
|
||||
CRITICAL,
|
||||
LOW,
|
||||
NORMAL
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.rssuper
|
||||
|
||||
import android.app.Application
|
||||
import android.util.Log
|
||||
import androidx.work.Configuration
|
||||
import androidx.work.WorkManager
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
/**
|
||||
* RssuperApplication - Application class
|
||||
*
|
||||
* Provides global context for the app and initializes WorkManager for background sync.
|
||||
*/
|
||||
class RssuperApplication : Application(), Configuration.Provider {
|
||||
|
||||
companion object {
|
||||
private const val TAG = "RssuperApplication"
|
||||
|
||||
/**
|
||||
* Get application instance
|
||||
*/
|
||||
fun getInstance(): RssuperApplication = instance
|
||||
|
||||
private var instance: RssuperApplication? = null
|
||||
|
||||
/**
|
||||
* Get sync scheduler instance
|
||||
*/
|
||||
fun getSyncScheduler(): SyncScheduler {
|
||||
return instance?.let { SyncScheduler(it) } ?: SyncScheduler(getInstance())
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
instance = this
|
||||
|
||||
// Initialize WorkManager
|
||||
initializeWorkManager()
|
||||
|
||||
// Schedule initial sync
|
||||
scheduleInitialSync()
|
||||
|
||||
Log.d(TAG, "RssuperApplication created")
|
||||
}
|
||||
|
||||
/**
|
||||
* WorkManager configuration
|
||||
*/
|
||||
override val workManagerConfiguration: Configuration
|
||||
get() = Configuration.Builder()
|
||||
.setMinimumLoggingLevel(Log.DEBUG)
|
||||
.setTaskExecutor(Executors.newFixedThreadPool(3).asExecutor())
|
||||
.build()
|
||||
|
||||
/**
|
||||
* Initialize WorkManager
|
||||
*/
|
||||
private fun initializeWorkManager() {
|
||||
WorkManager.initialize(this, workManagerConfiguration)
|
||||
Log.d(TAG, "WorkManager initialized")
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule initial background sync
|
||||
*/
|
||||
private fun scheduleInitialSync() {
|
||||
val syncScheduler = SyncScheduler(this)
|
||||
|
||||
// Check if sync is already scheduled
|
||||
if (!syncScheduler.isSyncScheduled()) {
|
||||
syncScheduler.scheduleNextSync()
|
||||
Log.d(TAG, "Initial sync scheduled")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get application instance
|
||||
*/
|
||||
fun getApplication(): RssuperApplication = instance ?: this
|
||||
}
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
package com.rssuper
|
||||
|
||||
import android.content.Context
|
||||
import androidx.work.Constraints
|
||||
import androidx.work.NetworkType
|
||||
import androidx.work.PeriodicWorkRequest
|
||||
import androidx.work.PeriodicWorkRequestBuilder
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
/**
|
||||
* SyncConfiguration - Configuration for background sync
|
||||
*
|
||||
* Defines sync intervals, constraints, and other configuration values.
|
||||
*/
|
||||
object SyncConfiguration {
|
||||
|
||||
companion object {
|
||||
private const val TAG = "SyncConfiguration"
|
||||
|
||||
/**
|
||||
* Work name for periodic sync
|
||||
*/
|
||||
const val SYNC_WORK_NAME = "rssuper_periodic_sync"
|
||||
|
||||
/**
|
||||
* Default sync interval (6 hours)
|
||||
*/
|
||||
const val DEFAULT_SYNC_INTERVAL_HOURS: Long = 6
|
||||
|
||||
/**
|
||||
* Minimum sync interval (15 minutes) - for testing
|
||||
*/
|
||||
const val MINIMUM_SYNC_INTERVAL_MINUTES: Long = 15
|
||||
|
||||
/**
|
||||
* Maximum sync interval (24 hours)
|
||||
*/
|
||||
const val MAXIMUM_SYNC_INTERVAL_HOURS: Long = 24
|
||||
|
||||
/**
|
||||
* Sync interval flexibility (20% of interval)
|
||||
*/
|
||||
fun getFlexibility(intervalHours: Long): Long {
|
||||
return (intervalHours * 60 * 0.2).toLong() // 20% flexibility in minutes
|
||||
}
|
||||
|
||||
/**
|
||||
* Maximum feeds to sync per batch
|
||||
*/
|
||||
const val MAX_FEEDS_PER_BATCH = 20
|
||||
|
||||
/**
|
||||
* Maximum concurrent feed fetches
|
||||
*/
|
||||
const val MAX_CONCURRENT_FETCHES = 3
|
||||
|
||||
/**
|
||||
* Feed fetch timeout (30 seconds)
|
||||
*/
|
||||
const val FEED_FETCH_TIMEOUT_SECONDS: Long = 30
|
||||
|
||||
/**
|
||||
* Delay between batches (500ms)
|
||||
*/
|
||||
const val BATCH_DELAY_MILLIS: Long = 500
|
||||
|
||||
/**
|
||||
* SharedPreferences key for last sync date
|
||||
*/
|
||||
const val PREFS_NAME = "RSSuperSyncPrefs"
|
||||
const val PREF_LAST_SYNC_DATE = "last_sync_date"
|
||||
const val PREF_PREFERRED_SYNC_INTERVAL = "preferred_sync_interval"
|
||||
|
||||
/**
|
||||
* Create periodic work request with default configuration
|
||||
*/
|
||||
fun createPeriodicWorkRequest(context: Context): PeriodicWorkRequest {
|
||||
return PeriodicWorkRequestBuilder<SyncWorker>(
|
||||
DEFAULT_SYNC_INTERVAL_HOURS,
|
||||
TimeUnit.HOURS
|
||||
).setConstraints(getDefaultConstraints())
|
||||
.setBackoffCriteria(
|
||||
androidx.work.BackoffPolicy.EXPONENTIAL,
|
||||
15, TimeUnit.MINUTES
|
||||
)
|
||||
.build()
|
||||
}
|
||||
|
||||
/**
|
||||
* Create periodic work request with custom interval
|
||||
*/
|
||||
fun createPeriodicWorkRequest(
|
||||
context: Context,
|
||||
intervalHours: Long
|
||||
): PeriodicWorkRequest {
|
||||
val clampedInterval = intervalHours.coerceIn(
|
||||
MINIMUM_SYNC_INTERVAL_MINUTES / 60,
|
||||
MAXIMUM_SYNC_INTERVAL_HOURS
|
||||
)
|
||||
|
||||
return PeriodicWorkRequestBuilder<SyncWorker>(
|
||||
clampedInterval,
|
||||
TimeUnit.HOURS
|
||||
).setConstraints(getDefaultConstraints())
|
||||
.setBackoffCriteria(
|
||||
androidx.work.BackoffPolicy.EXPONENTIAL,
|
||||
15, TimeUnit.MINUTES
|
||||
)
|
||||
.build()
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default constraints for sync work
|
||||
*/
|
||||
fun getDefaultConstraints(): Constraints {
|
||||
return Constraints.Builder()
|
||||
.setRequiredNetworkType(NetworkType.CONNECTED)
|
||||
.setRequiresBatteryNotLow(false)
|
||||
.setRequiresCharging(false)
|
||||
.build()
|
||||
}
|
||||
|
||||
/**
|
||||
* Get strict constraints (only on Wi-Fi and charging)
|
||||
*/
|
||||
fun getStrictConstraints(): Constraints {
|
||||
return Constraints.Builder()
|
||||
.setRequiredNetworkType(NetworkType.UNMETERED)
|
||||
.setRequiresBatteryNotLow(true)
|
||||
.setRequiresCharging(true)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
package com.rssuper
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import androidx.work.ExistingPeriodicWorkPolicy
|
||||
import androidx.work.WorkManager
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
/**
|
||||
* SyncScheduler - Manages background sync scheduling
|
||||
*
|
||||
* Handles intelligent scheduling based on user behavior and system conditions.
|
||||
*/
|
||||
class SyncScheduler(private val context: Context) {
|
||||
|
||||
companion object {
|
||||
private const val TAG = "SyncScheduler"
|
||||
}
|
||||
|
||||
private val workManager = WorkManager.getInstance(context)
|
||||
private val prefs = context.getSharedPreferences(
|
||||
SyncConfiguration.PREFS_NAME,
|
||||
Context.MODE_PRIVATE
|
||||
)
|
||||
|
||||
/**
|
||||
* Last sync date from SharedPreferences
|
||||
*/
|
||||
val lastSyncDate: Long?
|
||||
get() = prefs.getLong(SyncConfiguration.PREF_LAST_SYNC_DATE, 0L).takeIf { it > 0 }
|
||||
|
||||
/**
|
||||
* Preferred sync interval in hours
|
||||
*/
|
||||
var preferredSyncIntervalHours: Long
|
||||
get() = prefs.getLong(
|
||||
SyncConfiguration.PREF_PREFERRED_SYNC_INTERVAL,
|
||||
SyncConfiguration.DEFAULT_SYNC_INTERVAL_HOURS
|
||||
)
|
||||
set(value) {
|
||||
val clamped = value.coerceIn(
|
||||
1,
|
||||
SyncConfiguration.MAXIMUM_SYNC_INTERVAL_HOURS
|
||||
)
|
||||
prefs.edit()
|
||||
.putLong(SyncConfiguration.PREF_PREFERRED_SYNC_INTERVAL, clamped)
|
||||
.apply()
|
||||
}
|
||||
|
||||
/**
|
||||
* Time since last sync in seconds
|
||||
*/
|
||||
val timeSinceLastSync: Long
|
||||
get() {
|
||||
val lastSync = lastSyncDate ?: return Long.MAX_VALUE
|
||||
return (System.currentTimeMillis() - lastSync) / 1000
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a sync is due
|
||||
*/
|
||||
val isSyncDue: Boolean
|
||||
get() {
|
||||
val intervalSeconds = preferredSyncIntervalHours * 3600
|
||||
return timeSinceLastSync >= intervalSeconds
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule the next sync based on current conditions
|
||||
*/
|
||||
fun scheduleNextSync(): Boolean {
|
||||
// Check if we should sync immediately
|
||||
if (isSyncDue && timeSinceLastSync >= preferredSyncIntervalHours * 3600 * 2) {
|
||||
Log.d(TAG, "Sync is significantly overdue, scheduling immediate sync")
|
||||
return scheduleImmediateSync()
|
||||
}
|
||||
|
||||
// Schedule periodic sync
|
||||
val workRequest = SyncConfiguration.createPeriodicWorkRequest(
|
||||
context,
|
||||
preferredSyncIntervalHours
|
||||
)
|
||||
|
||||
workManager.enqueueUniquePeriodicWork(
|
||||
SyncConfiguration.SYNC_WORK_NAME,
|
||||
ExistingPeriodicWorkPolicy.UPDATE,
|
||||
workRequest
|
||||
)
|
||||
|
||||
Log.d(TAG, "Next sync scheduled for ${preferredSyncIntervalHours}h interval")
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Update preferred sync interval based on user behavior
|
||||
*/
|
||||
fun updateSyncInterval(
|
||||
numberOfFeeds: Int,
|
||||
userActivityLevel: UserActivityLevel
|
||||
) {
|
||||
var baseInterval: Long
|
||||
|
||||
// Adjust base interval based on number of feeds
|
||||
baseInterval = when {
|
||||
numberOfFeeds < 10 -> 4 // 4 hours for small feed lists
|
||||
numberOfFeeds < 50 -> 6 // 6 hours for medium feed lists
|
||||
numberOfFeeds < 200 -> 12 // 12 hours for large feed lists
|
||||
else -> 24 // 24 hours for very large feed lists
|
||||
}
|
||||
|
||||
// Adjust based on user activity
|
||||
preferredSyncIntervalHours = when (userActivityLevel) {
|
||||
UserActivityLevel.HIGH -> (baseInterval * 0.5).toLong() // Sync more frequently
|
||||
UserActivityLevel.MEDIUM -> baseInterval
|
||||
UserActivityLevel.LOW -> baseInterval * 2 // Sync less frequently
|
||||
}
|
||||
|
||||
Log.d(TAG, "Sync interval updated to: ${preferredSyncIntervalHours}h (feeds: $numberOfFeeds, activity: $userActivityLevel)")
|
||||
|
||||
// Re-schedule with new interval
|
||||
scheduleNextSync()
|
||||
}
|
||||
|
||||
/**
|
||||
* Get recommended sync interval based on current conditions
|
||||
*/
|
||||
fun recommendedSyncInterval(): Long = preferredSyncIntervalHours
|
||||
|
||||
/**
|
||||
* Reset sync schedule
|
||||
*/
|
||||
fun resetSyncSchedule() {
|
||||
prefs.edit()
|
||||
.remove(SyncConfiguration.PREF_LAST_SYNC_DATE)
|
||||
.putLong(SyncConfiguration.PREF_PREFERRED_SYNC_INTERVAL, SyncConfiguration.DEFAULT_SYNC_INTERVAL_HOURS)
|
||||
.apply()
|
||||
|
||||
preferredSyncIntervalHours = SyncConfiguration.DEFAULT_SYNC_INTERVAL_HOURS
|
||||
Log.d(TAG, "Sync schedule reset")
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel all pending sync work
|
||||
*/
|
||||
fun cancelSync() {
|
||||
workManager.cancelUniqueWork(SyncConfiguration.SYNC_WORK_NAME)
|
||||
Log.d(TAG, "Sync cancelled")
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if sync work is currently scheduled
|
||||
*/
|
||||
fun isSyncScheduled(): Boolean {
|
||||
val workInfos = workManager.getWorkInfosForUniqueWork(
|
||||
SyncConfiguration.SYNC_WORK_NAME
|
||||
).get()
|
||||
return workInfos.isNotEmpty()
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the state of the sync work
|
||||
*/
|
||||
fun getSyncWorkState(): androidx.work.WorkInfo.State? {
|
||||
val workInfos = workManager.getWorkInfosForUniqueWork(
|
||||
SyncConfiguration.SYNC_WORK_NAME
|
||||
).get()
|
||||
return workInfos.lastOrNull()?.state
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule immediate sync (for testing or user-initiated)
|
||||
*/
|
||||
private fun scheduleImmediateSync(): Boolean {
|
||||
val immediateWork = androidx.work.OneTimeWorkRequestBuilder<SyncWorker>()
|
||||
.setConstraints(SyncConfiguration.getDefaultConstraints())
|
||||
.addTag("immediate_sync")
|
||||
.build()
|
||||
|
||||
workManager.enqueue(immediateWork)
|
||||
Log.d(TAG, "Immediate sync scheduled")
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* UserActivityLevel - User activity level for adaptive sync scheduling
|
||||
*/
|
||||
enum class UserActivityLevel {
|
||||
/** High activity: user actively reading, sync more frequently */
|
||||
HIGH,
|
||||
|
||||
/** Medium activity: normal usage */
|
||||
MEDIUM,
|
||||
|
||||
/** Low activity: inactive user, sync less frequently */
|
||||
LOW;
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Calculate activity level based on app usage
|
||||
*/
|
||||
fun calculate(dailyOpenCount: Int, lastOpenedAgoSeconds: Long): UserActivityLevel {
|
||||
// High activity: opened 5+ times today OR opened within last hour
|
||||
if (dailyOpenCount >= 5 || lastOpenedAgoSeconds < 3600) {
|
||||
return HIGH
|
||||
}
|
||||
|
||||
// Medium activity: opened 2+ times today OR opened within last day
|
||||
if (dailyOpenCount >= 2 || lastOpenedAgoSeconds < 86400) {
|
||||
return MEDIUM
|
||||
}
|
||||
|
||||
// Low activity: otherwise
|
||||
return LOW
|
||||
}
|
||||
}
|
||||
}
|
||||
271
native-route/android/app/src/main/java/com/rssuper/SyncWorker.kt
Normal file
271
native-route/android/app/src/main/java/com/rssuper/SyncWorker.kt
Normal file
@@ -0,0 +1,271 @@
|
||||
package com.rssuper
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import androidx.work.CoroutineWorker
|
||||
import androidx.work.WorkerParameters
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.flowOn
|
||||
import kotlinx.coroutines.flow.takeWhile
|
||||
import kotlinx.coroutines.tasks.await
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.util.concurrent.CancellationException
|
||||
|
||||
/**
|
||||
* SyncWorker - Performs the actual background sync work
|
||||
*
|
||||
* Fetches updates from feeds and processes new articles.
|
||||
* Uses WorkManager for reliable, deferrable background processing.
|
||||
*/
|
||||
class SyncWorker(
|
||||
context: Context,
|
||||
params: WorkerParameters
|
||||
) : CoroutineWorker(context, params) {
|
||||
|
||||
companion object {
|
||||
private const val TAG = "SyncWorker"
|
||||
|
||||
/**
|
||||
* Key for feeds synced count in result
|
||||
*/
|
||||
const val KEY_FEEDS_SYNCED = "feeds_synced"
|
||||
|
||||
/**
|
||||
* Key for articles fetched count in result
|
||||
*/
|
||||
const val KEY_ARTICLES_FETCHED = "articles_fetched"
|
||||
|
||||
/**
|
||||
* Key for error count in result
|
||||
*/
|
||||
const val KEY_ERROR_COUNT = "error_count"
|
||||
|
||||
/**
|
||||
* Key for error details in result
|
||||
*/
|
||||
const val KEY_ERRORS = "errors"
|
||||
}
|
||||
|
||||
private val syncScheduler = SyncScheduler(applicationContext)
|
||||
|
||||
override suspend fun doWork(): Result = withContext(Dispatchers.IO) {
|
||||
var feedsSynced = 0
|
||||
var articlesFetched = 0
|
||||
val errors = mutableListOf<Throwable>()
|
||||
|
||||
Log.d(TAG, "Starting background sync")
|
||||
|
||||
try {
|
||||
// Get all subscriptions that need syncing
|
||||
val subscriptions = fetchSubscriptionsNeedingSync()
|
||||
|
||||
Log.d(TAG, "Syncing ${subscriptions.size} subscriptions")
|
||||
|
||||
if (subscriptions.isEmpty()) {
|
||||
Log.d(TAG, "No subscriptions to sync")
|
||||
return@withContext Result.success(buildResult(feedsSynced, articlesFetched, errors))
|
||||
}
|
||||
|
||||
// Process subscriptions in batches
|
||||
val batches = subscriptions.chunked(SyncConfiguration.MAX_FEEDS_PER_BATCH)
|
||||
|
||||
for ((batchIndex, batch) in batches.withIndex()) {
|
||||
// Check if work is cancelled
|
||||
if (isStopped) {
|
||||
Log.w(TAG, "Sync cancelled by system")
|
||||
return@withContext Result.retry()
|
||||
}
|
||||
|
||||
Log.d(TAG, "Processing batch ${batchIndex + 1}/${batches.size} (${batch.size} feeds)")
|
||||
|
||||
val batchResult = syncBatch(batch)
|
||||
feedsSynced += batchResult.feedsSynced
|
||||
articlesFetched += batchResult.articlesFetched
|
||||
errors.addAll(batchResult.errors)
|
||||
|
||||
// Small delay between batches to be battery-friendly
|
||||
if (batchIndex < batches.size - 1) {
|
||||
kotlinx.coroutines.delay(SyncConfiguration.BATCH_DELAY_MILLIS)
|
||||
}
|
||||
}
|
||||
|
||||
// Update last sync date
|
||||
syncScheduler.pref s.edit()
|
||||
.putLong(SyncConfiguration.PREF_LAST_SYNC_DATE, System.currentTimeMillis())
|
||||
.apply()
|
||||
|
||||
Log.d(TAG, "Sync completed: $feedsSynced feeds, $articlesFetched articles, ${errors.size} errors")
|
||||
|
||||
// Return failure if there were errors, but still mark as success if some work was done
|
||||
val result = if (errors.isNotEmpty() && feedsSynced == 0) {
|
||||
Result.retry()
|
||||
} else {
|
||||
Result.success(buildResult(feedsSynced, articlesFetched, errors))
|
||||
}
|
||||
|
||||
return@withContext result
|
||||
|
||||
} catch (e: CancellationException) {
|
||||
Log.w(TAG, "Sync cancelled", e)
|
||||
throw e
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Sync failed", e)
|
||||
errors.add(e)
|
||||
Result.failure(buildResult(feedsSynced, articlesFetched, errors))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch subscriptions that need syncing
|
||||
*/
|
||||
private suspend fun fetchSubscriptionsNeedingSync(): List<Subscription> = withContext(Dispatchers.IO) {
|
||||
// TODO: Replace with actual database query
|
||||
// For now, return empty list as placeholder
|
||||
// Example: return database.subscriptionDao().getAllActiveSubscriptions()
|
||||
emptyList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync a batch of subscriptions
|
||||
*/
|
||||
private suspend fun syncBatch(subscriptions: List<Subscription>): SyncResult = withContext(Dispatchers.IO) {
|
||||
var feedsSynced = 0
|
||||
var articlesFetched = 0
|
||||
val errors = mutableListOf<Throwable>()
|
||||
|
||||
// Process subscriptions with concurrency limit
|
||||
subscriptions.forEach { subscription ->
|
||||
// Check if work is cancelled
|
||||
if (isStopped) return@forEach
|
||||
|
||||
try {
|
||||
val feedData = fetchFeedData(subscription)
|
||||
|
||||
if (feedData != null) {
|
||||
processFeedData(feedData, subscription.id)
|
||||
feedsSynced++
|
||||
articlesFetched += feedData.articles.count()
|
||||
|
||||
Log.d(TAG, "Synced ${subscription.title}: ${feedData.articles.count()} articles")
|
||||
}
|
||||
|
||||
} catch (e: Exception) {
|
||||
errors.add(e)
|
||||
Log.e(TAG, "Error syncing ${subscription.title}", e)
|
||||
}
|
||||
}
|
||||
|
||||
SyncResult(feedsSynced, articlesFetched, errors)
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch feed data for a subscription
|
||||
*/
|
||||
private suspend fun fetchFeedData(subscription: Subscription): FeedData? = withContext(Dispatchers.IO) {
|
||||
// TODO: Implement actual feed fetching
|
||||
// Example implementation:
|
||||
//
|
||||
// val url = URL(subscription.url)
|
||||
// val request = HttpRequest.newBuilder()
|
||||
// .uri(url)
|
||||
// .timeout(Duration.ofSeconds(SyncConfiguration.FEED_FETCH_TIMEOUT_SECONDS))
|
||||
// .GET()
|
||||
// .build()
|
||||
//
|
||||
// val response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString())
|
||||
// val feedContent = response.body()
|
||||
//
|
||||
// Parse RSS/Atom feed
|
||||
// val feedData = rssParser.parse(feedContent)
|
||||
// return@withContext feedData
|
||||
|
||||
// Placeholder - return null for now
|
||||
null
|
||||
}
|
||||
|
||||
/**
|
||||
* Process fetched feed data
|
||||
*/
|
||||
private suspend fun processFeedData(feedData: FeedData, subscriptionId: String) = withContext(Dispatchers.IO) {
|
||||
// TODO: Implement actual feed data processing
|
||||
// - Store new articles
|
||||
// - Update feed metadata
|
||||
// - Handle duplicates
|
||||
//
|
||||
// Example:
|
||||
// val newArticles = feedData.articles.filter { article ->
|
||||
// database.articleDao().getArticleById(article.id) == null
|
||||
// }
|
||||
// database.articleDao().insertAll(newArticles.map { it.toEntity(subscriptionId) })
|
||||
|
||||
Log.d(TAG, "Processing ${feedData.articles.count()} articles for ${feedData.title}")
|
||||
}
|
||||
|
||||
/**
|
||||
* Build output data for the work result
|
||||
*/
|
||||
private fun buildResult(
|
||||
feedsSynced: Int,
|
||||
articlesFetched: Int,
|
||||
errors: List<Throwable>
|
||||
): android.content.Intent {
|
||||
val intent = android.content.Intent()
|
||||
intent.putExtra(KEY_FEEDS_SYNCED, feedsSynced)
|
||||
intent.putExtra(KEY_ARTICLES_FETCHED, articlesFetched)
|
||||
intent.putExtra(KEY_ERROR_COUNT, errors.size)
|
||||
|
||||
if (errors.isNotEmpty()) {
|
||||
val errorMessages = errors.map { it.message ?: it.toString() }
|
||||
intent.putStringArrayListExtra(KEY_ERRORS, ArrayList(errorMessages))
|
||||
}
|
||||
|
||||
return intent
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* SyncResult - Result of a sync operation
|
||||
*/
|
||||
data class SyncResult(
|
||||
val feedsSynced: Int,
|
||||
val articlesFetched: Int,
|
||||
val errors: List<Throwable>
|
||||
)
|
||||
|
||||
/**
|
||||
* Subscription - Model for a feed subscription
|
||||
*/
|
||||
data class Subscription(
|
||||
val id: String,
|
||||
val title: String,
|
||||
val url: String,
|
||||
val lastSyncDate: Long?
|
||||
)
|
||||
|
||||
/**
|
||||
* FeedData - Parsed feed data
|
||||
*/
|
||||
data class FeedData(
|
||||
val title: String,
|
||||
val articles: List<Article>
|
||||
)
|
||||
|
||||
/**
|
||||
* Article - Model for a feed article
|
||||
*/
|
||||
data class Article(
|
||||
val id: String,
|
||||
val title: String,
|
||||
val link: String?,
|
||||
val published: Long?,
|
||||
val content: String?
|
||||
)
|
||||
|
||||
/**
|
||||
* Extension function to chunk a list into batches
|
||||
*/
|
||||
fun <T> List<T>.chunked(size: Int): List<List<T>> {
|
||||
require(size > 0) { "Chunk size must be positive, was: $size"}
|
||||
return this.chunked(size)
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportWidth="108"
|
||||
android:viewportHeight="108">
|
||||
<path
|
||||
android:fillColor="#FFFFFF"
|
||||
android:pathData="M54,12c-6.627,0 -12,5.373 -12,12v72c0,6.627 5.373,12 12,12h0c6.627,0 12,-5.373 12,-12V24c0,-6.627 -5.373,-12 -12,-12zM54,36c-8.837,0 -16,7.163 -16,16v48h32V52c0,-8.837 -7.163,-16 -16,-16z"/>
|
||||
<path
|
||||
android:fillColor="#6200EE"
|
||||
android:pathData="M54,28c-5.523,0 -10,4.477 -10,10v12h20V38c0,-5.523 -4.477,-10 -10,-10zM54,92c-3.039,0 -5.5,-2.461 -5.5,-5.5v-2h11v2c0,3.039 -2.461,5.5 -5.5,5.5z"/>
|
||||
</vector>
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@color/primary_color"/>
|
||||
<foreground android:drawable="@drawable/ic_notification_foreground"/>
|
||||
</adaptive-icon>
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportWidth="108"
|
||||
android:viewportHeight="108">
|
||||
<path
|
||||
android:fillColor="#FFFFFF"
|
||||
android:pathData="M54,12c-6.627,0 -12,5.373 -12,12v72c0,6.627 5.373,12 12,12h0c6.627,0 12,-5.373 12,-12V24c0,-6.627 -5.373,-12 -12,-12zM54,36c-8.837,0 -16,7.163 -16,16v48h32V52c0,-8.837 -7.163,-16 -16,-16z"/>
|
||||
<path
|
||||
android:fillColor="#6200EE"
|
||||
android:pathData="M54,28c-5.523,0 -10,4.477 -10,10v12h20V38c0,-5.523 -4.477,-10 -10,-10zM54,92c-3.039,0 -5.5,-2.461 -5.5,-5.5v-2h11v2c0,3.039 -2.461,5.5 -5.5,5.5z"/>
|
||||
</vector>
|
||||
15
native-route/android/app/src/main/res/values/colors.xml
Normal file
15
native-route/android/app/src/main/res/values/colors.xml
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="primary_color">#6200EE</color>
|
||||
<color name="primary_dark">#3700B3</color>
|
||||
<color name="primary_light">#BB86FC</color>
|
||||
<color name="accent_color">#03DAC6</color>
|
||||
<color name="notification_icon">#6200EE</color>
|
||||
<color name="notification_critical">#FF1744</color>
|
||||
<color name="notification_low">#4CAF50</color>
|
||||
<color name="notification_normal">#2196F3</color>
|
||||
<color name="white">#FFFFFF</color>
|
||||
<color name="black">#000000</color>
|
||||
<color name="gray">#757575</color>
|
||||
<color name="light_gray">#F5F5F5</color>
|
||||
</resources>
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<drawable name="ic_notification">@drawable/ic_notification</drawable>
|
||||
<drawable name="ic_launcher">@drawable/ic_launcher</drawable>
|
||||
</resources>
|
||||
12
native-route/android/app/src/main/res/values/strings.xml
Normal file
12
native-route/android/app/src/main/res/values/strings.xml
Normal file
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">RSSuper</string>
|
||||
<string name="notification_channel_title">RSSuper Notifications</string>
|
||||
<string name="notification_channel_description">RSSuper notification notifications</string>
|
||||
<string name="notification_channel_critical_title">Critical</string>
|
||||
<string name="notification_channel_critical_description">Critical RSSuper notifications</string>
|
||||
<string name="notification_channel_low_title">Low Priority</string>
|
||||
<string name="notification_channel_low_description">Low priority RSSuper notifications</string>
|
||||
<string name="notification_open">Open RSSuper</string>
|
||||
<string name="notification_mark_read">Mark as read</string>
|
||||
</resources>
|
||||
10
native-route/android/app/src/main/res/values/styles.xml
Normal file
10
native-route/android/app/src/main/res/values/styles.xml
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<style name="Theme.RSSuper" parent="Theme.MaterialComponents.Light.NoActionBar">
|
||||
<item name="colorPrimary">@color/primary_color</item>
|
||||
<item name="colorPrimaryDark">@color/primary_dark</item>
|
||||
<item name="colorAccent">@color/accent_color</item>
|
||||
<item name="android:statusBarColor">@color/primary_dark</item>
|
||||
<item name="android:navigationBarColor">@color/white</item>
|
||||
</style>
|
||||
</resources>
|
||||
@@ -0,0 +1,168 @@
|
||||
package com.rssuper
|
||||
|
||||
import android.content.Context
|
||||
import androidx.test.core.app.ApplicationTestCase
|
||||
import androidx.work_testing.FakeWorkManagerConfiguration
|
||||
import androidx.work_testing.TestDriver
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
/**
|
||||
* SyncWorkerTests - Unit tests for SyncWorker
|
||||
*/
|
||||
class SyncWorkerTests : ApplicationTestCase<Context>() {
|
||||
|
||||
private lateinit var context: Context
|
||||
private lateinit var syncScheduler: SyncScheduler
|
||||
|
||||
override val packageName: String get() = "com.rssuper"
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
context = getTargetContext()
|
||||
syncScheduler = SyncScheduler(context)
|
||||
|
||||
// Clear any existing sync state
|
||||
syncScheduler.resetSyncSchedule()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSyncScheduler_initialState() {
|
||||
// Test initial state
|
||||
assertNull("Last sync date should be null initially", syncScheduler.lastSyncDate)
|
||||
assertEquals(
|
||||
"Default sync interval should be 6 hours",
|
||||
SyncConfiguration.DEFAULT_SYNC_INTERVAL_HOURS,
|
||||
syncScheduler.preferredSyncIntervalHours
|
||||
)
|
||||
assertTrue("Sync should be due initially", syncScheduler.isSyncDue)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSyncScheduler_updateSyncInterval_withFewFeeds() {
|
||||
// Test with few feeds (high frequency)
|
||||
syncScheduler.updateSyncInterval(5, UserActivityLevel.HIGH)
|
||||
|
||||
assertTrue(
|
||||
"Sync interval should be reduced for few feeds with high activity",
|
||||
syncScheduler.preferredSyncIntervalHours <= 2
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSyncScheduler_updateSyncInterval_withManyFeeds() {
|
||||
// Test with many feeds (lower frequency)
|
||||
syncScheduler.updateSyncInterval(500, UserActivityLevel.LOW)
|
||||
|
||||
assertTrue(
|
||||
"Sync interval should be increased for many feeds with low activity",
|
||||
syncScheduler.preferredSyncIntervalHours >= 24
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSyncScheduler_updateSyncInterval_clampsToMax() {
|
||||
// Test that interval is clamped to maximum
|
||||
syncScheduler.updateSyncInterval(1000, UserActivityLevel.LOW)
|
||||
|
||||
assertTrue(
|
||||
"Sync interval should not exceed maximum",
|
||||
syncScheduler.preferredSyncIntervalHours <= SyncConfiguration.MAXIMUM_SYNC_INTERVAL_HOURS
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSyncScheduler_isSyncDue_afterUpdate() {
|
||||
// Simulate a sync by setting last sync date
|
||||
syncScheduler.getSharedPreferences(
|
||||
SyncConfiguration.PREFS_NAME,
|
||||
Context.MODE_PRIVATE
|
||||
).edit()
|
||||
.putLong(SyncConfiguration.PREF_LAST_SYNC_DATE, System.currentTimeMillis())
|
||||
.apply()
|
||||
|
||||
assertFalse("Sync should not be due immediately after sync", syncScheduler.isSyncDue)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSyncScheduler_resetSyncSchedule() {
|
||||
// Set some state
|
||||
syncScheduler.preferredSyncIntervalHours = 12
|
||||
syncScheduler.getSharedPreferences(
|
||||
SyncConfiguration.PREFS_NAME,
|
||||
Context.MODE_PRIVATE
|
||||
).edit()
|
||||
.putLong(SyncConfiguration.PREF_LAST_SYNC_DATE, System.currentTimeMillis())
|
||||
.apply()
|
||||
|
||||
// Reset
|
||||
syncScheduler.resetSyncSchedule()
|
||||
|
||||
// Verify reset
|
||||
assertNull("Last sync date should be null after reset", syncScheduler.lastSyncDate)
|
||||
assertEquals(
|
||||
"Sync interval should be reset to default",
|
||||
SyncConfiguration.DEFAULT_SYNC_INTERVAL_HOURS,
|
||||
syncScheduler.preferredSyncIntervalHours
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testUserActivityLevel_calculation_highActivity() {
|
||||
val activityLevel = UserActivityLevel.calculate(dailyOpenCount = 10, lastOpenedAgoSeconds = 60)
|
||||
assertEquals("Should be HIGH activity", UserActivityLevel.HIGH, activityLevel)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testUserActivityLevel_calculation_mediumActivity() {
|
||||
val activityLevel = UserActivityLevel.calculate(dailyOpenCount = 3, lastOpenedAgoSeconds = 3600)
|
||||
assertEquals("Should be MEDIUM activity", UserActivityLevel.MEDIUM, activityLevel)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testUserActivityLevel_calculation_lowActivity() {
|
||||
val activityLevel = UserActivityLevel.calculate(dailyOpenCount = 0, lastOpenedAgoSeconds = 86400 * 7)
|
||||
assertEquals("Should be LOW activity", UserActivityLevel.LOW, activityLevel)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSyncConfiguration_createPeriodicWorkRequest() {
|
||||
val workRequest = SyncConfiguration.createPeriodicWorkRequest(context)
|
||||
|
||||
assertNotNull("Work request should not be null", workRequest)
|
||||
assertEquals(
|
||||
"Interval should be default (6 hours)",
|
||||
SyncConfiguration.DEFAULT_SYNC_INTERVAL_HOURS,
|
||||
workRequest.intervalDuration,
|
||||
TimeUnit.HOURS
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSyncConfiguration_createPeriodicWorkRequest_customInterval() {
|
||||
val workRequest = SyncConfiguration.createPeriodicWorkRequest(context, 12)
|
||||
|
||||
assertEquals(
|
||||
"Interval should be custom (12 hours)",
|
||||
12L,
|
||||
workRequest.intervalDuration,
|
||||
TimeUnit.HOURS
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSyncConfiguration_constraints() {
|
||||
val defaultConstraints = SyncConfiguration.getDefaultConstraints()
|
||||
val strictConstraints = SyncConfiguration.getStrictConstraints()
|
||||
|
||||
// Default constraints should require network but not charging
|
||||
assertTrue("Default constraints should require network", defaultConstraints.requiredNetworkType != androidx.work.NetworkType.NOT_REQUIRED)
|
||||
assertFalse("Default constraints should not require charging", defaultConstraints.requiresCharging)
|
||||
|
||||
// Strict constraints should require Wi-Fi and charging
|
||||
assertEquals("Strict constraints should require Wi-Fi", androidx.work.NetworkType.UNMETERED, strictConstraints.requiredNetworkType)
|
||||
assertTrue("Strict constraints should require charging", strictConstraints.requiresCharging)
|
||||
}
|
||||
}
|
||||
120
native-route/ios/RSSuper/AppDelegate.swift
Normal file
120
native-route/ios/RSSuper/AppDelegate.swift
Normal file
@@ -0,0 +1,120 @@
|
||||
import UIKit
|
||||
import UserNotifications
|
||||
|
||||
@main
|
||||
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
|
||||
|
||||
var notificationManager: NotificationManager?
|
||||
var notificationPreferencesStore: NotificationPreferencesStore?
|
||||
|
||||
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
|
||||
// Initialize notification manager
|
||||
notificationManager = NotificationManager.shared
|
||||
notificationPreferencesStore = NotificationPreferencesStore.shared
|
||||
|
||||
// Initialize notification manager
|
||||
notificationManager?.initialize()
|
||||
|
||||
// Set up notification center delegate
|
||||
UNUserNotificationCenter.current().delegate = self
|
||||
|
||||
// Update badge count when app comes to foreground
|
||||
notificationCenter.addObserver(
|
||||
self,
|
||||
selector: #selector(updateBadgeCount),
|
||||
name: Notification.Name("badgeUpdate"),
|
||||
object: nil
|
||||
)
|
||||
|
||||
print("AppDelegate: App launched")
|
||||
return true
|
||||
}
|
||||
|
||||
/// Update badge count when app comes to foreground
|
||||
@objc func updateBadgeCount() {
|
||||
if let count = notificationManager?.unreadCount() {
|
||||
print("Badge count updated: \(count)")
|
||||
}
|
||||
}
|
||||
|
||||
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
|
||||
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
|
||||
}
|
||||
|
||||
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
|
||||
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
|
||||
}
|
||||
|
||||
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
|
||||
print("Scene sessions discarded")
|
||||
}
|
||||
|
||||
// MARK: - Notification Center Delegate
|
||||
|
||||
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
|
||||
// Get notification content
|
||||
let content = notification.content
|
||||
|
||||
// Determine presentation options based on urgency
|
||||
let category = content.categoryIdentifier
|
||||
let options: UNNotificationPresentationOptions = [
|
||||
.banner,
|
||||
.sound,
|
||||
.badge
|
||||
]
|
||||
|
||||
if category == "Critical" {
|
||||
options.insert(.criticalAlert)
|
||||
options.insert(.sound)
|
||||
} else if category == "Low Priority" {
|
||||
options.remove(.sound)
|
||||
} else {
|
||||
options.remove(.sound)
|
||||
}
|
||||
|
||||
completionHandler(options)
|
||||
}
|
||||
|
||||
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
|
||||
// Handle notification click
|
||||
let action = response.action
|
||||
let identifier = action.identifier
|
||||
|
||||
print("Notification clicked: \(identifier)")
|
||||
|
||||
// Open app when notification is clicked
|
||||
if identifier == "openApp" {
|
||||
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
|
||||
let window = windowScene.windows.first
|
||||
window?.makeKeyAndVisible()
|
||||
}
|
||||
}
|
||||
|
||||
completionHandler()
|
||||
}
|
||||
|
||||
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
|
||||
// Handle notification click
|
||||
let action = response.action
|
||||
let identifier = action.identifier
|
||||
|
||||
print("Notification clicked: \(identifier)")
|
||||
|
||||
// Open app when notification is clicked
|
||||
if identifier == "openApp" {
|
||||
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
|
||||
let window = windowScene.windows.first
|
||||
window?.makeKeyAndVisible()
|
||||
}
|
||||
}
|
||||
|
||||
completionHandler()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Notification Center Extension
|
||||
|
||||
extension Notification.Name {
|
||||
static let badgeUpdate = Notification.Name("badgeUpdate")
|
||||
}
|
||||
|
||||
234
native-route/ios/RSSuper/BackgroundSyncService.swift
Normal file
234
native-route/ios/RSSuper/BackgroundSyncService.swift
Normal file
@@ -0,0 +1,234 @@
|
||||
import Foundation
|
||||
import BackgroundTasks
|
||||
|
||||
/// Main background sync service coordinator
|
||||
/// Orchestrates background feed synchronization using BGTaskScheduler
|
||||
final class BackgroundSyncService {
|
||||
// MARK: - Singleton
|
||||
|
||||
static let shared = BackgroundSyncService()
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
/// Identifier for the background refresh task
|
||||
static let backgroundRefreshIdentifier = "com.rssuper.backgroundRefresh"
|
||||
|
||||
/// Identifier for the periodic sync task
|
||||
static let periodicSyncIdentifier = "com.rssuper.periodicSync"
|
||||
|
||||
private let syncScheduler: SyncScheduler
|
||||
private let syncWorker: SyncWorker
|
||||
|
||||
/// Current sync state
|
||||
private var isSyncing: Bool = false
|
||||
|
||||
/// Last successful sync date
|
||||
var lastSyncDate: Date?
|
||||
|
||||
/// Pending feeds count
|
||||
var pendingFeedsCount: Int = 0
|
||||
|
||||
// MARK: - Initialization
|
||||
|
||||
private init() {
|
||||
self.syncScheduler = SyncScheduler()
|
||||
self.syncWorker = SyncWorker()
|
||||
}
|
||||
|
||||
// MARK: - Public API
|
||||
|
||||
/// Register background tasks with the system
|
||||
func registerBackgroundTasks() {
|
||||
// Register app refresh task
|
||||
BGTaskScheduler.shared.register(forTaskWithIdentifier: Self.backgroundRefreshIdentifier,
|
||||
with: nil) { task in
|
||||
self.handleBackgroundTask(task)
|
||||
}
|
||||
|
||||
// Register periodic sync task (if available on device)
|
||||
BGTaskScheduler.shared.register(forTaskIdentifier: Self.periodicSyncIdentifier,
|
||||
with: nil) { task in
|
||||
self.handlePeriodicSync(task)
|
||||
}
|
||||
|
||||
print("✓ Background tasks registered")
|
||||
}
|
||||
|
||||
/// Schedule a background refresh task
|
||||
func scheduleBackgroundRefresh() -> Bool {
|
||||
guard !isSyncing else {
|
||||
print("⚠️ Sync already in progress")
|
||||
return false
|
||||
}
|
||||
|
||||
let taskRequest = BGAppRefreshTaskRequest(identifier: Self.backgroundRefreshIdentifier)
|
||||
|
||||
// Schedule between 15 minutes and 4 hours from now
|
||||
taskRequest.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60)
|
||||
|
||||
// Set retry interval (minimum 15 minutes)
|
||||
taskRequest.requiredReasons = [.networkAvailable]
|
||||
|
||||
do {
|
||||
try BGTaskScheduler.shared.submit(taskRequest)
|
||||
print("✓ Background refresh scheduled")
|
||||
return true
|
||||
} catch {
|
||||
print("❌ Failed to schedule background refresh: \(error)")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/// Schedule periodic sync (iOS 13+) with custom interval
|
||||
func schedulePeriodicSync(interval: TimeInterval = 6 * 3600) -> Bool {
|
||||
guard !isSyncing else {
|
||||
print("⚠️ Sync already in progress")
|
||||
return false
|
||||
}
|
||||
|
||||
let taskRequest = BGProcessingTaskRequest(identifier: Self.periodicSyncIdentifier)
|
||||
taskRequest.requiresNetworkConnectivity = true
|
||||
taskRequest.requiresExternalPower = false // Allow on battery
|
||||
taskRequest.minimumInterval = interval
|
||||
|
||||
do {
|
||||
try BGTaskScheduler.shared.submit(taskRequest)
|
||||
print("✓ Periodic sync scheduled (interval: \(interval/3600)h)")
|
||||
return true
|
||||
} catch {
|
||||
print("❌ Failed to schedule periodic sync: \(error)")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/// Cancel all pending background tasks
|
||||
func cancelAllPendingTasks() {
|
||||
BGTaskScheduler.shared.cancelAllTaskRequests()
|
||||
print("✓ All pending background tasks cancelled")
|
||||
}
|
||||
|
||||
/// Get pending task requests
|
||||
func getPendingTaskRequests() async -> [BGTaskScheduler.PendingTaskRequest] {
|
||||
do {
|
||||
let requests = try await BGTaskScheduler.shared.pendingTaskRequests()
|
||||
return requests
|
||||
} catch {
|
||||
print("❌ Failed to get pending tasks: \(error)")
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
/// Force immediate sync (for testing or user-initiated)
|
||||
func forceSync() async {
|
||||
guard !isSyncing else {
|
||||
print("⚠️ Sync already in progress")
|
||||
return
|
||||
}
|
||||
|
||||
isSyncing = true
|
||||
|
||||
do {
|
||||
let result = try await syncWorker.performSync()
|
||||
lastSyncDate = Date()
|
||||
|
||||
print("✓ Force sync completed: \(result.feedsSynced) feeds, \(result.articlesFetched) articles")
|
||||
|
||||
// Schedule next background refresh
|
||||
scheduleBackgroundRefresh()
|
||||
|
||||
} catch {
|
||||
print("❌ Force sync failed: \(error)")
|
||||
}
|
||||
|
||||
isSyncing = false
|
||||
}
|
||||
|
||||
/// Check if background tasks are enabled
|
||||
func areBackgroundTasksEnabled() -> Bool {
|
||||
// Check if Background Modes capability is enabled
|
||||
// This is a basic check; more sophisticated checks can be added
|
||||
return true
|
||||
}
|
||||
|
||||
// MARK: - Private Methods
|
||||
|
||||
/// Handle background app refresh task
|
||||
private func handleBackgroundTask(_ task: BGTask) {
|
||||
guard let appRefreshTask = task as? BGAppRefreshTask else {
|
||||
print("❌ Unexpected task type")
|
||||
task.setTaskCompleted(success: false)
|
||||
return
|
||||
}
|
||||
|
||||
print("🔄 Background refresh task started (expiration: \(appRefreshTask.expirationDate))")
|
||||
|
||||
isSyncing = true
|
||||
|
||||
Task(priority: .userInitiated) {
|
||||
do {
|
||||
let result = try await syncWorker.performSync()
|
||||
|
||||
lastSyncDate = Date()
|
||||
|
||||
print("✓ Background refresh completed: \(result.feedsSynced) feeds, \(result.articlesFetched) articles")
|
||||
|
||||
// Re-schedule the task
|
||||
scheduleBackgroundRefresh()
|
||||
|
||||
task.setTaskCompleted(success: true)
|
||||
|
||||
} catch {
|
||||
print("❌ Background refresh failed: \(error)")
|
||||
task.setTaskCompleted(success: false, retryAttempted: true)
|
||||
}
|
||||
|
||||
isSyncing = false
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle periodic sync task
|
||||
private func handlePeriodicSync(_ task: BGTask) {
|
||||
guard let processingTask = task as? BGProcessingTask else {
|
||||
print("❌ Unexpected task type")
|
||||
task.setTaskCompleted(success: false)
|
||||
return
|
||||
}
|
||||
|
||||
print("🔄 Periodic sync task started (expiration: \(processingTask.expirationDate))")
|
||||
|
||||
isSyncing = true
|
||||
|
||||
Task(priority: .utility) {
|
||||
do {
|
||||
let result = try await syncWorker.performSync()
|
||||
|
||||
lastSyncDate = Date()
|
||||
|
||||
print("✓ Periodic sync completed: \(result.feedsSynced) feeds, \(result.articlesFetched) articles")
|
||||
|
||||
task.setTaskCompleted(success: true)
|
||||
|
||||
} catch {
|
||||
print("❌ Periodic sync failed: \(error)")
|
||||
task.setTaskCompleted(success: false, retryAttempted: true)
|
||||
}
|
||||
|
||||
isSyncing = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SyncResult
|
||||
|
||||
/// Result of a sync operation
|
||||
struct SyncResult {
|
||||
let feedsSynced: Int
|
||||
let articlesFetched: Int
|
||||
let errors: [Error]
|
||||
|
||||
init(feedsSynced: Int = 0, articlesFetched: Int = 0, errors: [Error] = []) {
|
||||
self.feedsSynced = feedsSynced
|
||||
self.articlesFetched = articlesFetched
|
||||
self.errors = errors
|
||||
}
|
||||
}
|
||||
55
native-route/ios/RSSuper/Info.plist
Normal file
55
native-route/ios/RSSuper/Info.plist
Normal file
@@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>$(PRODUCT_NAME)</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
<key>NSLocationWhenInUseUsageDescription</key>
|
||||
<string>We need your location to provide nearby feed updates.</string>
|
||||
<key>NSUserNotificationsUsageDescription</key>
|
||||
<string>We need permission to send you RSSuper notifications for new articles and feed updates.</string>
|
||||
<key>UIBackgroundModes</key>
|
||||
<array>
|
||||
<string>fetch</string>
|
||||
<string>remote-notification</string>
|
||||
</array>
|
||||
<key>UILaunchScreen</key>
|
||||
<dict>
|
||||
<key>UIColorName</key>
|
||||
<string>primary</string>
|
||||
<key>UIImageName</key>
|
||||
<string>logo</string>
|
||||
</dict>
|
||||
<key>UIRequiresFullScreen</key>
|
||||
<false/>
|
||||
<key>UIStatusBarHidden</key>
|
||||
<false/>
|
||||
<key>UISupportedInterfaceOrientations</key>
|
||||
<array>
|
||||
<string>UIInterfaceOrientationPortrait</string>
|
||||
<string>UIInterfaceOrientationLandscapeLeft</string>
|
||||
<string>UIInterfaceOrientationLandscapeRight</string>
|
||||
</array>
|
||||
<key>UISupportedInterfaceOrientations~ipad</key>
|
||||
<array>
|
||||
<string>UIInterfaceOrientationPortrait</string>
|
||||
<string>UIInterfaceOrientationPortraitUpsideDown</string>
|
||||
<string>UIInterfaceOrientationLandscapeLeft</string>
|
||||
<string>UIInterfaceOrientationLandscapeRight</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
109
native-route/ios/RSSuper/RefreshFeedsAppIntent.swift
Normal file
109
native-route/ios/RSSuper/RefreshFeedsAppIntent.swift
Normal file
@@ -0,0 +1,109 @@
|
||||
import Foundation
|
||||
import AppIntents
|
||||
|
||||
/// AppIntent for background feed refresh
|
||||
/// Allows users to create Shortcuts for manual feed refresh
|
||||
struct RefreshFeedsAppIntent: AppIntent {
|
||||
static var title: LocalizedStringResource {
|
||||
"Refresh Feeds"
|
||||
}
|
||||
|
||||
static var description: LocalizedStringResource {
|
||||
"Manually refresh all subscribed feeds"
|
||||
}
|
||||
|
||||
static var intentIdentifier: String {
|
||||
"refreshFeeds"
|
||||
}
|
||||
|
||||
static var openAppAfterRun: Bool {
|
||||
false // Don't open app after background refresh
|
||||
}
|
||||
|
||||
@Parameter(title: "Refresh All", default: true)
|
||||
var refreshAll: Bool
|
||||
|
||||
@Parameter(title: "Specific Feed", default: "")
|
||||
var feedId: String
|
||||
|
||||
init() {}
|
||||
|
||||
init(refreshAll: Bool, feedId: String) {
|
||||
self.refreshAll = refreshAll
|
||||
self.feedId = feedId
|
||||
}
|
||||
|
||||
func perform() async throws -> RefreshFeedsResult {
|
||||
// Check if we have network connectivity
|
||||
guard await checkNetworkConnectivity() else {
|
||||
return RefreshFeedsResult(
|
||||
status: .failed,
|
||||
message: "No network connectivity",
|
||||
feedsRefreshed: 0
|
||||
)
|
||||
}
|
||||
|
||||
do {
|
||||
if refreshAll {
|
||||
// Refresh all feeds
|
||||
let result = try await BackgroundSyncService.shared.forceSync()
|
||||
|
||||
return RefreshFeedsResult(
|
||||
status: .success,
|
||||
message: "All feeds refreshed",
|
||||
feedsRefreshed: result.feedsSynced
|
||||
)
|
||||
} else if !feedId.isEmpty {
|
||||
// Refresh specific feed
|
||||
let result = try await BackgroundSyncService.shared.performPartialSync(
|
||||
subscriptionIds: [feedId]
|
||||
)
|
||||
|
||||
return RefreshFeedsResult(
|
||||
status: .success,
|
||||
message: "Feed refreshed",
|
||||
feedsRefreshed: result.feedsSynced
|
||||
)
|
||||
} else {
|
||||
return RefreshFeedsResult(
|
||||
status: .failed,
|
||||
message: "No feed specified",
|
||||
feedsRefreshed: 0
|
||||
)
|
||||
}
|
||||
|
||||
} catch {
|
||||
return RefreshFeedsResult(
|
||||
status: .failed,
|
||||
message: error.localizedDescription,
|
||||
feedsRefreshed: 0
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private func checkNetworkConnectivity() async -> Bool {
|
||||
// TODO: Implement actual network connectivity check
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
/// Result of RefreshFeedsAppIntent
|
||||
struct RefreshFeedsResult: AppIntentResult {
|
||||
enum Status: String, Codable {
|
||||
case success
|
||||
case failed
|
||||
}
|
||||
|
||||
var status: Status
|
||||
var message: String
|
||||
var feedsRefreshed: Int
|
||||
|
||||
var title: String {
|
||||
switch status {
|
||||
case .success:
|
||||
return "✓ Refreshed \(feedsRefreshed) feed(s)"
|
||||
case .failed:
|
||||
return "✗ Refresh failed: \(message)"
|
||||
}
|
||||
}
|
||||
}
|
||||
209
native-route/ios/RSSuper/Services/NotificationManager.swift
Normal file
209
native-route/ios/RSSuper/Services/NotificationManager.swift
Normal file
@@ -0,0 +1,209 @@
|
||||
import UserNotifications
|
||||
import Foundation
|
||||
import Combine
|
||||
|
||||
/// Notification manager for iOS RSSuper
|
||||
/// Coordinates notifications, badge management, and preference storage
|
||||
class NotificationManager {
|
||||
|
||||
static let shared = NotificationManager()
|
||||
|
||||
private let notificationService = NotificationService.shared
|
||||
private let notificationCenter = NotificationCenter.default
|
||||
private let defaultBadgeIcon: String = "rssuper-icon"
|
||||
|
||||
private var unreadCount = 0
|
||||
private var badgeVisible = true
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
|
||||
private init() {}
|
||||
|
||||
/// Initialize the notification manager
|
||||
func initialize() {
|
||||
notificationService.initialize(self)
|
||||
loadBadgeCount()
|
||||
|
||||
// Set up badge visibility
|
||||
if badgeVisible {
|
||||
showBadge()
|
||||
} else {
|
||||
hideBadge()
|
||||
}
|
||||
|
||||
print("NotificationManager initialized")
|
||||
}
|
||||
|
||||
/// Load saved badge count
|
||||
private func loadBadgeCount() {
|
||||
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
|
||||
|
||||
if let count = appDelegate.notificationManager?.badgeCount {
|
||||
self.unreadCount = count
|
||||
updateBadgeLabel(label: String(count))
|
||||
}
|
||||
}
|
||||
|
||||
/// Show badge
|
||||
func showBadge() {
|
||||
guard badgeVisible else { return }
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self.notificationCenter.post(name: .badgeUpdate, object: nil)
|
||||
}
|
||||
|
||||
print("Badge shown")
|
||||
}
|
||||
|
||||
/// Hide badge
|
||||
func hideBadge() {
|
||||
DispatchQueue.main.async {
|
||||
self.notificationCenter.post(name: .badgeUpdate, object: nil)
|
||||
}
|
||||
|
||||
print("Badge hidden")
|
||||
}
|
||||
|
||||
/// Update badge with count
|
||||
func updateBadge(label: String) {
|
||||
DispatchQueue.main.async {
|
||||
self.updateBadgeLabel(label: label)
|
||||
}
|
||||
}
|
||||
|
||||
/// Update badge label
|
||||
private func updateBadgeLabel(label: String) {
|
||||
let badge = UNNotificationBadgeManager()
|
||||
badge.badgeCount = Int(label) ?? 0
|
||||
badge.badgeIcon = defaultBadgeIcon
|
||||
badge.badgePosition = .center
|
||||
badge.badgeBackground = UIColor.systemBackground
|
||||
badge.badgeText = label
|
||||
badge.badgeTextColor = .black
|
||||
badge.badgeFont = .preferredFont(forTextStyle: .body)
|
||||
badge.badgeCornerRadius = 0
|
||||
badge.badgeBorder = nil
|
||||
badge.badgeShadow = nil
|
||||
badge.badgeCornerRadius = 0
|
||||
badge.badgeBorder = nil
|
||||
badge.badgeShadow = nil
|
||||
badge.badgeCornerRadius = 0
|
||||
badge.badgeBorder = nil
|
||||
badge.badgeShadow = nil
|
||||
badge.badgeCornerRadius = 0
|
||||
badge.badgeBorder = nil
|
||||
badge.badgeShadow = nil
|
||||
badge.badgeCornerRadius = 0
|
||||
badge.badgeBorder = nil
|
||||
badge.badgeShadow = nil
|
||||
badge.badgeCornerRadius = 0
|
||||
badge.badgeBorder = nil
|
||||
badge.badgeShadow = nil
|
||||
badge.badgeCornerRadius = 0
|
||||
badge.badgeBorder = nil
|
||||
badge.badgeShadow = nil
|
||||
}
|
||||
|
||||
/// Set unread count
|
||||
func setUnreadCount(_ count: Int) {
|
||||
unreadCount = count
|
||||
|
||||
// Update badge
|
||||
if count > 0 {
|
||||
showBadge()
|
||||
} else {
|
||||
hideBadge()
|
||||
}
|
||||
|
||||
// Update badge label
|
||||
updateBadge(label: String(count))
|
||||
}
|
||||
|
||||
/// Clear unread count
|
||||
func clearUnreadCount() {
|
||||
unreadCount = 0
|
||||
hideBadge()
|
||||
updateBadge(label: "0")
|
||||
}
|
||||
|
||||
/// Get unread count
|
||||
func unreadCount() -> Int {
|
||||
return unreadCount
|
||||
}
|
||||
|
||||
/// Get badge visibility
|
||||
func badgeVisibility() -> Bool {
|
||||
return badgeVisible
|
||||
}
|
||||
|
||||
/// Set badge visibility
|
||||
func setBadgeVisibility(_ visible: Bool) {
|
||||
badgeVisible = visible
|
||||
|
||||
if visible {
|
||||
showBadge()
|
||||
} else {
|
||||
hideBadge()
|
||||
}
|
||||
}
|
||||
|
||||
/// Show notification with badge
|
||||
func showWithBadge(title: String, body: String, icon: String, urgency: NotificationUrgency) {
|
||||
let notification = notificationService.showNotification(
|
||||
title: title,
|
||||
body: body,
|
||||
icon: icon,
|
||||
urgency: urgency
|
||||
)
|
||||
|
||||
if unreadCount == 0 {
|
||||
showBadge()
|
||||
}
|
||||
}
|
||||
|
||||
/// Show notification without badge
|
||||
func showWithoutBadge(title: String, body: String, icon: String, urgency: NotificationUrgency) {
|
||||
let notification = notificationService.showNotification(
|
||||
title: title,
|
||||
body: body,
|
||||
icon: icon,
|
||||
urgency: urgency
|
||||
)
|
||||
}
|
||||
|
||||
/// Show critical notification
|
||||
func showCritical(title: String, body: String, icon: String) {
|
||||
showWithBadge(title: title, body: body, icon: icon, urgency: .critical)
|
||||
}
|
||||
|
||||
/// Show low priority notification
|
||||
func showLow(title: String, body: String, icon: String) {
|
||||
showWithBadge(title: title, body: body, icon: icon, urgency: .low)
|
||||
}
|
||||
|
||||
/// Show normal notification
|
||||
func showNormal(title: String, body: String, icon: String) {
|
||||
showWithBadge(title: title, body: body, icon: icon, urgency: .normal)
|
||||
}
|
||||
|
||||
/// Get notification service
|
||||
func notificationService() -> NotificationService {
|
||||
return notificationService
|
||||
}
|
||||
|
||||
/// Get notification center
|
||||
func notificationCenter() -> UNUserNotificationCenter {
|
||||
return notificationService.notificationCenter()
|
||||
}
|
||||
|
||||
/// Check if notification manager is available
|
||||
func isAvailable() -> Bool {
|
||||
return notificationService.isAvailable
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Notification Center Extensions
|
||||
|
||||
extension Notification.Name {
|
||||
static let badgeUpdate = Notification.Name("badgeUpdate")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
import Foundation
|
||||
import UserNotifications
|
||||
import Combine
|
||||
|
||||
/// Notification preferences store for iOS RSSuper
|
||||
/// Provides persistent storage for user notification settings
|
||||
class NotificationPreferencesStore {
|
||||
|
||||
static let shared = NotificationPreferencesStore()
|
||||
|
||||
private let defaults = UserDefaults.standard
|
||||
private let prefsKey = "notification_prefs"
|
||||
|
||||
private var preferences: NotificationPreferences?
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
|
||||
private init() {
|
||||
loadPreferences()
|
||||
}
|
||||
|
||||
/// Load saved preferences
|
||||
private func loadPreferences() {
|
||||
guard let json = defaults.string(forKey: prefsKey) else {
|
||||
// Set default preferences
|
||||
preferences = NotificationPreferences()
|
||||
defaults.set(json, forKey: prefsKey)
|
||||
return
|
||||
}
|
||||
|
||||
do {
|
||||
preferences = try JSONDecoder().decode(NotificationPreferences.self, from: Data(json))
|
||||
} catch {
|
||||
print("Failed to decode preferences: \(error)")
|
||||
preferences = NotificationPreferences()
|
||||
}
|
||||
}
|
||||
|
||||
/// Save preferences
|
||||
func savePreferences(_ prefs: NotificationPreferences) {
|
||||
if let json = try? JSONEncoder().encode(prefs) {
|
||||
defaults.set(json, forKey: prefsKey)
|
||||
}
|
||||
preferences = prefs
|
||||
}
|
||||
|
||||
/// Get notification preferences
|
||||
func preferences() -> NotificationPreferences? {
|
||||
return preferences
|
||||
}
|
||||
|
||||
/// Get new articles preference
|
||||
func isNewArticlesEnabled() -> Bool {
|
||||
return preferences?.newArticles ?? true
|
||||
}
|
||||
|
||||
/// Set new articles preference
|
||||
func setNewArticles(_ enabled: Bool) {
|
||||
preferences?.newArticles = enabled
|
||||
savePreferences(preferences ?? NotificationPreferences())
|
||||
}
|
||||
|
||||
/// Get episode releases preference
|
||||
func isEpisodeReleasesEnabled() -> Bool {
|
||||
return preferences?.episodeReleases ?? true
|
||||
}
|
||||
|
||||
/// Set episode releases preference
|
||||
func setEpisodeReleases(_ enabled: Bool) {
|
||||
preferences?.episodeReleases = enabled
|
||||
savePreferences(preferences ?? NotificationPreferences())
|
||||
}
|
||||
|
||||
/// Get custom alerts preference
|
||||
func isCustomAlertsEnabled() -> Bool {
|
||||
return preferences?.customAlerts ?? true
|
||||
}
|
||||
|
||||
/// Set custom alerts preference
|
||||
func setCustomAlerts(_ enabled: Bool) {
|
||||
preferences?.customAlerts = enabled
|
||||
savePreferences(preferences ?? NotificationPreferences())
|
||||
}
|
||||
|
||||
/// Get badge count preference
|
||||
func isBadgeCountEnabled() -> Bool {
|
||||
return preferences?.badgeCount ?? true
|
||||
}
|
||||
|
||||
/// Set badge count preference
|
||||
func setBadgeCount(_ enabled: Bool) {
|
||||
preferences?.badgeCount = enabled
|
||||
savePreferences(preferences ?? NotificationPreferences())
|
||||
}
|
||||
|
||||
/// Get sound preference
|
||||
func isSoundEnabled() -> Bool {
|
||||
return preferences?.sound ?? true
|
||||
}
|
||||
|
||||
/// Set sound preference
|
||||
func setSound(_ enabled: Bool) {
|
||||
preferences?.sound = enabled
|
||||
savePreferences(preferences ?? NotificationPreferences())
|
||||
}
|
||||
|
||||
/// Get vibration preference
|
||||
func isVibrationEnabled() -> Bool {
|
||||
return preferences?.vibration ?? true
|
||||
}
|
||||
|
||||
/// Set vibration preference
|
||||
func setVibration(_ enabled: Bool) {
|
||||
preferences?.vibration = enabled
|
||||
savePreferences(preferences ?? NotificationPreferences())
|
||||
}
|
||||
|
||||
/// Enable all notifications
|
||||
func enableAll() {
|
||||
preferences = NotificationPreferences()
|
||||
savePreferences(preferences ?? NotificationPreferences())
|
||||
}
|
||||
|
||||
/// Disable all notifications
|
||||
func disableAll() {
|
||||
preferences = NotificationPreferences(
|
||||
newArticles: false,
|
||||
episodeReleases: false,
|
||||
customAlerts: false,
|
||||
badgeCount: false,
|
||||
sound: false,
|
||||
vibration: false
|
||||
)
|
||||
savePreferences(preferences ?? NotificationPreferences())
|
||||
}
|
||||
|
||||
/// Get all preferences as dictionary
|
||||
func allPreferences() -> [String: Bool] {
|
||||
guard let prefs = preferences else {
|
||||
return [:]
|
||||
}
|
||||
|
||||
return [
|
||||
"newArticles": prefs.newArticles,
|
||||
"episodeReleases": prefs.episodeReleases,
|
||||
"customAlerts": prefs.customAlerts,
|
||||
"badgeCount": prefs.badgeCount,
|
||||
"sound": prefs.sound,
|
||||
"vibration": prefs.vibration
|
||||
]
|
||||
}
|
||||
|
||||
/// Set all preferences from dictionary
|
||||
func setAllPreferences(_ prefs: [String: Bool]) {
|
||||
let notificationPrefs = NotificationPreferences(
|
||||
newArticles: prefs["newArticles"] ?? true,
|
||||
episodeReleases: prefs["episodeReleases"] ?? true,
|
||||
customAlerts: prefs["customAlerts"] ?? true,
|
||||
badgeCount: prefs["badgeCount"] ?? true,
|
||||
sound: prefs["sound"] ?? true,
|
||||
vibration: prefs["vibration"] ?? true
|
||||
)
|
||||
|
||||
preferences = notificationPrefs
|
||||
defaults.set(try? JSONEncoder().encode(notificationPrefs), forKey: prefsKey)
|
||||
}
|
||||
|
||||
/// Get preferences key
|
||||
func prefsKey() -> String {
|
||||
return prefsKey
|
||||
}
|
||||
}
|
||||
|
||||
/// Notification preferences data class
|
||||
@objcMembers
|
||||
struct NotificationPreferences: Codable {
|
||||
var newArticles: Bool = true
|
||||
var episodeReleases: Bool = true
|
||||
var customAlerts: Bool = true
|
||||
var badgeCount: Bool = true
|
||||
var sound: Bool = true
|
||||
var vibration: Bool = true
|
||||
}
|
||||
|
||||
276
native-route/ios/RSSuper/Services/NotificationService.swift
Normal file
276
native-route/ios/RSSuper/Services/NotificationService.swift
Normal file
@@ -0,0 +1,276 @@
|
||||
import UserNotifications
|
||||
import Foundation
|
||||
|
||||
/// Main notification service for iOS RSSuper
|
||||
/// Handles push and local notifications using UserNotifications framework
|
||||
class NotificationService {
|
||||
|
||||
static let shared = NotificationService()
|
||||
|
||||
private let unuserNotifications = UNUserNotificationCenter.current()
|
||||
private let notificationCenter = NotificationCenter.default
|
||||
private let defaultNotificationCategory = "Default"
|
||||
private let criticalNotificationCategory = "Critical"
|
||||
private let lowPriorityNotificationCategory = "Low Priority"
|
||||
|
||||
private let defaultIcon: String = "rssuper-icon"
|
||||
private let criticalIcon: String = "rssuper-icon"
|
||||
private let lowPriorityIcon: String = "rssuper-icon"
|
||||
|
||||
private let defaultTitle: String = "RSSuper"
|
||||
|
||||
private var isInitialized = false
|
||||
|
||||
private init() {}
|
||||
|
||||
/// Initialize the notification service
|
||||
/// - Parameter context: Application context for initialization
|
||||
func initialize(_ context: Any) {
|
||||
guard !isInitialized else { return }
|
||||
|
||||
do {
|
||||
// Request authorization
|
||||
try requestAuthorization(context: context)
|
||||
|
||||
// Set default notification settings
|
||||
setDefaultNotificationSettings()
|
||||
|
||||
// Set up notification categories
|
||||
setNotificationCategories()
|
||||
|
||||
isInitialized = true
|
||||
print("NotificationService initialized")
|
||||
} catch {
|
||||
print("Failed to initialize NotificationService: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
/// Request notification authorization
|
||||
/// - Parameter context: Application context
|
||||
private func requestAuthorization(context: Any) throws {
|
||||
let options: UNAuthorizationOptions = [.alert, .sound, .badge]
|
||||
|
||||
switch unuserNotifications.requestAuthorization(options: options) {
|
||||
case .authorized:
|
||||
print("Notification authorization authorized")
|
||||
case .denied:
|
||||
print("Notification authorization denied")
|
||||
case .restricted:
|
||||
print("Notification authorization restricted")
|
||||
case .notDetermined:
|
||||
print("Notification authorization not determined")
|
||||
@unknown default:
|
||||
print("Unknown notification authorization state")
|
||||
}
|
||||
}
|
||||
|
||||
/// Set default notification settings
|
||||
private func setDefaultNotificationSettings() {
|
||||
do {
|
||||
try unuserNotifications.setNotificationCategories([
|
||||
defaultNotificationCategory,
|
||||
criticalNotificationCategory,
|
||||
lowPriorityNotificationCategory
|
||||
], completionHandler: { _, error in
|
||||
if let error = error {
|
||||
print("Failed to set notification categories: \(error)")
|
||||
} else {
|
||||
print("Notification categories set successfully")
|
||||
}
|
||||
})
|
||||
} catch {
|
||||
print("Failed to set default notification settings: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
/// Set notification categories
|
||||
private func setNotificationCategories() {
|
||||
let categories = [
|
||||
UNNotificationCategory(
|
||||
identifier: defaultNotificationCategory,
|
||||
actions: [
|
||||
UNNotificationAction(
|
||||
identifier: "openApp",
|
||||
title: "Open App",
|
||||
options: .foreground
|
||||
),
|
||||
UNNotificationAction(
|
||||
identifier: "markRead",
|
||||
title: "Mark as Read",
|
||||
options: .previewClose
|
||||
)
|
||||
],
|
||||
intentIdentifiers: [],
|
||||
options: .initialDisplayOptions
|
||||
),
|
||||
UNNotificationCategory(
|
||||
identifier: criticalNotificationCategory,
|
||||
actions: [
|
||||
UNNotificationAction(
|
||||
identifier: "openApp",
|
||||
title: "Open App",
|
||||
options: .foreground
|
||||
)
|
||||
],
|
||||
intentIdentifiers: [],
|
||||
options: .criticalAlert
|
||||
),
|
||||
UNNotificationCategory(
|
||||
identifier: lowPriorityNotificationCategory,
|
||||
actions: [
|
||||
UNNotificationAction(
|
||||
identifier: "openApp",
|
||||
title: "Open App",
|
||||
options: .foreground
|
||||
)
|
||||
],
|
||||
intentIdentifiers: [],
|
||||
options: .initialDisplayOptions
|
||||
)
|
||||
]
|
||||
|
||||
do {
|
||||
try unuserNotifications.setNotificationCategories(categories, completionHandler: { _, error in
|
||||
if let error = error {
|
||||
print("Failed to set notification categories: \(error)")
|
||||
} else {
|
||||
print("Notification categories set successfully")
|
||||
}
|
||||
})
|
||||
} catch {
|
||||
print("Failed to set notification categories: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
/// Show a local notification
|
||||
/// - Parameters:
|
||||
/// - title: Notification title
|
||||
/// - body: Notification body
|
||||
/// - icon: Icon name
|
||||
/// - urgency: Notification urgency
|
||||
/// - contentDate: Scheduled content date
|
||||
/// - userInfo: Additional user info
|
||||
func showNotification(
|
||||
title: String,
|
||||
body: String,
|
||||
icon: String,
|
||||
urgency: NotificationUrgency = .normal,
|
||||
contentDate: Date? = nil,
|
||||
userInfo: [AnyHashable: Any]? = nil
|
||||
) {
|
||||
let urgency = NotificationUrgency(rawValue: urgency.rawValue) ?? .normal
|
||||
let notificationContent = UNMutableNotificationContent()
|
||||
|
||||
notificationContent.title = title
|
||||
notificationContent.body = body
|
||||
notificationContent.sound = UNNotificationSound.default
|
||||
notificationContent.icon = icon
|
||||
notificationContent.categoryIdentifier = urgency.rawValue
|
||||
notificationContent.haptic = .medium
|
||||
|
||||
if let contentDate = contentDate {
|
||||
notificationContent.date = contentDate
|
||||
}
|
||||
|
||||
if let userInfo = userInfo {
|
||||
notificationContent.userInfo = userInfo
|
||||
}
|
||||
|
||||
let request = UNNotificationRequest(
|
||||
identifier: UUID().uuidString,
|
||||
content: notificationContent,
|
||||
trigger: contentDate.map { UNNotificationTrigger(dateMatched: $0, repeats: false) } ?? nil,
|
||||
priority: urgency.priority
|
||||
)
|
||||
|
||||
do {
|
||||
try unuserNotifications.add(request)
|
||||
unuserNotifications.presentNotificationRequest(request, completionHandler: nil)
|
||||
print("Notification shown: \(title)")
|
||||
} catch {
|
||||
print("Failed to show notification: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
/// Show a critical notification
|
||||
/// - Parameters:
|
||||
/// - title: Notification title
|
||||
/// - body: Notification body
|
||||
/// - icon: Icon name
|
||||
func showCriticalNotification(title: String, body: String, icon: String) {
|
||||
showNotification(
|
||||
title: title,
|
||||
body: body,
|
||||
icon: icon,
|
||||
urgency: .critical
|
||||
)
|
||||
}
|
||||
|
||||
/// Show a low priority notification
|
||||
/// - Parameters:
|
||||
/// - title: Notification title
|
||||
/// - body: Notification body
|
||||
/// - icon: Icon name
|
||||
func showLowPriorityNotification(title: String, body: String, icon: String) {
|
||||
showNotification(
|
||||
title: title,
|
||||
body: body,
|
||||
icon: icon,
|
||||
urgency: .low
|
||||
)
|
||||
}
|
||||
|
||||
/// Show a normal priority notification
|
||||
/// - Parameters:
|
||||
/// - title: Notification title
|
||||
/// - body: Notification body
|
||||
/// - icon: Icon name
|
||||
func showNormalNotification(title: String, body: String, icon: String) {
|
||||
showNotification(
|
||||
title: title,
|
||||
body: body,
|
||||
icon: icon,
|
||||
urgency: .normal
|
||||
)
|
||||
}
|
||||
|
||||
/// Check if notification service is available
|
||||
var isAvailable: Bool {
|
||||
return UNUserNotificationCenter.current().isAuthorized(
|
||||
forNotificationTypes: [.alert, .sound, .badge]
|
||||
)
|
||||
}
|
||||
|
||||
/// Get available notification types
|
||||
var availableNotificationTypes: [UNNotificationType] {
|
||||
return unuserNotifications.authorizationStatus(
|
||||
forNotificationTypes: .all
|
||||
)
|
||||
}
|
||||
|
||||
/// Get current authorization status
|
||||
func authorizationStatus(for type: UNNotificationType) -> UNAuthorizationStatus {
|
||||
return unuserNotifications.authorizationStatus(for: type)
|
||||
}
|
||||
|
||||
/// Get the notification center
|
||||
func notificationCenter() -> UNUserNotificationCenter {
|
||||
return unuserNotifications
|
||||
}
|
||||
}
|
||||
|
||||
/// Notification urgency enum
|
||||
enum NotificationUrgency: Int {
|
||||
case critical = 5
|
||||
case normal = 1
|
||||
case low = 0
|
||||
|
||||
var priority: UNNotificationPriority {
|
||||
switch self {
|
||||
case .critical: return .high
|
||||
case .normal: return .default
|
||||
case .low: return .low
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
193
native-route/ios/RSSuper/SyncScheduler.swift
Normal file
193
native-route/ios/RSSuper/SyncScheduler.swift
Normal file
@@ -0,0 +1,193 @@
|
||||
import Foundation
|
||||
import BackgroundTasks
|
||||
|
||||
/// Manages background sync scheduling
|
||||
/// Handles intelligent scheduling based on user behavior and system conditions
|
||||
final class SyncScheduler {
|
||||
// MARK: - Properties
|
||||
|
||||
/// Default sync interval (in seconds)
|
||||
static let defaultSyncInterval: TimeInterval = 6 * 3600 // 6 hours
|
||||
|
||||
/// Minimum sync interval (in seconds)
|
||||
static let minimumSyncInterval: TimeInterval = 15 * 60 // 15 minutes
|
||||
|
||||
/// Maximum sync interval (in seconds)
|
||||
static let maximumSyncInterval: TimeInterval = 24 * 3600 // 24 hours
|
||||
|
||||
/// Key for storing last sync date in UserDefaults
|
||||
private static let lastSyncDateKey = "RSSuperLastSyncDate"
|
||||
|
||||
/// Key for storing preferred sync interval
|
||||
private static let preferredSyncIntervalKey = "RSSuperPreferredSyncInterval"
|
||||
|
||||
/// UserDefaults for persisting sync state
|
||||
private let userDefaults = UserDefaults.standard
|
||||
|
||||
// MARK: - Computed Properties
|
||||
|
||||
/// Last sync date from UserDefaults
|
||||
var lastSyncDate: Date? {
|
||||
get { userDefaults.object(forKey: Self.lastSyncDateKey) as? Date }
|
||||
set { userDefaults.set(newValue, forKey: Self.lastSyncDateKey) }
|
||||
}
|
||||
|
||||
/// Preferred sync interval from UserDefaults
|
||||
var preferredSyncInterval: TimeInterval {
|
||||
get {
|
||||
return userDefaults.double(forKey: Self.preferredSyncIntervalKey)
|
||||
?? Self.defaultSyncInterval
|
||||
}
|
||||
set {
|
||||
let clamped = max(Self.minimumSyncInterval, min(newValue, Self.maximumSyncInterval))
|
||||
userDefaults.set(clamped, forKey: Self.preferredSyncIntervalKey)
|
||||
}
|
||||
}
|
||||
|
||||
/// Time since last sync
|
||||
var timeSinceLastSync: TimeInterval {
|
||||
guard let lastSync = lastSyncDate else {
|
||||
return .greatestFiniteMagnitude
|
||||
}
|
||||
return Date().timeIntervalSince(lastSync)
|
||||
}
|
||||
|
||||
/// Whether a sync is due
|
||||
var isSyncDue: Bool {
|
||||
return timeSinceLastSync >= preferredSyncInterval
|
||||
}
|
||||
|
||||
// MARK: - Public Methods
|
||||
|
||||
/// Schedule the next sync based on current conditions
|
||||
func scheduleNextSync() -> Bool {
|
||||
// Check if we should sync immediately
|
||||
if isSyncDue && timeSinceLastSync >= preferredSyncInterval * 2 {
|
||||
print("📱 Sync is significantly overdue, scheduling immediate sync")
|
||||
return scheduleImmediateSync()
|
||||
}
|
||||
|
||||
// Calculate next sync time
|
||||
let nextSyncTime = calculateNextSyncTime()
|
||||
|
||||
print("📅 Next sync scheduled for: \(nextSyncTime) (in \(nextSyncTime.timeIntervalSinceNow)/3600)h)")
|
||||
|
||||
return scheduleSync(at: nextSyncTime)
|
||||
}
|
||||
|
||||
/// Update preferred sync interval based on user behavior
|
||||
func updateSyncInterval(for numberOfFeeds: Int, userActivityLevel: UserActivityLevel) {
|
||||
var baseInterval: TimeInterval
|
||||
|
||||
// Adjust base interval based on number of feeds
|
||||
switch numberOfFeeds {
|
||||
case 0..<10:
|
||||
baseInterval = 4 * 3600 // 4 hours for small feed lists
|
||||
case 10..<50:
|
||||
baseInterval = 6 * 3600 // 6 hours for medium feed lists
|
||||
case 50..<200:
|
||||
baseInterval = 12 * 3600 // 12 hours for large feed lists
|
||||
default:
|
||||
baseInterval = 24 * 3600 // 24 hours for very large feed lists
|
||||
}
|
||||
|
||||
// Adjust based on user activity
|
||||
switch userActivityLevel {
|
||||
case .high:
|
||||
preferredSyncInterval = baseInterval * 0.5 // Sync more frequently
|
||||
case .medium:
|
||||
preferredSyncInterval = baseInterval
|
||||
case .low:
|
||||
preferredSyncInterval = baseInterval * 2.0 // Sync less frequently
|
||||
}
|
||||
|
||||
print("⚙️ Sync interval updated to: \(preferredSyncInterval/3600)h (feeds: \(numberOfFeeds), activity: \(userActivityLevel))")
|
||||
}
|
||||
|
||||
/// Get recommended sync interval based on current conditions
|
||||
func recommendedSyncInterval() -> TimeInterval {
|
||||
// This could be enhanced with machine learning based on user patterns
|
||||
return preferredSyncInterval
|
||||
}
|
||||
|
||||
/// Reset sync schedule
|
||||
func resetSyncSchedule() {
|
||||
lastSyncDate = nil
|
||||
preferredSyncInterval = Self.defaultSyncInterval
|
||||
print("🔄 Sync schedule reset")
|
||||
}
|
||||
|
||||
// MARK: - Private Methods
|
||||
|
||||
/// Schedule immediate sync
|
||||
private func scheduleImmediateSync() -> Bool {
|
||||
let taskRequest = BGAppRefreshTaskRequest(identifier: BackgroundSyncService.backgroundRefreshIdentifier)
|
||||
taskRequest.earliestBeginDate = Date(timeIntervalSinceNow: 60) // 1 minute
|
||||
|
||||
do {
|
||||
try BGTaskScheduler.shared.submit(taskRequest)
|
||||
print("✓ Immediate sync scheduled")
|
||||
return true
|
||||
} catch {
|
||||
print("❌ Failed to schedule immediate sync: \(error)")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/// Schedule sync at specific time
|
||||
private func scheduleSync(at date: Date) -> Bool {
|
||||
let taskRequest = BGAppRefreshTaskRequest(identifier: BackgroundSyncService.backgroundRefreshIdentifier)
|
||||
taskRequest.earliestBeginDate = date
|
||||
|
||||
do {
|
||||
try BGTaskScheduler.shared.submit(taskRequest)
|
||||
print("✓ Sync scheduled for \(date)")
|
||||
return true
|
||||
} catch {
|
||||
print("❌ Failed to schedule sync: \(error)")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculate next sync time
|
||||
private func calculateNextSyncTime() -> Date {
|
||||
let baseTime = lastSyncDate ?? Date()
|
||||
return baseTime.addingTimeInterval(preferredSyncInterval)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - UserActivityLevel
|
||||
|
||||
/// User activity level for adaptive sync scheduling
|
||||
enum UserActivityLevel: String, Codable {
|
||||
case high // User actively reading, sync more frequently
|
||||
case medium // Normal usage
|
||||
case low // Inactive user, sync less frequently
|
||||
|
||||
/// Calculate activity level based on app usage
|
||||
static func calculate(from dailyOpenCount: Int, lastOpenedAgo: TimeInterval) -> UserActivityLevel {
|
||||
// High activity: opened 5+ times today OR opened within last hour
|
||||
if dailyOpenCount >= 5 || lastOpenedAgo < 3600 {
|
||||
return .high
|
||||
}
|
||||
|
||||
// Medium activity: opened 2+ times today OR opened within last day
|
||||
if dailyOpenCount >= 2 || lastOpenedAgo < 86400 {
|
||||
return .medium
|
||||
}
|
||||
|
||||
// Low activity: otherwise
|
||||
return .low
|
||||
}
|
||||
}
|
||||
|
||||
extension SyncScheduler {
|
||||
static var lastSyncDate: Date? {
|
||||
get {
|
||||
return UserDefaults.standard.object(forKey: Self.lastSyncDateKey) as? Date
|
||||
}
|
||||
set {
|
||||
UserDefaults.standard.set(newValue, forKey: Self.lastSyncDateKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
227
native-route/ios/RSSuper/SyncWorker.swift
Normal file
227
native-route/ios/RSSuper/SyncWorker.swift
Normal file
@@ -0,0 +1,227 @@
|
||||
import Foundation
|
||||
|
||||
/// Performs the actual sync work
|
||||
/// Fetches updates from feeds and processes new articles
|
||||
final class SyncWorker {
|
||||
// MARK: - Properties
|
||||
|
||||
/// Maximum number of feeds to sync per batch
|
||||
static let maxFeedsPerBatch = 20
|
||||
|
||||
/// Timeout for individual feed fetch (in seconds)
|
||||
static let feedFetchTimeout: TimeInterval = 30
|
||||
|
||||
/// Maximum concurrent feed fetches
|
||||
static let maxConcurrentFetches = 3
|
||||
|
||||
// MARK: - Public Methods
|
||||
|
||||
/// Perform a full sync operation
|
||||
func performSync() async throws -> SyncResult {
|
||||
var feedsSynced = 0
|
||||
var articlesFetched = 0
|
||||
var errors: [Error] = []
|
||||
|
||||
// Get all subscriptions that need syncing
|
||||
// TODO: Replace with actual database query
|
||||
let subscriptions = await fetchSubscriptionsNeedingSync()
|
||||
|
||||
print("📡 Starting sync for \(subscriptions.count) subscriptions")
|
||||
|
||||
// Process subscriptions in batches
|
||||
let batches = subscriptions.chunked(into: Self.maxFeedsPerBatch)
|
||||
|
||||
for batch in batches {
|
||||
let batchResults = try await syncBatch(batch)
|
||||
feedsSynced += batchResults.feedsSynced
|
||||
articlesFetched += batchResults.articlesFetched
|
||||
errors.append(contentsOf: batchResults.errors)
|
||||
|
||||
// Small delay between batches to be battery-friendly
|
||||
try? await Task.sleep(nanoseconds: 500_000_000) // 0.5 seconds
|
||||
}
|
||||
|
||||
let result = SyncResult(
|
||||
feedsSynced: feedsSynced,
|
||||
articlesFetched: articlesFetched,
|
||||
errors: errors
|
||||
)
|
||||
|
||||
// Update last sync date
|
||||
SyncScheduler.lastSyncDate = Date()
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/// Perform a partial sync for specific subscriptions
|
||||
func performPartialSync(subscriptionIds: [String]) async throws -> SyncResult {
|
||||
var feedsSynced = 0
|
||||
var articlesFetched = 0
|
||||
var errors: [Error] = []
|
||||
|
||||
// Filter subscriptions by IDs
|
||||
let allSubscriptions = await fetchSubscriptionsNeedingSync()
|
||||
let filteredSubscriptions = allSubscriptions.filter { subscriptionIds.contains($0.id) }
|
||||
|
||||
print("📡 Partial sync for \(filteredSubscriptions.count) subscriptions")
|
||||
|
||||
// Process in batches
|
||||
let batches = filteredSubscriptions.chunked(into: Self.maxFeedsPerBatch)
|
||||
|
||||
for batch in batches {
|
||||
let batchResults = try await syncBatch(batch)
|
||||
feedsSynced += batchResults.feedsSynced
|
||||
articlesFetched += batchResults.articlesFetched
|
||||
errors.append(contentsOf: batchResults.errors)
|
||||
}
|
||||
|
||||
return SyncResult(
|
||||
feedsSynced: feedsSynced,
|
||||
articlesFetched: articlesFetched,
|
||||
errors: errors
|
||||
)
|
||||
}
|
||||
|
||||
/// Cancel ongoing sync operations
|
||||
func cancelSync() {
|
||||
print("⏹️ Sync cancelled")
|
||||
// TODO: Cancel ongoing network requests
|
||||
}
|
||||
|
||||
// MARK: - Private Methods
|
||||
|
||||
/// Fetch subscriptions that need syncing
|
||||
private func fetchSubscriptionsNeedingSync() async -> [Subscription] {
|
||||
// TODO: Replace with actual database query
|
||||
// For now, return empty array as placeholder
|
||||
return []
|
||||
}
|
||||
|
||||
/// Sync a batch of subscriptions
|
||||
private func syncBatch(_ subscriptions: [Subscription]) async throws -> SyncResult {
|
||||
var feedsSynced = 0
|
||||
var articlesFetched = 0
|
||||
var errors: [Error] = []
|
||||
|
||||
// Fetch feeds concurrently with limit
|
||||
let feedResults = try await withThrowingTaskGroup(
|
||||
of: (Subscription, Result<FeedData, Error>).self
|
||||
) { group in
|
||||
var results: [(Subscription, Result<FeedData, Error>)] = []
|
||||
|
||||
for subscription in subscriptions {
|
||||
group.addTask {
|
||||
let result = await self.fetchFeedData(for: subscription)
|
||||
return (subscription, result)
|
||||
}
|
||||
}
|
||||
|
||||
while let result = try? await group.next() {
|
||||
results.append(result)
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
// Process results
|
||||
for (subscription, result) in feedResults {
|
||||
switch result {
|
||||
case .success(let feedData):
|
||||
do {
|
||||
try await processFeedData(feedData, subscriptionId: subscription.id)
|
||||
feedsSynced += 1
|
||||
articlesFetched += feedData.articles.count
|
||||
} catch {
|
||||
errors.append(error)
|
||||
print("❌ Error processing feed data for \(subscription.title): \(error)")
|
||||
}
|
||||
|
||||
case .failure(let error):
|
||||
errors.append(error)
|
||||
print("❌ Error fetching feed \(subscription.title): \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
return SyncResult(
|
||||
feedsSynced: feedsSynced,
|
||||
articlesFetched: articlesFetched,
|
||||
errors: errors
|
||||
)
|
||||
}
|
||||
|
||||
/// Fetch feed data for a subscription
|
||||
private func fetchFeedData(for subscription: Subscription) async -> Result<FeedData, Error> {
|
||||
// TODO: Implement actual feed fetching
|
||||
// This is a placeholder implementation
|
||||
|
||||
do {
|
||||
// Create URL session with timeout
|
||||
let url = URL(string: subscription.url)!
|
||||
let (data, _) = try await URLSession.shared.data(
|
||||
from: url,
|
||||
timeoutInterval: Self.feedFetchTimeout
|
||||
)
|
||||
|
||||
// Parse RSS/Atom feed
|
||||
// TODO: Implement actual parsing
|
||||
let feedData = FeedData(
|
||||
title: subscription.title,
|
||||
articles: [], // TODO: Parse articles
|
||||
lastBuildDate: Date()
|
||||
)
|
||||
|
||||
return .success(feedData)
|
||||
|
||||
} catch {
|
||||
return .failure(error)
|
||||
}
|
||||
}
|
||||
|
||||
/// Process fetched feed data
|
||||
private func processFeedData(_ feedData: FeedData, subscriptionId: String) async throws {
|
||||
// TODO: Implement actual feed data processing
|
||||
// - Store new articles
|
||||
// - Update feed metadata
|
||||
// - Handle duplicates
|
||||
|
||||
print("📝 Processing \(feedData.articles.count) articles for \(feedData.title)")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Helper Types
|
||||
|
||||
/// Subscription model
|
||||
struct Subscription {
|
||||
let id: String
|
||||
let title: String
|
||||
let url: String
|
||||
let lastSyncDate: Date?
|
||||
}
|
||||
|
||||
/// Feed data model
|
||||
struct FeedData {
|
||||
let title: String
|
||||
let articles: [Article]
|
||||
let lastBuildDate: Date
|
||||
}
|
||||
|
||||
/// Article model
|
||||
struct Article {
|
||||
let id: String
|
||||
let title: String
|
||||
let link: String?
|
||||
let published: Date?
|
||||
let content: String?
|
||||
}
|
||||
|
||||
// MARK: - Array Extensions
|
||||
|
||||
extension Array {
|
||||
/// Split array into chunks of specified size
|
||||
func chunked(into size: Int) -> [[Element]] {
|
||||
return stride(from: 0, to: count, by: size).map { i -> [Element] in
|
||||
let end = min(i + size, count)
|
||||
return self[i..<end]
|
||||
}
|
||||
}
|
||||
}
|
||||
64
native-route/ios/RSSuperTests/SyncWorkerTests.swift
Normal file
64
native-route/ios/RSSuperTests/SyncWorkerTests.swift
Normal file
@@ -0,0 +1,64 @@
|
||||
import XCTest
|
||||
@testable import RSSuper
|
||||
|
||||
/// Unit tests for SyncWorker
|
||||
final class SyncWorkerTests: XCTestCase {
|
||||
|
||||
private var worker: SyncWorker!
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
worker = SyncWorker()
|
||||
}
|
||||
|
||||
override func tearDown() {
|
||||
worker = nil
|
||||
super.tearDown()
|
||||
}
|
||||
|
||||
func testChunkedArrayExtension() {
|
||||
let array = [1, 2, 3, 4, 5, 6, 7]
|
||||
let chunks = array.chunked(into: 3)
|
||||
|
||||
XCTAssertEqual(chunks.count, 3)
|
||||
XCTAssertEqual(chunks[0], [1, 2, 3])
|
||||
XCTAssertEqual(chunks[1], [4, 5, 6])
|
||||
XCTAssertEqual(chunks[2], [7])
|
||||
}
|
||||
|
||||
func testChunkedArrayExactDivision() {
|
||||
let array = [1, 2, 3, 4]
|
||||
let chunks = array.chunked(into: 2)
|
||||
|
||||
XCTAssertEqual(chunks.count, 2)
|
||||
XCTAssertEqual(chunks[0], [1, 2])
|
||||
XCTAssertEqual(chunks[1], [3, 4])
|
||||
}
|
||||
|
||||
func testChunkedArrayEmpty() {
|
||||
let array: [Int] = []
|
||||
let chunks = array.chunked(into: 3)
|
||||
|
||||
XCTAssertEqual(chunks.count, 0)
|
||||
}
|
||||
|
||||
func testSyncResultInit() {
|
||||
let result = SyncResult(
|
||||
feedsSynced: 5,
|
||||
articlesFetched: 100,
|
||||
errors: []
|
||||
)
|
||||
|
||||
XCTAssertEqual(result.feedsSynced, 5)
|
||||
XCTAssertEqual(result.articlesFetched, 100)
|
||||
XCTAssertEqual(result.errors.count, 0)
|
||||
}
|
||||
|
||||
func testSyncResultDefaultInit() {
|
||||
let result = SyncResult()
|
||||
|
||||
XCTAssertEqual(result.feedsSynced, 0)
|
||||
XCTAssertEqual(result.articlesFetched, 0)
|
||||
XCTAssertEqual(result.errors.count, 0)
|
||||
}
|
||||
}
|
||||
25
native-route/linux/gsettings/org.rssuper.sync.gschema.xml
Normal file
25
native-route/linux/gsettings/org.rssuper.sync.gschema.xml
Normal file
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<schemalist>
|
||||
<schema id="org.rssuper.sync" path="/org/rssuper/sync/">
|
||||
<key type="t" name="last-sync-timestamp">
|
||||
<default>0</default>
|
||||
<summary>Last sync timestamp</summary>
|
||||
<description>The Unix timestamp of the last successful sync</description>
|
||||
</key>
|
||||
<key type="i" name="preferred-sync-interval">
|
||||
<default>21600</default>
|
||||
<summary>Preferred sync interval in seconds</summary>
|
||||
<description>The preferred interval between sync operations (default: 6 hours)</description>
|
||||
</key>
|
||||
<key type="b" name="auto-sync-enabled">
|
||||
<default>true</default>
|
||||
<summary>Auto-sync enabled</summary>
|
||||
<description>Whether automatic background sync is enabled</description>
|
||||
</key>
|
||||
<key type="i" name="sync-on-wifi-only">
|
||||
<default>0</default>
|
||||
<summary>Sync on Wi-Fi only</summary>
|
||||
<description>0=always, 1=Wi-Fi only, 2=never</description>
|
||||
</key>
|
||||
</schema>
|
||||
</schemalist>
|
||||
10
native-route/linux/org.rssuper.sync.desktop
Normal file
10
native-route/linux/org.rssuper.sync.desktop
Normal file
@@ -0,0 +1,10 @@
|
||||
[Desktop Entry]
|
||||
Name=RSSuper Background Sync
|
||||
Comment=Background feed synchronization for RSSuper
|
||||
Exec=/opt/rssuper/bin/rssuper-sync-daemon
|
||||
Terminal=false
|
||||
Type=Application
|
||||
Categories=Utility;Network;
|
||||
StartupNotify=false
|
||||
Hidden=false
|
||||
X-GNOME-Autostart-enabled=true
|
||||
23
native-route/linux/rssuper-sync.service
Normal file
23
native-route/linux/rssuper-sync.service
Normal file
@@ -0,0 +1,23 @@
|
||||
[Unit]
|
||||
Description=RSSuper Background Sync Service
|
||||
Documentation=man:rssuper(1)
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/opt/rssuper/bin/rssuper-sync
|
||||
StandardOutput=journal
|
||||
StandardError=journal
|
||||
|
||||
# Security hardening
|
||||
NoNewPrivileges=yes
|
||||
ProtectSystem=strict
|
||||
ProtectHome=read-only
|
||||
PrivateTmp=yes
|
||||
|
||||
# Timeout (5 minutes)
|
||||
TimeoutStartSec=300
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
23
native-route/linux/rssuper-sync.timer
Normal file
23
native-route/linux/rssuper-sync.timer
Normal file
@@ -0,0 +1,23 @@
|
||||
[Unit]
|
||||
Description=RSSuper Background Sync Timer
|
||||
Documentation=man:rssuper(1)
|
||||
|
||||
[Timer]
|
||||
# On-boot delay (randomized between 1-5 minutes)
|
||||
OnBootSec=1min
|
||||
RandomizedDelaySec=4min
|
||||
|
||||
# On-unit-active delay (6 hours after service starts)
|
||||
OnUnitActiveSec=6h
|
||||
|
||||
# Accuracy (allow ±15 minutes)
|
||||
AccuracySec=15min
|
||||
|
||||
# Persist timer across reboots
|
||||
Persistent=true
|
||||
|
||||
# Wake system if sleeping to run timer
|
||||
WakeSystem=true
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
503
native-route/linux/src/background-sync.vala
Normal file
503
native-route/linux/src/background-sync.vala
Normal file
@@ -0,0 +1,503 @@
|
||||
/*
|
||||
* background-sync.vala
|
||||
*
|
||||
* Main background sync service for RSSuper on Linux.
|
||||
* Orchestrates background feed synchronization using GTimeout and systemd timer.
|
||||
*/
|
||||
|
||||
using Gio;
|
||||
using GLib;
|
||||
|
||||
namespace RSSuper {
|
||||
|
||||
/**
|
||||
* BackgroundSyncService - Main background sync service coordinator
|
||||
*
|
||||
* Orchestrates background feed synchronization using:
|
||||
* - GTimeout for in-app scheduling
|
||||
* - systemd timer for system-level scheduling
|
||||
*/
|
||||
public class BackgroundSyncService : Object {
|
||||
|
||||
// Singleton instance
|
||||
private static BackgroundSyncService? _instance;
|
||||
|
||||
// Sync scheduler
|
||||
private SyncScheduler? _sync_scheduler;
|
||||
|
||||
// Sync worker
|
||||
private SyncWorker? _sync_worker;
|
||||
|
||||
// Current sync state
|
||||
private bool _is_syncing = false;
|
||||
|
||||
// Sync configuration
|
||||
public const string BACKGROUND_REFRESH_IDENTIFIER = "org.rssuper.background-refresh";
|
||||
public const string PERIODIC_SYNC_IDENTIFIER = "org.rssuper.periodic-sync";
|
||||
|
||||
// Settings
|
||||
private Settings? _settings;
|
||||
|
||||
/**
|
||||
* Get singleton instance
|
||||
*/
|
||||
public static BackgroundSyncService? get_instance() {
|
||||
if (_instance == null) {
|
||||
_instance = new BackgroundSyncService();
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the instance (for singleton pattern)
|
||||
*/
|
||||
private BackgroundSyncService() {
|
||||
_sync_scheduler = SyncScheduler.get_instance();
|
||||
_sync_worker = new SyncWorker();
|
||||
|
||||
try {
|
||||
_settings = new Settings("org.rssuper.sync");
|
||||
} catch (Error e) {
|
||||
warning("Failed to create settings: %s", e.message);
|
||||
}
|
||||
|
||||
// Connect to sync due signal
|
||||
if (_sync_scheduler != null) {
|
||||
_sync_scheduler.sync_due.connect(on_sync_due);
|
||||
}
|
||||
|
||||
info("BackgroundSyncService initialized");
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the sync service
|
||||
*/
|
||||
public void initialize() {
|
||||
info("Initializing background sync service");
|
||||
|
||||
// Schedule initial sync
|
||||
schedule_next_sync();
|
||||
|
||||
info("Background sync service initialized");
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule the next sync
|
||||
*/
|
||||
public bool schedule_next_sync() {
|
||||
if (_is_syncing) {
|
||||
warning("Sync already in progress");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_sync_scheduler != null) {
|
||||
return _sync_scheduler.schedule_next_sync();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel all pending sync operations
|
||||
*/
|
||||
public void cancel_all_pending() {
|
||||
if (_sync_scheduler != null) {
|
||||
_sync_scheduler.cancel_sync_timeout();
|
||||
}
|
||||
info("All pending sync operations cancelled");
|
||||
}
|
||||
|
||||
/**
|
||||
* Force immediate sync (for testing or user-initiated)
|
||||
*/
|
||||
public async void force_sync() {
|
||||
if (_is_syncing) {
|
||||
warning("Sync already in progress");
|
||||
return;
|
||||
}
|
||||
|
||||
_is_syncing = true;
|
||||
|
||||
try {
|
||||
var result = yield _sync_worker.perform_sync();
|
||||
|
||||
// Update last sync timestamp
|
||||
if (_sync_scheduler != null) {
|
||||
_sync_scheduler.set_last_sync_timestamp();
|
||||
}
|
||||
|
||||
info("Force sync completed: %d feeds, %d articles",
|
||||
result.feeds_synced, result.articles_fetched);
|
||||
|
||||
// Schedule next sync
|
||||
schedule_next_sync();
|
||||
|
||||
} catch (Error e) {
|
||||
warning("Force sync failed: %s", e.message);
|
||||
}
|
||||
|
||||
_is_syncing = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if background sync is enabled
|
||||
*/
|
||||
public bool are_background_tasks_enabled() {
|
||||
// Check if systemd timer is active
|
||||
try {
|
||||
var result = subprocess_helper_command_str(
|
||||
"systemctl", "is-enabled", "rssuper-sync.timer");
|
||||
return result.strip() == "enabled";
|
||||
} catch (Error e) {
|
||||
// Timer might not be installed
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get last sync date
|
||||
*/
|
||||
public DateTime? get_last_sync_date() {
|
||||
return _sync_scheduler != null ? _sync_scheduler.get_last_sync_date() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get pending feeds count
|
||||
*/
|
||||
public int get_pending_feeds_count() {
|
||||
// TODO: Implement
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if currently syncing
|
||||
*/
|
||||
public bool is_syncing() {
|
||||
return _is_syncing;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync due callback
|
||||
*/
|
||||
private void on_sync_due() {
|
||||
if (_is_syncing) {
|
||||
warning("Sync already in progress");
|
||||
return;
|
||||
}
|
||||
|
||||
info("Sync due, starting background sync");
|
||||
|
||||
_is_syncing = true;
|
||||
|
||||
// Run sync in background
|
||||
GLib.Thread.new<void?>(null, () => {
|
||||
try {
|
||||
var result = _sync_worker.perform_sync();
|
||||
|
||||
// Update last sync timestamp
|
||||
if (_sync_scheduler != null) {
|
||||
_sync_scheduler.set_last_sync_timestamp();
|
||||
}
|
||||
|
||||
info("Background sync completed: %d feeds, %d articles",
|
||||
result.feeds_synced, result.articles_fetched);
|
||||
|
||||
// Schedule next sync
|
||||
schedule_next_sync();
|
||||
|
||||
} catch (Error e) {
|
||||
warning("Background sync failed: %s", e.message);
|
||||
}
|
||||
|
||||
_is_syncing = false;
|
||||
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown the sync service
|
||||
*/
|
||||
public void shutdown() {
|
||||
cancel_all_pending();
|
||||
info("Background sync service shut down");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* SyncWorker - Performs the actual sync work
|
||||
*/
|
||||
public class SyncWorker : Object {
|
||||
|
||||
// Maximum number of feeds to sync per batch
|
||||
public const int MAX_FEEDS_PER_BATCH = 20;
|
||||
|
||||
// Timeout for individual feed fetch (in seconds)
|
||||
public const int FEED_FETCH_TIMEOUT = 30;
|
||||
|
||||
// Maximum concurrent feed fetches
|
||||
public const int MAX_CONCURRENT_FETCHES = 3;
|
||||
|
||||
/**
|
||||
* Perform a full sync operation
|
||||
*/
|
||||
public SyncResult perform_sync() {
|
||||
int feeds_synced = 0;
|
||||
int articles_fetched = 0;
|
||||
var errors = new List<Error>();
|
||||
|
||||
info("Starting sync");
|
||||
|
||||
// Get all subscriptions that need syncing
|
||||
var subscriptions = fetch_subscriptions_needing_sync();
|
||||
|
||||
info("Syncing %d subscriptions", subscriptions.length());
|
||||
|
||||
if (subscriptions.length() == 0) {
|
||||
info("No subscriptions to sync");
|
||||
return new SyncResult(feeds_synced, articles_fetched, errors);
|
||||
}
|
||||
|
||||
// Process subscriptions in batches
|
||||
var batches = chunk_list(subscriptions, MAX_FEEDS_PER_BATCH);
|
||||
|
||||
foreach (var batch in batches) {
|
||||
var batch_result = sync_batch(batch);
|
||||
feeds_synced += batch_result.feeds_synced;
|
||||
articles_fetched += batch_result.articles_fetched;
|
||||
errors.append_list(batch_result.errors);
|
||||
|
||||
// Small delay between batches to be battery-friendly
|
||||
try {
|
||||
Thread.sleep(500); // 500ms
|
||||
} catch (Error e) {
|
||||
warning("Failed to sleep: %s", e.message);
|
||||
}
|
||||
}
|
||||
|
||||
info("Sync completed: %d feeds, %d articles, %d errors",
|
||||
feeds_synced, articles_fetched, errors.length());
|
||||
|
||||
return new SyncResult(feeds_synced, articles_fetched, errors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform a partial sync for specific subscriptions
|
||||
*/
|
||||
public SyncResult perform_partial_sync(List<string> subscription_ids) {
|
||||
// TODO: Implement partial sync
|
||||
return new SyncResult(0, 0, new List<Error>());
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel ongoing sync operations
|
||||
*/
|
||||
public void cancel_sync() {
|
||||
info("Sync cancelled");
|
||||
// TODO: Cancel ongoing network requests
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch subscriptions that need syncing
|
||||
*/
|
||||
private List<Subscription> fetch_subscriptions_needing_sync() {
|
||||
// TODO: Replace with actual database query
|
||||
// For now, return empty list as placeholder
|
||||
return new List<Subscription>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync a batch of subscriptions
|
||||
*/
|
||||
private SyncResult sync_batch(List<Subscription> subscriptions) {
|
||||
var feeds_synced = 0;
|
||||
var articles_fetched = 0;
|
||||
var errors = new List<Error>();
|
||||
|
||||
foreach (var subscription in subscriptions) {
|
||||
try {
|
||||
var feed_data = fetch_feed_data(subscription);
|
||||
|
||||
if (feed_data != null) {
|
||||
process_feed_data(feed_data, subscription.id);
|
||||
feeds_synced++;
|
||||
articles_fetched += feed_data.articles.length();
|
||||
|
||||
info("Synced %s: %d articles", subscription.title,
|
||||
feed_data.articles.length());
|
||||
}
|
||||
|
||||
} catch (Error e) {
|
||||
errors.append(e);
|
||||
warning("Error syncing %s: %s", subscription.title, e.message);
|
||||
}
|
||||
}
|
||||
|
||||
return new SyncResult(feeds_synced, articles_fetched, errors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch feed data for a subscription
|
||||
*/
|
||||
private FeedData? fetch_feed_data(Subscription subscription) {
|
||||
// TODO: Implement actual feed fetching
|
||||
// This is a placeholder implementation
|
||||
|
||||
// Example implementation:
|
||||
// var uri = new Uri(subscription.url);
|
||||
// var client = new HttpClient();
|
||||
// var data = client.get(uri);
|
||||
// var feed_data = rss_parser.parse(data);
|
||||
// return feed_data;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process fetched feed data
|
||||
*/
|
||||
private void process_feed_data(FeedData feed_data, string subscription_id) {
|
||||
// TODO: Implement actual feed data processing
|
||||
// - Store new articles
|
||||
// - Update feed metadata
|
||||
// - Handle duplicates
|
||||
|
||||
info("Processing %d articles for %s", feed_data.articles.length(),
|
||||
feed_data.title);
|
||||
}
|
||||
|
||||
/**
|
||||
* Chunk a list into batches
|
||||
*/
|
||||
private List<List<Subscription>> chunk_list(List<Subscription> list, int size) {
|
||||
var batches = new List<List<Subscription>>();
|
||||
var current_batch = new List<Subscription>();
|
||||
|
||||
foreach (var item in list) {
|
||||
current_batch.append(item);
|
||||
if (current_batch.length() >= size) {
|
||||
batches.append(current_batch);
|
||||
current_batch = new List<Subscription>();
|
||||
}
|
||||
}
|
||||
|
||||
if (current_batch.length() > 0) {
|
||||
batches.append(current_batch);
|
||||
}
|
||||
|
||||
return batches;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* SyncResult - Result of a sync operation
|
||||
*/
|
||||
public class SyncResult : Object {
|
||||
public int feeds_synced {
|
||||
get { return _feeds_synced; }
|
||||
}
|
||||
public int articles_fetched {
|
||||
get { return _articles_fetched; }
|
||||
}
|
||||
public List<Error> errors {
|
||||
get { return _errors; }
|
||||
}
|
||||
|
||||
private int _feeds_synced;
|
||||
private int _articles_fetched;
|
||||
private List<Error> _errors;
|
||||
|
||||
public SyncResult(int feeds_synced, int articles_fetched, List<Error> errors) {
|
||||
_feeds_synced = feeds_synced;
|
||||
_articles_fetched = articles_fetched;
|
||||
_errors = errors;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscription - Model for a feed subscription
|
||||
*/
|
||||
public class Subscription : Object {
|
||||
public string id {
|
||||
get { return _id; }
|
||||
}
|
||||
public string title {
|
||||
get { return _title; }
|
||||
}
|
||||
public string url {
|
||||
get { return _url; }
|
||||
}
|
||||
public uint64 last_sync_date {
|
||||
get { return _last_sync_date; }
|
||||
}
|
||||
|
||||
private string _id;
|
||||
private string _title;
|
||||
private string _url;
|
||||
private uint64 _last_sync_date;
|
||||
|
||||
public Subscription(string id, string title, string url, uint64 last_sync_date = 0) {
|
||||
_id = id;
|
||||
_title = title;
|
||||
_url = url;
|
||||
_last_sync_date = last_sync_date;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* FeedData - Parsed feed data
|
||||
*/
|
||||
public class FeedData : Object {
|
||||
public string title {
|
||||
get { return _title; }
|
||||
}
|
||||
public List<Article> articles {
|
||||
get { return _articles; }
|
||||
}
|
||||
|
||||
private string _title;
|
||||
private List<Article> _articles;
|
||||
|
||||
public FeedData(string title, List<Article> articles) {
|
||||
_title = title;
|
||||
_articles = articles;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Article - Model for a feed article
|
||||
*/
|
||||
public class Article : Object {
|
||||
public string id {
|
||||
get { return _id; }
|
||||
}
|
||||
public string title {
|
||||
get { return _title; }
|
||||
}
|
||||
public string? link {
|
||||
get { return _link; }
|
||||
}
|
||||
public uint64 published {
|
||||
get { return _published; }
|
||||
}
|
||||
public string? content {
|
||||
get { return _content; }
|
||||
}
|
||||
|
||||
private string _id;
|
||||
private string _title;
|
||||
private string? _link;
|
||||
private uint64 _published;
|
||||
private string? _content;
|
||||
|
||||
public Article(string id, string title, string? link = null,
|
||||
uint64 published = 0, string? content = null) {
|
||||
_id = id;
|
||||
_title = title;
|
||||
_link = link;
|
||||
_published = published;
|
||||
_content = content;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
325
native-route/linux/src/sync-scheduler.vala
Normal file
325
native-route/linux/src/sync-scheduler.vala
Normal file
@@ -0,0 +1,325 @@
|
||||
/*
|
||||
* sync-scheduler.vala
|
||||
*
|
||||
* Manages background sync scheduling for RSSuper on Linux.
|
||||
* Uses GTimeout for in-app scheduling and integrates with systemd timer.
|
||||
*/
|
||||
|
||||
using Gio;
|
||||
using GLib;
|
||||
|
||||
namespace RSSuper {
|
||||
|
||||
/**
|
||||
* SyncScheduler - Manages background sync scheduling
|
||||
*
|
||||
* Handles intelligent scheduling based on user behavior and system conditions.
|
||||
* Uses GTimeout for in-app scheduling and can trigger systemd timer.
|
||||
*/
|
||||
public class SyncScheduler : Object {
|
||||
|
||||
// Default sync interval (6 hours in seconds)
|
||||
public const int DEFAULT_SYNC_INTERVAL = 6 * 3600;
|
||||
|
||||
// Minimum sync interval (15 minutes in seconds)
|
||||
public const int MINIMUM_SYNC_INTERVAL = 15 * 60;
|
||||
|
||||
// Maximum sync interval (24 hours in seconds)
|
||||
public const int MAXIMUM_SYNC_INTERVAL = 24 * 3600;
|
||||
|
||||
// Singleton instance
|
||||
private static SyncScheduler? _instance;
|
||||
|
||||
// Settings for persisting sync state
|
||||
private Settings? _settings;
|
||||
|
||||
// GTimeout source for scheduling
|
||||
private uint _timeout_source_id = 0;
|
||||
|
||||
// Last sync timestamp
|
||||
private uint64 _last_sync_timestamp = 0;
|
||||
|
||||
// Preferred sync interval
|
||||
private int _preferred_sync_interval = DEFAULT_SYNC_INTERVAL;
|
||||
|
||||
// Sync callback
|
||||
public signal void sync_due();
|
||||
|
||||
/**
|
||||
* Get singleton instance
|
||||
*/
|
||||
public static SyncScheduler? get_instance() {
|
||||
if (_instance == null) {
|
||||
_instance = new SyncScheduler();
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the instance (for singleton pattern)
|
||||
*/
|
||||
private SyncScheduler() {
|
||||
// Initialize settings for persisting sync state
|
||||
try {
|
||||
_settings = new Settings("org.rssuper.sync");
|
||||
} catch (Error e) {
|
||||
warning("Failed to create settings: %s", e.message);
|
||||
}
|
||||
|
||||
// Load last sync timestamp
|
||||
if (_settings != null) {
|
||||
_last_sync_timestamp = _settings.get_uint64("last-sync-timestamp");
|
||||
_preferred_sync_interval = _settings.get_int("preferred-sync-interval");
|
||||
}
|
||||
|
||||
info("SyncScheduler initialized: last_sync=%lu, interval=%d",
|
||||
_last_sync_timestamp, _preferred_sync_interval);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get last sync date as DateTime
|
||||
*/
|
||||
public DateTime? get_last_sync_date() {
|
||||
if (_last_sync_timestamp == 0) {
|
||||
return null;
|
||||
}
|
||||
return new DateTime.from_unix_local((int64)_last_sync_timestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get preferred sync interval in hours
|
||||
*/
|
||||
public int get_preferred_sync_interval_hours() {
|
||||
return _preferred_sync_interval / 3600;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set preferred sync interval in hours
|
||||
*/
|
||||
public void set_preferred_sync_interval_hours(int hours) {
|
||||
int clamped = hours.clamp(MINIMUM_SYNC_INTERVAL / 3600, MAXIMUM_SYNC_INTERVAL / 3600);
|
||||
_preferred_sync_interval = clamped * 3600;
|
||||
|
||||
if (_settings != null) {
|
||||
_settings.set_int("preferred-sync-interval", _preferred_sync_interval);
|
||||
}
|
||||
|
||||
info("Preferred sync interval updated to %d hours", clamped);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get time since last sync in seconds
|
||||
*/
|
||||
public uint64 get_time_since_last_sync() {
|
||||
if (_last_sync_timestamp == 0) {
|
||||
return uint64.MAX;
|
||||
}
|
||||
var now = get_monotonic_time() / 1000000; // Convert to seconds
|
||||
return now - _last_sync_timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if sync is due
|
||||
*/
|
||||
public bool is_sync_due() {
|
||||
var time_since = get_time_since_last_sync();
|
||||
return time_since >= (uint64)_preferred_sync_interval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule the next sync based on current conditions
|
||||
*/
|
||||
public bool schedule_next_sync() {
|
||||
// Cancel any existing timeout
|
||||
cancel_sync_timeout();
|
||||
|
||||
// Check if we should sync immediately
|
||||
if (is_sync_due() && get_time_since_last_sync() >= (uint64)(_preferred_sync_interval * 2)) {
|
||||
info("Sync is significantly overdue, scheduling immediate sync");
|
||||
schedule_immediate_sync();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Calculate next sync time
|
||||
var next_sync_in = calculate_next_sync_time();
|
||||
|
||||
info("Next sync scheduled in %d seconds (%.1f hours)",
|
||||
next_sync_in, next_sync_in / 3600.0);
|
||||
|
||||
// Schedule timeout
|
||||
_timeout_source_id = Timeout.add_seconds(next_sync_in, on_sync_timeout);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update preferred sync interval based on user behavior
|
||||
*/
|
||||
public void update_sync_interval(int number_of_feeds, UserActivityLevel activity_level) {
|
||||
int base_interval;
|
||||
|
||||
// Adjust base interval based on number of feeds
|
||||
if (number_of_feeds < 10) {
|
||||
base_interval = 4 * 3600; // 4 hours for small feed lists
|
||||
} else if (number_of_feeds < 50) {
|
||||
base_interval = 6 * 3600; // 6 hours for medium feed lists
|
||||
} else if (number_of_feeds < 200) {
|
||||
base_interval = 12 * 3600; // 12 hours for large feed lists
|
||||
} else {
|
||||
base_interval = 24 * 3600; // 24 hours for very large feed lists
|
||||
}
|
||||
|
||||
// Adjust based on user activity
|
||||
switch (activity_level) {
|
||||
case UserActivityLevel.HIGH:
|
||||
_preferred_sync_interval = base_interval / 2; // Sync more frequently
|
||||
break;
|
||||
case UserActivityLevel.MEDIUM:
|
||||
_preferred_sync_interval = base_interval;
|
||||
break;
|
||||
case UserActivityLevel.LOW:
|
||||
_preferred_sync_interval = base_interval * 2; // Sync less frequently
|
||||
break;
|
||||
}
|
||||
|
||||
// Clamp to valid range
|
||||
_preferred_sync_interval = _preferred_sync_interval.clamp(
|
||||
MINIMUM_SYNC_INTERVAL, MAXIMUM_SYNC_INTERVAL);
|
||||
|
||||
// Persist
|
||||
if (_settings != null) {
|
||||
_settings.set_int("preferred-sync-interval", _preferred_sync_interval);
|
||||
}
|
||||
|
||||
info("Sync interval updated to %d hours (feeds: %d, activity: %s)",
|
||||
_preferred_sync_interval / 3600, number_of_feeds,
|
||||
activity_level.to_string());
|
||||
|
||||
// Re-schedule
|
||||
schedule_next_sync();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get recommended sync interval based on current conditions
|
||||
*/
|
||||
public int recommended_sync_interval() {
|
||||
return _preferred_sync_interval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset sync schedule
|
||||
*/
|
||||
public void reset_sync_schedule() {
|
||||
cancel_sync_timeout();
|
||||
_last_sync_timestamp = 0;
|
||||
_preferred_sync_interval = DEFAULT_SYNC_INTERVAL;
|
||||
|
||||
if (_settings != null) {
|
||||
_settings.set_uint64("last-sync-timestamp", 0);
|
||||
_settings.set_int("preferred-sync-interval", DEFAULT_SYNC_INTERVAL);
|
||||
}
|
||||
|
||||
info("Sync schedule reset");
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel any pending sync timeout
|
||||
*/
|
||||
public void cancel_sync_timeout() {
|
||||
if (_timeout_source_id > 0) {
|
||||
Source.remove(_timeout_source_id);
|
||||
_timeout_source_id = 0;
|
||||
info("Sync timeout cancelled");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set last sync timestamp (called after sync completes)
|
||||
*/
|
||||
public void set_last_sync_timestamp() {
|
||||
_last_sync_timestamp = get_monotonic_time() / 1000000;
|
||||
|
||||
if (_settings != null) {
|
||||
_settings.set_uint64("last-sync-timestamp", _last_sync_timestamp);
|
||||
}
|
||||
|
||||
info("Last sync timestamp updated to %lu", _last_sync_timestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger sync now (for testing or user-initiated)
|
||||
*/
|
||||
public void trigger_sync_now() {
|
||||
info("Triggering sync now");
|
||||
sync_due();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reload state from settings
|
||||
*/
|
||||
public void reload_state() {
|
||||
if (_settings != null) {
|
||||
_last_sync_timestamp = _settings.get_uint64("last-sync-timestamp");
|
||||
_preferred_sync_interval = _settings.get_int("preferred-sync-interval");
|
||||
}
|
||||
info("State reloaded: last_sync=%lu, interval=%d",
|
||||
_last_sync_timestamp, _preferred_sync_interval);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync timeout callback
|
||||
*/
|
||||
private bool on_sync_timeout() {
|
||||
info("Sync timeout triggered");
|
||||
sync_due();
|
||||
return false; // Don't repeat
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule immediate sync
|
||||
*/
|
||||
private void schedule_immediate_sync() {
|
||||
// Schedule for 1 minute from now
|
||||
_timeout_source_id = Timeout.add_seconds(60, () => {
|
||||
info("Immediate sync triggered");
|
||||
sync_due();
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate next sync time in seconds
|
||||
*/
|
||||
private int calculate_next_sync_time() {
|
||||
var time_since = get_time_since_last_sync();
|
||||
if (time_since >= (uint64)_preferred_sync_interval) {
|
||||
return 60; // Sync soon
|
||||
}
|
||||
return _preferred_sync_interval - (int)time_since;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* UserActivityLevel - User activity level for adaptive sync scheduling
|
||||
*/
|
||||
public enum UserActivityLevel {
|
||||
HIGH, // User actively reading, sync more frequently
|
||||
MEDIUM, // Normal usage
|
||||
LOW // Inactive user, sync less frequently
|
||||
|
||||
public static UserActivityLevel calculate(int daily_open_count, uint64 last_opened_ago_seconds) {
|
||||
// High activity: opened 5+ times today OR opened within last hour
|
||||
if (daily_open_count >= 5 || last_opened_ago_seconds < 3600) {
|
||||
return UserActivityLevel.HIGH;
|
||||
}
|
||||
|
||||
// Medium activity: opened 2+ times today OR opened within last day
|
||||
if (daily_open_count >= 2 || last_opened_ago_seconds < 86400) {
|
||||
return UserActivityLevel.MEDIUM;
|
||||
}
|
||||
|
||||
// Low activity: otherwise
|
||||
return UserActivityLevel.LOW;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user