Sync with settings.

This commit is contained in:
Amine 2018-11-03 18:47:43 +01:00
parent 363eaf9bf9
commit a78c6e6b33
10 changed files with 42 additions and 21 deletions

View File

@ -192,8 +192,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
handleDrawer()
handleSwipeRefreshLayout()
handleRecurringTask()
}
private fun handleSwipeRefreshLayout() {
@ -349,6 +347,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
getElementsAccordingToTab()
handleGDPRDialog(sharedPref.getBoolean("GDPR_shown", false))
handleRecurringTask()
}
private fun getAndStoreAllItems() {
@ -1425,7 +1425,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
.build()
WorkManager.getInstance().enqueueUniquePeriodicWork("selfoss-loading", ExistingPeriodicWorkPolicy.KEEP, backgroundWork)
WorkManager.getInstance().enqueueUniquePeriodicWork("selfoss-loading", ExistingPeriodicWorkPolicy.REPLACE, backgroundWork)
}
}
}

View File

@ -1,8 +1,11 @@
package apps.amine.bou.readerforselfoss
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build
import android.preference.PreferenceManager
import androidx.multidex.MultiDexApplication
import android.widget.ImageView
@ -59,6 +62,18 @@ class MyApp : MultiDexApplication() {
initTheme()
tryToHandleBug()
handleNotificationChannels()
}
private fun handleNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = getString(R.string.notification_channel_sync)
val importance = NotificationManager.IMPORTANCE_LOW
val mChannel = NotificationChannel(Config.syncChannelId, name, importance)
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(mChannel)
}
}
override fun attachBaseContext(base: Context?) {

View File

@ -1,10 +1,15 @@
package apps.amine.bou.readerforselfoss.background
import android.app.NotificationManager
import android.content.Context
import android.os.Handler
import android.preference.PreferenceManager
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationCompat.PRIORITY_LOW
import androidx.room.Room
import androidx.work.Worker
import androidx.work.WorkerParameters
import apps.amine.bou.readerforselfoss.R
import apps.amine.bou.readerforselfoss.api.selfoss.Item
import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi
import apps.amine.bou.readerforselfoss.persistence.database.AppDatabase
@ -14,12 +19,9 @@ import apps.amine.bou.readerforselfoss.utils.persistence.toEntity
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.util.*
import kotlin.concurrent.schedule
import kotlin.concurrent.thread
import android.app.NotificationManager
import android.app.NotificationChannel
import android.util.Log
import androidx.core.app.NotificationCompat
import apps.amine.bou.readerforselfoss.R
class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(context, params) {
@ -27,18 +29,13 @@ class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(con
val notificationManager =
applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
//If on Oreo then notification required a notification channel.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
val channel =
NotificationChannel("default", "Default", NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)
}
val notification = NotificationCompat.Builder(applicationContext, "default")
.setContentTitle("Loading")
.setContentText("Loading new items")
val notification = NotificationCompat.Builder(applicationContext, Config.syncChannelId)
.setContentTitle(context.getString(R.string.loading_notification_title))
.setContentText(context.getString(R.string.loading_notification_text))
.setOngoing(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(PRIORITY_LOW)
.setChannelId(Config.syncChannelId)
.setSmallIcon(R.drawable.ic_cloud_download)
notificationManager.notify(1, notification.build())
@ -59,8 +56,10 @@ class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(con
)
api.allItems().enqueue(object : Callback<List<Item>> {
override fun onFailure(call: Call<List<Item>>, t: Throwable) {
Timer("", false).schedule(4000) {
notificationManager.cancel(1)
}
}
override fun onResponse(
call: Call<List<Item>>,
@ -73,9 +72,11 @@ class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(con
db.itemsDao()
.insertAllItems(*(apiItems.map { it.toEntity() }).toTypedArray())
}
Timer("", false).schedule(4000) {
notificationManager.cancel(1)
}
}
}
})
return Result.SUCCESS
}

View File

@ -36,6 +36,8 @@ class Config(c: Context) {
const val trackerUrl = "https://github.com/aminecmi/ReaderforSelfoss/issues"
const val syncChannelId = "sync-channel-id"
fun logoutAndRedirect(
c: Context,
callingActivity: Activity,

Binary file not shown.

After

Width:  |  Height:  |  Size: 334 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 228 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 380 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 547 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 678 B

View File

@ -158,4 +158,7 @@
<string name="pref_switch_periodic_refresh_on">Articles will periodically be synced</string>
<string name="pref_periodic_refresh_minutes_title"><![CDATA[Sync interval ( >= 15 minutes)]]></string>
<string name="pref_switch_refresh_when_charging">Only refresh when phone is charging</string>
<string name="loading_notification_title">Loading ...</string>
<string name="loading_notification_text">Selfoss is syncing your articles</string>
<string name="notification_channel_sync">Sync notification</string>
</resources>