Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Replace anko #1581

Closed
wants to merge 47 commits into from
Closed

Replace anko #1581

wants to merge 47 commits into from

Conversation

kunzef
Copy link
Contributor

@kunzef kunzef commented May 28, 2023

Issue

This fixes the following issue(s):

Why this is useful for all students

Most likely users won't notice. But Anko has been archived since 2020 and thus not received improvements. Also the way how asynchronous work is done is not up to date / deprecated classes are used. This PR fixes this in all cases where previously Anko was used.

How is Anko replaced

  • for services through the corresponding native implementation
  • for SharedPreferences through the androidx PreferenceManager
  • asynchronous handling:
    • for impersistent work by binding to lifecycle using coroutines where possible
    • for persistent work by using WorkManager
  • all other cases through native implementations see commit messages for every replacement

kunzef and others added 30 commits May 25, 2023 02:39
This solved the getDefaultSharedPreferenceDeprecated message
@kunzef
Copy link
Contributor Author

kunzef commented May 31, 2023

Hey all, I just finished this. I would really appreciate it if someone can have a look at the replacement of the asynchronous stuff as this is the most critical part.

@kunzef kunzef marked this pull request as ready for review May 31, 2023 20:56
@kunzef kunzef marked this pull request as draft June 1, 2023 01:11
@kunzef
Copy link
Contributor Author

kunzef commented Jun 1, 2023

Just discovered a bug where clicking Recover Cards on the main page crashes the app. I am currently investigating it and reverted the PR to a draft PR.

@kordianbruck kordianbruck requested a review from Liqs-v2 July 18, 2023 22:12
@kunzef kunzef marked this pull request as ready for review August 1, 2023 23:59
@kunzef
Copy link
Contributor Author

kunzef commented Aug 2, 2023

Just discovered a bug where clicking Recover Cards on the main page crashes the app. I am currently investigating it and reverted the PR to a draft PR.

This is fixed through #1594

The PR should be working fine now. Regarding the merge-conflicts I can not resolve them on my side, unfortunately but I have attached fixes for QueryLocationsService (renamed to QueryLocationWorker) and for CacheManager. The conflicts in CalendarFragment are trivial.

QueryLocationWorker.kt

class QueryLocationWorker(appContext: Context, workerParams: WorkerParameters) :
    Worker(appContext, workerParams) {

    private lateinit var locationManager: LocationManager

    override fun doWork(): Result {
        locationManager = LocationManager(applicationContext)

        Utils.log("Query location service worker started …")
        val calendarDao = TcaDb.getInstance(applicationContext).calendarDao()
        val roomLocationsDao = TcaDb.getInstance(applicationContext).roomLocationsDao()

        calendarDao.lecturesWithoutCoordinates
            .filter { it.location.isNotEmpty() }
            .mapNotNull { createRoomLocationsOrNull(it) }
            .also { roomLocationsDao.insert(*it.toTypedArray()) }

        // Do sync of google calendar if necessary
        val shouldSyncCalendar = Utils.getSettingBool(applicationContext, Const.SYNC_CALENDAR, false) &&
            ContextCompat.checkSelfPermission(applicationContext, Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED
        val syncManager = SyncManager(applicationContext)
        val needsSync = syncManager.needSync(Const.SYNC_CALENDAR, TIME_TO_SYNC_CALENDAR)

        if (shouldSyncCalendar.not() || needsSync.not()) {
            return Result.failure()
        }

        try {
            CalendarController.syncCalendar(applicationContext)
            syncManager.replaceIntoDb(Const.SYNC_CALENDAR)
            return Result.success()
        } catch (e: SQLiteException) {
            Utils.log(e)
            return Result.failure()
        }
    }

    private fun createRoomLocationsOrNull(item: CalendarItem): RoomLocations? {
        val geo = locationManager.roomLocationStringToGeo(item.location)
        return geo?.let {
            RoomLocations(item.location, it)
        }
    }

    companion object {

        private const val TIME_TO_SYNC_CALENDAR = 604800 // 1 week

        @JvmStatic
        fun enqueueWork(context: Context) {
            Utils.log("Query locations work enqueued")
            WorkManager.getInstance(context).enqueue(OneTimeWorkRequest.from(QueryLocationWorker::class.java))
        }
    }
}

ChacheManager.kt

class CacheManager @Inject constructor(private val context: Context) {

    val cache: Cache
        get() = Cache(context.cacheDir, 10 * 1024 * 1024) // 10 MB

    fun fillCache() {
        class WorkWhenReceived(appContext: Context, workerParams: WorkerParameters) :
            Worker(appContext, workerParams) {
            override fun doWork(): Result {
                syncCalendar()
                return Result.success()
            }
        }
        // start expedited background work
        val request = OneTimeWorkRequestBuilder<WorkWhenReceived>()
            .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
            .build()
        WorkManager.getInstance(context)
            .enqueue(request)
    }

    private fun syncCalendar() {
        TUMOnlineClient
                .getInstance(context)
                .getCalendar(CacheControl.USE_CACHE)
                .enqueue(object : Callback<EventsResponse> {
                    override fun onResponse(call: Call<EventsResponse>, response: Response<EventsResponse>) {
                        val eventsResponse = response.body() ?: return
                        val events = eventsResponse.events ?: return
                        CalendarController(context).importCalendar(events)
                        loadRoomLocations()
                    }

                    override fun onFailure(call: Call<EventsResponse>, t: Throwable) {
                        Utils.log(t, "Error while loading calendar in CacheManager")
                    }
                })
    }

    private fun loadRoomLocations() {
        // enqueues OneTimeWorkRequest
        QueryLocationsService.enqueueWork(context)
    }

    @Synchronized
    fun clearCache() {
        cache.delete()
    }
}

@tobiasjungmann
Copy link
Contributor

I've rebased your branch on the latest version of the master branch. To make the comparison easier I've done it on a new Branch Some lines in the same files concerning the card refactoring and the preference rework did overwrite themselves. Please have a look at it to make sure that the most recent changes of both are added and are working together since you probably have a much better overview about the changes than I have :D What I've seen from the code so far looks good and should be easy to merge afterwards!

@CommanderStorm
Copy link
Member

CommanderStorm commented Sep 15, 2023

@tobiasjungmann your rebase looks good to me. Can I merge it?

Edit:
I just checked tried to compile + merge => Tobias Rebase contains significant defects ^^:
image

@CommanderStorm
Copy link
Member

I rebased this in #1615
⇒ Works without errors, merging as soon as the build succeeds

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Replace Anko Library
3 participants