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

Commit

Permalink
Replace anko (v2) (#1615)
Browse files Browse the repository at this point in the history
* Replaced Anko in NotificationScheduler.kt

* Replaced Anko in NotificationUtils.kt

* Added preference key replaced Anko in BaseNavigationActivity.kt

* Beautify by removing this

* Replaced Anko ConnectivityManager in ProgressActivity.kt

* Replaced Anko SearchManager in FragmentForSearching.kt

* Replaced NotificationManager and SharedPreferences in SettingsFragment

* Removed unnecessary comment

* Replaced WifiManager in EduroamCard.kt

* Replaced DefaultSharedPreferences and WifiManager in EduroamCard.kt

* Replaced WifiManager in EduroamFixCardViewHolder.kt

* Replaced DefaultSharedPreferences in TopNewsCard.kt

* Replaced InputMethodManager in OnboardingStartFragment.kt

* Replaced ConnectivityManager in MainFragment.kt

* Replaced textColor in TicketAmountViewHolder.kt

* Replaced AlarmManager in MVVWidget.kt

* Replaced NotificationManager in FcmReceiverService.kt

* Replaced WifiManager in ScanResultsAvailableReceiver.kt

* Replaced ConnectivityManager in NetUtils.kt

* Replaced DefaultSharedPreferences in Utils.kt

* Replaced ConnectivityManager in BaseFragment.kt

* Replaced DefaultSharedPreferences in OnboardingExtrasFragment.kt

* Removed unused import in BaseFragment.kt

* Replaced all android preference manager with android x
This solved the getDefaultSharedPreferenceDeprecated message

* Replaced getSharedPreference with PreferenceManager default Preference

* Removed doAsyncs and replaced JobIntentService with WorkManager

* Replaced doAsync with expedited WorkManager work

* Replaced runOnUIThread with coroutines on main in BaseFragment.kt

* Replaced doAsync with coroutine in LocationManager and calling Activity

* Replaced anko.support.v4.defaultSharedPreferences in GradesFragment.kt

* Replaced doAsync with coroutines in StartupActivity.kt

* Replaced runOnUIThread with coroutines in MainFragment.kt

* Replaced doAsync in CardRepository

* Replaced runOnUiThread with coroutines in StudyRoomsFragment.kt

* Replace browse -> intent: CheckTokenFragment, OnboardingExtrasFragment

* Replaced textChangedListener -> KTX doAfterTextChanged

* Replaced onItemSelectedListener in NavigationDetailsFragment.kt
skip-checks:true

* Removed leftover code

* Replaces doAsync by replacing callback with coroutines in StudyRooms

* Removes unnecessary import

* Replaced doAsync with expedited WorkManager in CacheManager

* Removed unnecessary import

* Fixed ktlint

* Fixed ktlint formatting

* Fixed ktlint formatting

* Updated build.gradle:app

* fixed problems in the merge

* formatting fixes

---------

Co-authored-by: Franz <[email protected]>
Co-authored-by: Franz Kunze <[email protected]>
  • Loading branch information
3 people authored Sep 17, 2023
1 parent b4cad6e commit 34199d6
Show file tree
Hide file tree
Showing 44 changed files with 325 additions and 194 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ dependencies {
implementation "androidx.room:room-runtime:$room_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.0'
implementation 'android.arch.lifecycle:reactivestreams:1.1.1'
implementation 'android.arch.work:work-runtime-ktx:1.0.1'
implementation 'androidx.appcompat:appcompat:1.4.2'
Expand Down Expand Up @@ -136,11 +137,11 @@ dependencies {

// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.anko:anko:0.10.8"
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.fragment:fragment-ktx:1.4.1'
// Note: fix for internal androidx libraries using outdated WorkManager causing a crash
implementation "androidx.work:work-runtime-ktx:2.7.1"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1'

// RxJava
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.tum.`in`.tumcampusapp.api.tumonline.interceptors

import android.content.Context
import android.preference.PreferenceManager
import androidx.preference.PreferenceManager
import de.tum.`in`.tumcampusapp.utils.Const
import okhttp3.Interceptor
import okhttp3.Response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import androidx.core.app.NotificationManagerCompat
import de.tum.`in`.tumcampusapp.component.notifications.model.AppNotification
import de.tum.`in`.tumcampusapp.component.notifications.model.FutureNotification
import de.tum.`in`.tumcampusapp.component.notifications.model.InstantNotification
Expand All @@ -14,8 +15,6 @@ import de.tum.`in`.tumcampusapp.component.notifications.receivers.NotificationAl
import de.tum.`in`.tumcampusapp.component.notifications.receivers.NotificationReceiver
import de.tum.`in`.tumcampusapp.database.TcaDb
import de.tum.`in`.tumcampusapp.utils.Const
import org.jetbrains.anko.alarmManager
import org.jetbrains.anko.notificationManager
import org.joda.time.DateTime
import javax.inject.Inject

Expand All @@ -28,7 +27,7 @@ import javax.inject.Inject
*/
class NotificationScheduler @Inject constructor(private val context: Context) {

private val notificationManager = context.notificationManager
private val notificationManager = NotificationManagerCompat.from(context)

/**
* Schedules a list of [FutureNotification]s for the time specified by each notification.
Expand Down Expand Up @@ -89,7 +88,8 @@ class NotificationScheduler @Inject constructor(private val context: Context) {
fun cancel(globalId: Long, notification: FutureNotification) {
val pendingIntent = getAlarmIntent(notification, globalId)
pendingIntent.cancel()
context.alarmManager.cancel(pendingIntent)
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager.cancel(pendingIntent)
}

/**
Expand Down Expand Up @@ -117,7 +117,7 @@ class NotificationScheduler @Inject constructor(private val context: Context) {
*/
fun scheduleAlarm(type: NotificationType, time: DateTime) {
val alarmIntent = getAlarmIntent(type)
val alarmManager = context.alarmManager
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager

// If the same alarm has already been scheduled, we cancel it.
alarmIntent.cancel()
Expand All @@ -134,7 +134,7 @@ class NotificationScheduler @Inject constructor(private val context: Context) {
*/
private fun scheduleAlarm(notification: FutureNotification, globalId: Long) {
val alarmIntent = getAlarmIntent(notification, globalId)
val alarmManager = context.alarmManager
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager.setExact(AlarmManager.RTC_WAKEUP, notification.time.millis, alarmIntent)
addActiveAlarm(context, globalId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import android.app.NotificationManager
import android.content.Context
import android.graphics.Color
import android.os.Build
import androidx.core.app.NotificationManagerCompat
import de.tum.`in`.tumcampusapp.R
import de.tum.`in`.tumcampusapp.utils.Const
import org.jetbrains.anko.notificationManager

object NotificationUtils {

Expand Down Expand Up @@ -92,7 +92,7 @@ object NotificationUtils {
lightColor = Color.RED
}

val notificationManager = context.notificationManager
val notificationManager = NotificationManagerCompat.from(context)
val channels = listOf(default, chat, eduroam, cafeteria, mvv, emergency)

channels.forEach { notificationManager.createNotificationChannel(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package de.tum.`in`.tumcampusapp.component.notifications.receivers
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.OutOfQuotaPolicy
import androidx.work.WorkManager
import androidx.work.Worker
import androidx.work.WorkerParameters
import de.tum.`in`.tumcampusapp.component.notifications.NotificationScheduler
import de.tum.`in`.tumcampusapp.component.notifications.persistence.NotificationType
import de.tum.`in`.tumcampusapp.component.tumui.tutionfees.TuitionFeesNotificationProvider
import de.tum.`in`.tumcampusapp.component.ui.cafeteria.CafeteriaNotificationProvider
import de.tum.`in`.tumcampusapp.component.ui.transportation.TransportNotificationProvider
import de.tum.`in`.tumcampusapp.utils.Const
import org.jetbrains.anko.doAsync

class NotificationAlarmReceiver : BroadcastReceiver() {

Expand All @@ -23,11 +27,22 @@ class NotificationAlarmReceiver : BroadcastReceiver() {
else -> return
}

doAsync {
val notification = notificationProvider.buildNotification()
notification?.let {
NotificationScheduler(context).schedule(it)
// create subclass of Worker to enqueue with WorkManager
class WorkWhenReceived(appContext: Context, workerParams: WorkerParameters) :
Worker(appContext, workerParams) {
override fun doWork(): Result {
val notification = notificationProvider.buildNotification()
notification?.let {
NotificationScheduler(applicationContext).schedule(it)
}
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.drawerlayout.widget.DrawerLayout.LOCK_MODE_LOCKED_CLOSED
import androidx.drawerlayout.widget.DrawerLayout.LOCK_MODE_UNLOCKED
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.preference.PreferenceManager
import com.google.android.material.navigation.NavigationView
import de.tum.`in`.tumcampusapp.R
import de.tum.`in`.tumcampusapp.component.other.generic.drawer.DrawerHeaderInflater
Expand All @@ -22,7 +23,6 @@ import de.tum.`in`.tumcampusapp.component.ui.overview.CardManager
import de.tum.`in`.tumcampusapp.component.ui.overview.MainFragment
import de.tum.`in`.tumcampusapp.utils.Const
import de.tum.`in`.tumcampusapp.utils.closeDrawers
import org.jetbrains.anko.defaultSharedPreferences

class BaseNavigationActivity :
BaseActivity(
Expand Down Expand Up @@ -60,7 +60,8 @@ class BaseNavigationActivity :

initDrawer()
supportFragmentManager.registerFragmentLifecycleCallbacks(fragmentCallbacks, false)
defaultSharedPreferences.registerOnSharedPreferenceChangeListener(this)

PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this)

if (savedInstanceState == null) {
supportFragmentManager
Expand Down Expand Up @@ -148,7 +149,7 @@ class BaseNavigationActivity :
}

override fun onDestroy() {
defaultSharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this)
super.onDestroy()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.tum.`in`.tumcampusapp.component.other.generic.activity

import android.content.Context
import android.graphics.Color
import android.net.ConnectivityManager
import android.net.ConnectivityManager.NetworkCallback
import android.net.Network
import android.net.NetworkRequest
Expand Down Expand Up @@ -29,7 +31,6 @@ import de.tum.`in`.tumcampusapp.utils.NetUtils.internetCapability
import de.tum.`in`.tumcampusapp.utils.Utils
import de.tum.`in`.tumcampusapp.utils.setImageResourceOrHide
import de.tum.`in`.tumcampusapp.utils.setTextOrHide
import org.jetbrains.anko.connectivityManager
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
Expand Down Expand Up @@ -246,7 +247,8 @@ abstract class ProgressActivity<T>(
.build()

if (registered.not()) {
connectivityManager.registerNetworkCallback(request, networkCallback)
(getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager)
.registerNetworkCallback(request, networkCallback)
registered = true
}
}
Expand Down Expand Up @@ -289,7 +291,8 @@ abstract class ProgressActivity<T>(
*/
protected fun showLoadingStart() {
if (registered) {
connectivityManager.unregisterNetworkCallback(networkCallback)
(getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager)
.unregisterNetworkCallback(networkCallback)
registered = false
}

Expand Down Expand Up @@ -344,7 +347,8 @@ abstract class ProgressActivity<T>(
super.onDestroy()
apiCall?.cancel()
if (registered) {
connectivityManager.unregisterNetworkCallback(networkCallback)
(getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager)
.unregisterNetworkCallback(networkCallback)
registered = false
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.tum.`in`.tumcampusapp.component.other.generic.fragment

import android.content.Context
import android.graphics.Color
import android.net.ConnectivityManager
import android.net.Network
Expand All @@ -14,6 +15,7 @@ import androidx.annotation.LayoutRes
import androidx.annotation.StringRes
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.google.android.material.snackbar.Snackbar
import de.tum.`in`.tumcampusapp.R
Expand All @@ -33,8 +35,9 @@ import de.tum.`in`.tumcampusapp.utils.NetUtils
import de.tum.`in`.tumcampusapp.utils.Utils
import de.tum.`in`.tumcampusapp.utils.setImageResourceOrHide
import de.tum.`in`.tumcampusapp.utils.setTextOrHide
import org.jetbrains.anko.connectivityManager
import org.jetbrains.anko.support.v4.runOnUiThread
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
Expand All @@ -45,6 +48,8 @@ abstract class BaseFragment<T>(
@StringRes private val titleResId: Int
) : Fragment(), SwipeRefreshLayout.OnRefreshListener {

private val mainDispatcher: CoroutineDispatcher = Dispatchers.Main

private var apiCall: Call<T>? = null
private var hadSuccessfulRequest = false

Expand All @@ -63,7 +68,7 @@ abstract class BaseFragment<T>(

private val networkCallback: ConnectivityManager.NetworkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
runOnUiThread {
this@BaseFragment.lifecycleScope.launch(mainDispatcher) {
this@BaseFragment.onRefresh()
}
}
Expand Down Expand Up @@ -187,7 +192,7 @@ abstract class BaseFragment<T>(
* @param messageResId Resource id of the error text
*/
protected fun showError(messageResId: Int) {
runOnUiThread {
this@BaseFragment.lifecycleScope.launch(mainDispatcher) {
showError(UnknownErrorViewState(messageResId))
}
}
Expand All @@ -207,7 +212,7 @@ abstract class BaseFragment<T>(
}

protected fun showErrorSnackbar(messageResId: Int) {
runOnUiThread {
this@BaseFragment.lifecycleScope.launch(mainDispatcher) {
Snackbar.make(contentView, messageResId, Snackbar.LENGTH_LONG)
.setAction(R.string.retry) { retryRequest() }
.setActionTextColor(Color.WHITE)
Expand All @@ -228,13 +233,13 @@ abstract class BaseFragment<T>(
}

private fun showFailedTokenLayout(messageResId: Int = R.string.error_accessing_tumonline_body) {
runOnUiThread {
this@BaseFragment.lifecycleScope.launch(mainDispatcher) {
showError(FailedTokenViewState(messageResId))
}
}

protected fun showNoInternetLayout() {
runOnUiThread {
this@BaseFragment.lifecycleScope.launch(mainDispatcher) {
showError(NoInternetViewState())
}

Expand All @@ -243,25 +248,26 @@ abstract class BaseFragment<T>(
.build()

if (registered.not()) {
baseActivity.connectivityManager.registerNetworkCallback(request, networkCallback)
(baseActivity.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager)
.registerNetworkCallback(request, networkCallback)
registered = true
}
}

protected fun showEmptyResponseLayout(messageResId: Int, iconResId: Int? = null) {
runOnUiThread {
this@BaseFragment.lifecycleScope.launch(mainDispatcher) {
showError(EmptyViewState(iconResId, messageResId))
}
}

protected fun showContentLayout() {
runOnUiThread {
this@BaseFragment.lifecycleScope.launch(mainDispatcher) {
layoutAllErrorsBinding.layoutError.errorLayout.visibility = View.GONE
}
}

protected fun showErrorLayout() {
runOnUiThread {
this@BaseFragment.lifecycleScope.launch(mainDispatcher) {
layoutAllErrorsBinding.layoutError.errorLayout.visibility = View.VISIBLE
}
}
Expand Down Expand Up @@ -313,7 +319,8 @@ abstract class BaseFragment<T>(
*/
protected fun showLoadingStart() {
if (registered) {
baseActivity.connectivityManager.unregisterNetworkCallback(networkCallback)
(baseActivity.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager)
.unregisterNetworkCallback(networkCallback)
registered = false
}

Expand Down Expand Up @@ -372,7 +379,8 @@ abstract class BaseFragment<T>(

override fun onDestroy() {
if (registered) {
baseActivity.connectivityManager.unregisterNetworkCallback(networkCallback)
(baseActivity.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager)
.unregisterNetworkCallback(networkCallback)
registered = false
}
super.onDestroy()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.tum.`in`.tumcampusapp.component.other.generic.fragment

import android.app.SearchManager
import android.content.Context
import android.content.SearchRecentSuggestionsProvider.DATABASE_MODE_QUERIES
import android.database.Cursor
import android.os.Bundle
Expand All @@ -16,7 +17,6 @@ import androidx.appcompat.widget.SearchView
import de.tum.`in`.tumcampusapp.R
import de.tum.`in`.tumcampusapp.component.other.generic.activity.BaseNavigationActivity
import de.tum.`in`.tumcampusapp.utils.Utils
import org.jetbrains.anko.searchManager

abstract class FragmentForSearching<T>(
@LayoutRes layoutId: Int,
Expand Down Expand Up @@ -61,7 +61,7 @@ abstract class FragmentForSearching<T>(
searchItem = checkNotNull(menu.findItem(R.id.action_search))
searchView = searchItem.actionView as SearchView

val searchManager = requireContext().searchManager
val searchManager = requireContext().getSystemService(Context.SEARCH_SERVICE) as SearchManager
val info = searchManager.getSearchableInfo(requireActivity().componentName)
searchView.setSearchableInfo(info)

Expand Down
Loading

0 comments on commit 34199d6

Please sign in to comment.