Skip to content

Commit

Permalink
Revert "Switch to compose state and kotlin flows - 1. iteration (#279)…
Browse files Browse the repository at this point in the history
…" (#285)

This reverts commit ff34782.
  • Loading branch information
sunkup committed Apr 17, 2024
1 parent 1e78d06 commit ea88798
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 154 deletions.
23 changes: 13 additions & 10 deletions app/src/androidTest/java/at/bitfire/icsdroid/CalendarFetcherTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import androidx.test.platform.app.InstrumentationRegistry
import at.bitfire.icsdroid.HttpUtils.toAndroidUri
import at.bitfire.icsdroid.test.BuildConfig
import at.bitfire.icsdroid.test.R
import java.io.IOException
import java.io.InputStream
import java.net.HttpURLConnection
import java.util.LinkedList
import kotlinx.coroutines.runBlocking
import okhttp3.MediaType
import okhttp3.mockwebserver.MockResponse
Expand All @@ -25,6 +21,10 @@ import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.BeforeClass
import org.junit.Test
import java.io.IOException
import java.io.InputStream
import java.net.HttpURLConnection
import java.util.*

class CalendarFetcherTest {

Expand Down Expand Up @@ -55,8 +55,9 @@ class CalendarFetcherTest {

var ical: String? = null
val fetcher = object: CalendarFetcher(appContext, uri) {
override suspend fun onSuccess(data: InputStream, contentType: MediaType?, eTag: String?, lastModified: Long?, displayName: String?) {
override fun onSuccess(data: InputStream, contentType: MediaType?, eTag: String?, lastModified: Long?, displayName: String?) {
ical = IOUtils.toString(data, Charsets.UTF_8)
data.close()
}
}
runBlocking {
Expand Down Expand Up @@ -89,10 +90,11 @@ class CalendarFetcherTest {
var etag: String? = null
var lastmod: Long? = null
val fetcher = object: CalendarFetcher(appContext, server.url("/").toAndroidUri()) {
override suspend fun onSuccess(data: InputStream, contentType: MediaType?, eTag: String?, lastModified: Long?, displayName: String?) {
override fun onSuccess(data: InputStream, contentType: MediaType?, eTag: String?, lastModified: Long?, displayName: String?) {
ical = IOUtils.toString(data, Charsets.UTF_8)
etag = eTag
lastmod = lastModified
data.close()
}
}
runBlocking {
Expand Down Expand Up @@ -130,8 +132,9 @@ class CalendarFetcherTest {
redirects += target
super.onRedirect(httpCode, target)
}
override suspend fun onSuccess(data: InputStream, contentType: MediaType?, eTag: String?, lastModified: Long?, displayName: String?) {
override fun onSuccess(data: InputStream, contentType: MediaType?, eTag: String?, lastModified: Long?, displayName: String?) {
ical = IOUtils.toString(data, Charsets.UTF_8)
data.close()
}
}
runBlocking {
Expand Down Expand Up @@ -162,7 +165,7 @@ class CalendarFetcherTest {
var e: Exception? = null
runBlocking {
object : CalendarFetcher(appContext, server.url("/").toAndroidUri()) {
override suspend fun onError(error: Exception) {
override fun onError(error: Exception) {
e = error
}
}.fetch()
Expand All @@ -179,7 +182,7 @@ class CalendarFetcherTest {
var notModified = false
runBlocking {
object : CalendarFetcher(appContext, server.url("/").toAndroidUri()) {
override suspend fun onNotModified() {
override fun onNotModified() {
notModified = true
}
}.fetch()
Expand All @@ -196,7 +199,7 @@ class CalendarFetcherTest {
var e: Exception? = null
runBlocking {
object : CalendarFetcher(appContext, server.url("/").toAndroidUri()) {
override suspend fun onError(error: Exception) {
override fun onError(error: Exception) {
e = error
}
}.fetch()
Expand Down
40 changes: 0 additions & 40 deletions app/src/androidTest/java/at/bitfire/icsdroid/TestSettings.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,8 @@ import at.bitfire.icsdroid.db.dao.CredentialsDao
import at.bitfire.icsdroid.db.dao.SubscriptionsDao
import at.bitfire.icsdroid.db.entity.Subscription
import kotlinx.coroutines.runBlocking
import org.junit.AfterClass
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.BeforeClass
import org.junit.ClassRule
import org.junit.Test
import org.junit.*
import org.junit.Assert.*

class CalendarToRoomMigrationTest {

Expand Down Expand Up @@ -165,7 +158,7 @@ class CalendarToRoomMigrationTest {
}

@Test
fun testMigrateFromV2_1() = runBlocking {
fun testMigrateFromV2_1() {
// prepare: create local calendar plus subscription with subscription.id = LocalCalendar.id,
// but with calendarId=null and COLUMN_MANAGED_BY_DB=null
val calendar = createCalendar()
Expand Down
18 changes: 9 additions & 9 deletions app/src/main/java/at/bitfire/icsdroid/CalendarFetcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import android.provider.DocumentsContract
import android.util.Log
import at.bitfire.icsdroid.HttpUtils.toURI
import at.bitfire.icsdroid.HttpUtils.toUri
import okhttp3.Credentials
import okhttp3.MediaType
import okhttp3.Request
import java.io.FileNotFoundException
import java.io.IOException
import java.io.InputStream
import java.net.HttpURLConnection
import java.util.Date
import okhttp3.Credentials
import okhttp3.MediaType
import okhttp3.Request
import java.util.*

open class CalendarFetcher(
val context: Context,
Expand Down Expand Up @@ -48,10 +48,10 @@ open class CalendarFetcher(
fetchLocal()
}

open suspend fun onSuccess(data: InputStream, contentType: MediaType?, eTag: String?, lastModified: Long?, displayName: String?) {
open fun onSuccess(data: InputStream, contentType: MediaType?, eTag: String?, lastModified: Long?, displayName: String?) {
}

open suspend fun onNotModified() {
open fun onNotModified() {
}

open suspend fun onRedirect(httpCode: Int, target: Uri) {
Expand Down Expand Up @@ -83,17 +83,17 @@ open class CalendarFetcher(
fetchNetwork()
}

open suspend fun onNewPermanentUrl(target: Uri) {
open fun onNewPermanentUrl(target: Uri) {
}

open suspend fun onError(error: Exception) {
open fun onError(error: Exception) {
}


/**
* Fetch the file with Android SAF
*/
internal suspend fun fetchLocal() {
internal fun fetchLocal() {
Log.i(Constants.TAG, "Fetching local file $uri")
try {
val contentResolver = context.contentResolver
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/at/bitfire/icsdroid/ProcessEventsTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import at.bitfire.icsdroid.calendar.LocalEvent
import at.bitfire.icsdroid.db.AppDatabase
import at.bitfire.icsdroid.db.entity.Subscription
import at.bitfire.icsdroid.ui.NotificationUtils
import at.bitfire.icsdroid.ui.views.CalendarListActivity
import at.bitfire.icsdroid.ui.views.EditCalendarActivity
import java.io.InputStream
import java.io.InputStreamReader
import java.time.Duration
import at.bitfire.icsdroid.ui.views.CalendarListActivity
import net.fortuna.ical4j.model.Property
import net.fortuna.ical4j.model.PropertyList
import net.fortuna.ical4j.model.component.VAlarm
Expand Down Expand Up @@ -127,7 +127,7 @@ class ProcessEventsTask(
var exception: Throwable? = null

val downloader = object : CalendarFetcher(context, uri) {
override suspend fun onSuccess(
override fun onSuccess(
data: InputStream,
contentType: MediaType?,
eTag: String?,
Expand All @@ -148,18 +148,18 @@ class ProcessEventsTask(
}
}

override suspend fun onNotModified() {
override fun onNotModified() {
Log.i(Constants.TAG, "Calendar has not been modified since last sync")
subscriptionsDao.updateStatusNotModified(subscription.id)
}

override suspend fun onNewPermanentUrl(target: Uri) {
override fun onNewPermanentUrl(target: Uri) {
super.onNewPermanentUrl(target)
Log.i(Constants.TAG, "Got permanent redirect, saving new URL: $target")
subscriptionsDao.updateUrl(subscription.id, target)
}

override suspend fun onError(error: Exception) {
override fun onError(error: Exception) {
Log.w(Constants.TAG, "Sync error", error)
exception = error
}
Expand Down
24 changes: 0 additions & 24 deletions app/src/main/java/at/bitfire/icsdroid/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ package at.bitfire.icsdroid
import android.content.Context
import android.content.SharedPreferences
import androidx.lifecycle.LiveData
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow

class Settings(context: Context) {

Expand All @@ -22,10 +19,6 @@ class Settings(context: Context) {

fun forceDarkMode(): Boolean = prefs.getBoolean(FORCE_DARK_MODE, false)

@Deprecated(
message = "Replace LiveData by Flows",
replaceWith = ReplaceWith("forceDarkModeFlow()")
)
fun forceDarkModeLive(): LiveData<Boolean> = object: LiveData<Boolean>() {
val listener = SharedPreferences.OnSharedPreferenceChangeListener { prefs, key ->
if (key == FORCE_DARK_MODE) {
Expand All @@ -44,23 +37,6 @@ class Settings(context: Context) {
}
}

fun forceDarkModeFlow(): Flow<Boolean> = callbackFlow {
val listener = SharedPreferences.OnSharedPreferenceChangeListener { prefs, key ->
if (key == FORCE_DARK_MODE) {
val forceDarkMode = prefs.getBoolean(key, false)
trySend(forceDarkMode)
}
}

prefs.registerOnSharedPreferenceChangeListener(listener)
listener.onSharedPreferenceChanged(prefs, FORCE_DARK_MODE)

awaitClose {
// Remove listener
prefs.unregisterOnSharedPreferenceChangeListener(listener)
}
}

fun forceDarkMode(force: Boolean) {
// save setting
prefs.edit()
Expand Down
20 changes: 3 additions & 17 deletions app/src/main/java/at/bitfire/icsdroid/SyncWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ import android.content.ContentProviderClient
import android.content.ContentUris
import android.content.Context
import android.util.Log
import androidx.work.Constraints
import androidx.work.CoroutineWorker
import androidx.work.ExistingWorkPolicy
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import androidx.work.workDataOf
import androidx.work.*
import at.bitfire.ical4android.AndroidCalendar
import at.bitfire.ical4android.util.MiscUtils.closeCompat
import at.bitfire.icsdroid.Constants.TAG
Expand Down Expand Up @@ -86,16 +79,9 @@ class SyncWorker(
.enqueue()
}

@Deprecated(
message = "Replace LiveData by Flows",
replaceWith = ReplaceWith("statusFlow(context)")
)
fun liveStatus(context: Context) =
WorkManager.getInstance(context).getWorkInfosForUniqueWorkLiveData(NAME)

fun statusFlow(context: Context) =
WorkManager.getInstance(context).getWorkInfosForUniqueWorkFlow(NAME)

}

private val database = AppDatabase.getInstance(applicationContext)
Expand Down Expand Up @@ -158,7 +144,7 @@ class SyncWorker(
* 2. Checks that those calendars have a matching [Subscription] in the database.
* 3. If there's no matching [Subscription], create it.
*/
private suspend fun migrateLegacyCalendars() {
private fun migrateLegacyCalendars() {
@Suppress("DEPRECATION")
val legacyCredentials by lazy { CalendarCredentials(applicationContext) }

Expand Down Expand Up @@ -198,7 +184,7 @@ class SyncWorker(
* - updated (e.g. display name) if there's a [Subscription] for this calendar,
* - deleted if there's no [Subscription] for this calendar.
*/
private suspend fun updateLocalCalendars() {
private fun updateLocalCalendars() {
// subscriptions from DB
val subscriptions = subscriptionsDao.getAll()

Expand Down
Loading

0 comments on commit ea88798

Please sign in to comment.