Initial work on background task.
This commit is contained in:
parent
880dd1db5c
commit
ccab2c7648
@ -160,6 +160,8 @@ dependencies {
|
||||
|
||||
implementation "androidx.room:room-runtime:$room_version"
|
||||
kapt "androidx.room:room-compiler:$room_version"
|
||||
|
||||
implementation "android.arch.work:work-runtime-ktx:$work_version"
|
||||
}
|
||||
|
||||
|
||||
|
@ -25,6 +25,10 @@ import android.view.View
|
||||
import android.widget.Toast
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import androidx.work.Constraints
|
||||
import androidx.work.OneTimeWorkRequestBuilder
|
||||
import androidx.work.PeriodicWorkRequestBuilder
|
||||
import androidx.work.WorkManager
|
||||
import apps.amine.bou.readerforselfoss.adapters.ItemCardAdapter
|
||||
import apps.amine.bou.readerforselfoss.adapters.ItemListAdapter
|
||||
import apps.amine.bou.readerforselfoss.adapters.ItemsAdapter
|
||||
@ -34,6 +38,7 @@ import apps.amine.bou.readerforselfoss.api.selfoss.Source
|
||||
import apps.amine.bou.readerforselfoss.api.selfoss.Stats
|
||||
import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse
|
||||
import apps.amine.bou.readerforselfoss.api.selfoss.Tag
|
||||
import apps.amine.bou.readerforselfoss.background.LoadingWorker
|
||||
import apps.amine.bou.readerforselfoss.persistence.database.AppDatabase
|
||||
import apps.amine.bou.readerforselfoss.persistence.migrations.MIGRATION_1_2
|
||||
import apps.amine.bou.readerforselfoss.settings.SettingsActivity
|
||||
@ -75,6 +80,7 @@ import org.acra.ACRA
|
||||
import retrofit2.Call
|
||||
import retrofit2.Callback
|
||||
import retrofit2.Response
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
||||
@ -180,6 +186,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
||||
handleDrawer()
|
||||
|
||||
handleSwipeRefreshLayout()
|
||||
|
||||
handleRecurringTask()
|
||||
}
|
||||
|
||||
private fun handleSwipeRefreshLayout() {
|
||||
@ -1388,5 +1396,24 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
||||
alertDialog.show()
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleRecurringTask() {
|
||||
// TODO: add network type
|
||||
val myConstraints = Constraints.Builder()
|
||||
.setRequiresBatteryNotLow(true)
|
||||
.setRequiresStorageNotLow(true)
|
||||
.build()
|
||||
|
||||
// TODO: make the time variable from the settings.
|
||||
val backgroundWork =
|
||||
PeriodicWorkRequestBuilder<LoadingWorker>(4, TimeUnit.HOURS)
|
||||
.setConstraints(myConstraints)
|
||||
.addTag("selfoss-loading")
|
||||
.build()
|
||||
|
||||
|
||||
WorkManager.getInstance().enqueue(backgroundWork)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
class SelfossApi(
|
||||
c: Context,
|
||||
callingActivity: Activity,
|
||||
callingActivity: Activity?,
|
||||
isWithSelfSignedCert: Boolean,
|
||||
shouldLog: Boolean
|
||||
) {
|
||||
@ -91,9 +91,11 @@ class SelfossApi(
|
||||
.build()
|
||||
service = retrofit.create(SelfossService::class.java)
|
||||
} catch (e: IllegalArgumentException) {
|
||||
if (callingActivity != null) {
|
||||
Config.logoutAndRedirect(c, callingActivity, config.settings.edit(), baseUrlFail = true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun login(): Call<SuccessResponse> =
|
||||
service.loginToSelfoss(config.userLogin, config.userPassword)
|
||||
|
@ -0,0 +1,57 @@
|
||||
package apps.amine.bou.readerforselfoss.background
|
||||
|
||||
import android.content.Context
|
||||
import android.preference.PreferenceManager
|
||||
import androidx.room.Room
|
||||
import androidx.work.Worker
|
||||
import androidx.work.WorkerParameters
|
||||
import apps.amine.bou.readerforselfoss.api.selfoss.Item
|
||||
import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi
|
||||
import apps.amine.bou.readerforselfoss.persistence.database.AppDatabase
|
||||
import apps.amine.bou.readerforselfoss.persistence.migrations.MIGRATION_1_2
|
||||
import apps.amine.bou.readerforselfoss.utils.Config
|
||||
import apps.amine.bou.readerforselfoss.utils.persistence.toEntity
|
||||
import retrofit2.Call
|
||||
import retrofit2.Callback
|
||||
import retrofit2.Response
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(context, params) {
|
||||
|
||||
override fun doWork(): Result {
|
||||
val settings = this.context.getSharedPreferences(Config.settingsName, Context.MODE_PRIVATE)
|
||||
val sharedPref = PreferenceManager.getDefaultSharedPreferences(this.context)
|
||||
val shouldLogEverything = sharedPref.getBoolean("should_log_everything", false)
|
||||
|
||||
val db = Room.databaseBuilder(
|
||||
applicationContext,
|
||||
AppDatabase::class.java, "selfoss-database"
|
||||
).addMigrations(MIGRATION_1_2).build()
|
||||
|
||||
val api = SelfossApi(
|
||||
this.context,
|
||||
null,
|
||||
settings.getBoolean("isSelfSignedCert", false),
|
||||
shouldLogEverything
|
||||
)
|
||||
api.allItems().enqueue(object : Callback<List<Item>> {
|
||||
override fun onFailure(call: Call<List<Item>>, t: Throwable) {
|
||||
}
|
||||
|
||||
override fun onResponse(
|
||||
call: Call<List<Item>>,
|
||||
response: Response<List<Item>>
|
||||
) {
|
||||
thread {
|
||||
if (response.body() != null) {
|
||||
val apiItems = (response.body() as ArrayList<Item>)
|
||||
db.itemsDao().deleteAllItems()
|
||||
db.itemsDao()
|
||||
.insertAllItems(*(apiItems.map { it.toEntity() }).toTypedArray())
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
return Result.SUCCESS
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ buildscript {
|
||||
android_version = '1.0.0'
|
||||
lifecycle_version = '2.0.0'
|
||||
room_version = '2.1.0-alpha01'
|
||||
work_version = "1.0.0-alpha10"
|
||||
}
|
||||
repositories {
|
||||
google()
|
||||
|
Loading…
Reference in New Issue
Block a user