Archived
Renamed HelperFunctions to SubstUtil, played around with Kotlin extensions
This commit is contained in:
@@ -20,7 +20,7 @@ import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.constraintlayout.widget.ConstraintLayout
|
||||
import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat
|
||||
import com.denizd.substitutionplan.R
|
||||
import com.denizd.substitutionplan.data.HelperFunctions
|
||||
import com.denizd.substitutionplan.data.SubstUtil
|
||||
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
|
||||
import com.google.android.material.textfield.TextInputEditText
|
||||
import java.util.*
|
||||
@@ -44,7 +44,7 @@ internal class FirstTime : AppCompatActivity(R.layout.activity_first_time) {
|
||||
val parentLayout = findViewById<ConstraintLayout>(R.id.coordinatorLayout)
|
||||
parentLayout.layoutTransition.enableTransitionType(LayoutTransition.CHANGING)
|
||||
|
||||
HelperFunctions.setTheme(window = window, context = context)
|
||||
SubstUtil.setTheme(window = window, context = context)
|
||||
|
||||
prefs = PreferenceManager.getDefaultSharedPreferences(context) as SharedPreferences
|
||||
val edit = prefs.edit()
|
||||
|
||||
@@ -21,7 +21,7 @@ import androidx.constraintlayout.widget.ConstraintLayout
|
||||
import androidx.coordinatorlayout.widget.CoordinatorLayout
|
||||
import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat
|
||||
import com.denizd.substitutionplan.R
|
||||
import com.denizd.substitutionplan.data.HelperFunctions
|
||||
import com.denizd.substitutionplan.data.SubstUtil
|
||||
import com.denizd.substitutionplan.data.LoginWebViewClient
|
||||
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
@@ -48,7 +48,7 @@ internal class Login : AppCompatActivity(R.layout.activity_login_webview), Login
|
||||
val title = findViewById<TextView>(R.id.txtWelcome)
|
||||
prefs = PreferenceManager.getDefaultSharedPreferences(this)
|
||||
|
||||
HelperFunctions.setTheme(window = window, context = this)
|
||||
SubstUtil.setTheme(window = window, context = this)
|
||||
|
||||
logInButton = findViewById(R.id.loginFab)
|
||||
logInButton.setOnClickListener {
|
||||
|
||||
@@ -20,7 +20,7 @@ import androidx.core.widget.NestedScrollView
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentTransaction
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.denizd.substitutionplan.data.HelperFunctions
|
||||
import com.denizd.substitutionplan.data.SubstUtil
|
||||
import com.denizd.substitutionplan.R
|
||||
import com.denizd.substitutionplan.fragments.FoodFragment
|
||||
import com.denizd.substitutionplan.fragments.GeneralPlanFragment
|
||||
@@ -70,7 +70,7 @@ internal class Main : AppCompatActivity(R.layout.app_bar_main) {
|
||||
else -> { // launch the app
|
||||
|
||||
if (!prefs.getBoolean("colourTransferred", false)) {
|
||||
HelperFunctions.transferOldColourIntsToString(prefs, context)
|
||||
SubstUtil.transferOldColourIntsToString(prefs, context)
|
||||
edit.putBoolean("colourTransferred", true).apply()
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ import android.widget.Toast
|
||||
import androidx.browser.customtabs.CustomTabsIntent
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.denizd.substitutionplan.data.HelperFunctions
|
||||
import com.denizd.substitutionplan.data.SubstUtil
|
||||
import com.denizd.substitutionplan.R
|
||||
import com.denizd.substitutionplan.models.Substitution
|
||||
import com.google.android.material.card.MaterialCardView
|
||||
@@ -89,16 +89,16 @@ internal class SubstitutionAdapter(private var substitutions: List<Substitution>
|
||||
val add = currentItem.additional
|
||||
val type = currentItem.type.toLowerCase(Locale.ROOT)
|
||||
if (add.isNotEmpty()) {
|
||||
if (HelperFunctions.checkStringForArray(add, HelperFunctions.cancellations, true)) {
|
||||
strikeThrough(strings)
|
||||
if (SubstUtil.checkStringForArray(add, SubstUtil.cancellations, true)) {
|
||||
strings.strikeThrough()
|
||||
}
|
||||
} else {
|
||||
if (HelperFunctions.checkStringForArray(type, HelperFunctions.cancellations, true)) {
|
||||
strikeThrough(strings)
|
||||
if (SubstUtil.checkStringForArray(type, SubstUtil.cancellations, true)) {
|
||||
strings.strikeThrough()
|
||||
}
|
||||
}
|
||||
|
||||
var icon = HelperFunctions.getIconForCourse(currentItem.course)
|
||||
var icon = SubstUtil.getIconForCourse(currentItem.course)
|
||||
holder.group.setText(strings[0], TextView.BufferType.SPANNABLE)
|
||||
holder.time.setText(strings[1], TextView.BufferType.SPANNABLE)
|
||||
holder.course.setText(strings[2], TextView.BufferType.SPANNABLE)
|
||||
@@ -113,13 +113,13 @@ internal class SubstitutionAdapter(private var substitutions: List<Substitution>
|
||||
cardBackgroundColour = R.color.colorAccent
|
||||
View.GONE
|
||||
} else {
|
||||
val colourString = HelperFunctions.getColourString(holder.course.text.toString())
|
||||
val colourString = SubstUtil.getColourString(holder.course.text.toString())
|
||||
val colourPrefsInt = if (colourString.isNotEmpty()) {
|
||||
prefs.getString("card$colourString", "") ?: ""
|
||||
} else {
|
||||
""
|
||||
}
|
||||
colour = HelperFunctions.getColourForString(colourPrefsInt, holder.context)
|
||||
colour = SubstUtil.getColourForString(colourPrefsInt, holder.context)
|
||||
cardBackgroundColour = if (colour != 0) {
|
||||
colour
|
||||
} else {
|
||||
@@ -171,7 +171,7 @@ internal class SubstitutionAdapter(private var substitutions: List<Substitution>
|
||||
}
|
||||
|
||||
/// Strikes through the strings for course, room and teacher
|
||||
private fun strikeThrough(strings: Array<SpannableString>) {
|
||||
for (i in 2..4) { strings[i].setSpan(StrikethroughSpan(), 0, strings[i].length, 0) }
|
||||
private fun Array<SpannableString>.strikeThrough() {
|
||||
for (i in 2..4) { this[i].setSpan(StrikethroughSpan(), 0, this[i].length, 0) }
|
||||
}
|
||||
}
|
||||
@@ -11,8 +11,6 @@ import android.util.Log
|
||||
import androidx.preference.PreferenceManager
|
||||
import android.view.View
|
||||
import android.widget.RemoteViews
|
||||
import android.widget.TextView
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
||||
@@ -137,7 +135,7 @@ internal class DataFetcher(
|
||||
val indices = ArrayList<Int>()
|
||||
val daysAndVon = arrayOf("montag", "dienstag", "mittwoch", "donnerstag", "freitag", "von", "wünschen")
|
||||
for (i in 0 until foodElements.size) {
|
||||
if (HelperFunctions.checkStringForArray(foodElements[i].text(), daysAndVon, true)) indices.add(i)
|
||||
if (SubstUtil.checkStringForArray(foodElements[i].text(), daysAndVon, true)) indices.add(i)
|
||||
}
|
||||
indices.add(foodElements.size)
|
||||
|
||||
@@ -194,8 +192,8 @@ internal class DataFetcher(
|
||||
additional = cols[5].text(),
|
||||
teacher = cols[6].text(),
|
||||
type = cols[7].text(),
|
||||
priority = HelperFunctions.assignRanking(group, (date.length > 2 && date.substring(0, 3) == "psa")),
|
||||
date_priority = HelperFunctions.assignDatePriority(date),
|
||||
priority = SubstUtil.assignRanking(group, (date.length > 2 && date.substring(0, 3) == "psa")),
|
||||
date_priority = SubstUtil.assignDatePriority(date),
|
||||
website_priority = websitePriority
|
||||
)
|
||||
substArray.add(subst)
|
||||
@@ -206,7 +204,7 @@ internal class DataFetcher(
|
||||
var countOfMoreNotificationItems = 0
|
||||
if (jobService && prefs.getBoolean("notif", true)) {
|
||||
substArray.filter { substItem ->
|
||||
HelperFunctions.checkPersonalSubstitutions(
|
||||
SubstUtil.checkPersonalSubstitutions(
|
||||
substItem,
|
||||
coursePreference,
|
||||
classPreference,
|
||||
@@ -249,10 +247,10 @@ internal class DataFetcher(
|
||||
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
HelperFunctions.getNotificationChannel(context, prefs)
|
||||
SubstUtil.getNotificationChannel(context, prefs)
|
||||
}
|
||||
|
||||
val notification = NotificationCompat.Builder(context, HelperFunctions.notificationChannelId)
|
||||
val notification = NotificationCompat.Builder(context, SubstUtil.notificationChannelId)
|
||||
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
|
||||
.setCustomContentView(notificationLayout)
|
||||
.setContentIntent(openAppPending)
|
||||
|
||||
@@ -24,7 +24,7 @@ internal class LoginWebViewClient(private val successListener: OnLoginSuccessLis
|
||||
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
|
||||
super.onPageStarted(view, url, favicon)
|
||||
|
||||
if (!HelperFunctions.checkStringForArray(url.toString(), schoolUrls, false)) {
|
||||
if (!SubstUtil.checkStringForArray(url.toString(), schoolUrls, false)) {
|
||||
reloadLoginPage(webView = view)
|
||||
}
|
||||
}
|
||||
|
||||
+11
-11
@@ -34,7 +34,7 @@ import javax.xml.parsers.DocumentBuilderFactory
|
||||
* An object class used for declaring functions and variables used by multiple classes throughout
|
||||
* the project. As it is an object class, it can be accessed statically without initialisation
|
||||
*/
|
||||
internal object HelperFunctions {
|
||||
internal object SubstUtil {
|
||||
|
||||
/**
|
||||
* A language-independent list of all courses that is used to save the custom colours of any
|
||||
@@ -401,20 +401,20 @@ internal object HelperFunctions {
|
||||
private val prefTypes = arrayOf("string", "string", "string", "bool", "int", "bool", "bool", "bool",
|
||||
"string", "int", "int", "bool", "bool")
|
||||
|
||||
private fun getPrefValue(prefs: SharedPreferences, type: String, key: String): Any {
|
||||
private fun SharedPreferences.getPrefValue(type: String, key: String): Any {
|
||||
return when (type) {
|
||||
"string" -> prefs.getString(key, "") ?: ""
|
||||
"int" -> prefs.getInt(key, 0)
|
||||
"bool" -> prefs.getBoolean(key, false)
|
||||
"string" -> getString(key, "") ?: ""
|
||||
"int" -> getInt(key, 0)
|
||||
"bool" -> getBoolean(key, false)
|
||||
else -> ""
|
||||
}
|
||||
}
|
||||
|
||||
private fun setPrefValue(prefs: SharedPreferences, key: String, value: String, type: String) {
|
||||
private fun SharedPreferences.setPrefValue(key: String, value: String, type: String) {
|
||||
when (type) {
|
||||
"int" -> prefs.edit().putInt(key, value.toInt()).apply()
|
||||
"string" -> prefs.edit().putString(key, value).apply()
|
||||
"bool" -> prefs.edit().putBoolean(key, value.toBoolean()).apply()
|
||||
"int" -> edit().putInt(key, value.toInt()).apply()
|
||||
"string" -> edit().putString(key, value).apply()
|
||||
"bool" -> edit().putBoolean(key, value.toBoolean()).apply()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -439,7 +439,7 @@ internal object HelperFunctions {
|
||||
var input = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<items>"
|
||||
for (i in localPrefKeys.indices) {
|
||||
input += "\n\t<key>${localPrefKeys[i]}</key>" +
|
||||
"\n\t<value>${getPrefValue(prefs, localPrefTypes[i], localPrefKeys[i])}</value>"
|
||||
"\n\t<value>${prefs.getPrefValue(localPrefTypes[i], localPrefKeys[i])}</value>"
|
||||
}
|
||||
input += "\n</items>"
|
||||
out.apply {
|
||||
@@ -471,7 +471,7 @@ internal object HelperFunctions {
|
||||
for (i in localPrefTypes.indices) {
|
||||
val key = document.getElementsByTagName("key").item(i).textContent
|
||||
val value = document.getElementsByTagName("value").item(i).textContent
|
||||
setPrefValue(prefs, key, value, localPrefTypes[i])
|
||||
prefs.setPrefValue(key, value, localPrefTypes[i])
|
||||
}
|
||||
Toast.makeText(context, context.getString(R.string.success), Toast.LENGTH_LONG).show()
|
||||
} else {
|
||||
@@ -16,7 +16,6 @@ internal abstract class SubstDatabase : RoomDatabase() {
|
||||
|
||||
companion object {
|
||||
|
||||
private var instance: SubstDatabase? = null
|
||||
private val addTeacherColumn = object : Migration(5, 6) {
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("ALTER TABLE subst_table ADD COLUMN teacher TEXT NOT NULL DEFAULT ''")
|
||||
@@ -38,6 +37,8 @@ internal abstract class SubstDatabase : RoomDatabase() {
|
||||
}
|
||||
}
|
||||
|
||||
private var instance: SubstDatabase? = null
|
||||
|
||||
fun getInstance(context: Context): SubstDatabase? {
|
||||
if (instance == null) {
|
||||
synchronized (SubstDatabase::class) {
|
||||
|
||||
@@ -12,7 +12,7 @@ import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import com.denizd.substitutionplan.adapters.FoodAdapter
|
||||
import com.denizd.substitutionplan.data.HelperFunctions
|
||||
import com.denizd.substitutionplan.data.SubstUtil
|
||||
import com.denizd.substitutionplan.database.FoodViewModel
|
||||
import com.denizd.substitutionplan.databinding.FoodLayoutBinding
|
||||
import com.denizd.substitutionplan.models.Food
|
||||
@@ -50,7 +50,7 @@ internal class FoodFragment : Fragment() {
|
||||
foodViewModel.allFoods?.observe(this, Observer<List<Food>> { foodList ->
|
||||
binding.recyclerView.scheduleLayoutAnimation()
|
||||
mAdapter.setFood(if (foodList.isEmpty()) {
|
||||
HelperFunctions.getEmptyFoodMenu(mContext)
|
||||
SubstUtil.getEmptyFoodMenu(mContext)
|
||||
} else {
|
||||
foodList
|
||||
})
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.denizd.substitutionplan.fragments
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import androidx.lifecycle.Observer
|
||||
import com.denizd.substitutionplan.data.HelperFunctions
|
||||
import com.denizd.substitutionplan.data.SubstUtil
|
||||
import com.denizd.substitutionplan.models.Substitution
|
||||
|
||||
internal class GeneralPlanFragment : PlanFragment() {
|
||||
@@ -13,7 +13,7 @@ internal class GeneralPlanFragment : PlanFragment() {
|
||||
|
||||
substitutionPlan?.observe(this, Observer<List<Substitution>> { substitutions ->
|
||||
mAdapter.setSubst(if (substitutions.isEmpty()) {
|
||||
HelperFunctions.getEmptyGeneralSubstitution(mContext)
|
||||
SubstUtil.getEmptyGeneralSubstitution(mContext)
|
||||
} else {
|
||||
substitutions
|
||||
})
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.denizd.substitutionplan.fragments
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import androidx.lifecycle.Observer
|
||||
import com.denizd.substitutionplan.data.HelperFunctions
|
||||
import com.denizd.substitutionplan.data.SubstUtil
|
||||
import com.denizd.substitutionplan.models.Substitution
|
||||
|
||||
internal class PersonalPlanFragment : PlanFragment() {
|
||||
@@ -20,7 +20,7 @@ internal class PersonalPlanFragment : PlanFragment() {
|
||||
binding.recyclerView.visibility = View.VISIBLE
|
||||
|
||||
substitutions.filter { substItem ->
|
||||
HelperFunctions.checkPersonalSubstitutions(
|
||||
SubstUtil.checkPersonalSubstitutions(
|
||||
substItem,
|
||||
coursePreference,
|
||||
classPreference,
|
||||
@@ -31,7 +31,7 @@ internal class PersonalPlanFragment : PlanFragment() {
|
||||
}
|
||||
isPersonalPlanEmpty = (planCardList.size == 1 && planCardList[0].date.substring(0, 3) == "psa") || planCardList.isEmpty()
|
||||
if (isPersonalPlanEmpty) {
|
||||
planCardList.add(HelperFunctions.getEmptyPersonalSubstitution(mContext))
|
||||
planCardList.add(SubstUtil.getEmptyPersonalSubstitution(mContext))
|
||||
}
|
||||
binding.recyclerView.scheduleLayoutAnimation()
|
||||
mAdapter.setSubst(planCardList)
|
||||
|
||||
@@ -31,7 +31,7 @@ import com.denizd.substitutionplan.adapters.ColourPickerAdapter
|
||||
import com.denizd.substitutionplan.adapters.CourseColourAdapter
|
||||
import com.denizd.substitutionplan.adapters.RingtoneAdapter
|
||||
import com.denizd.substitutionplan.data.DataFetcher
|
||||
import com.denizd.substitutionplan.data.HelperFunctions
|
||||
import com.denizd.substitutionplan.data.SubstUtil
|
||||
import com.denizd.substitutionplan.data.Topic
|
||||
import com.denizd.substitutionplan.databinding.ContentSettingsBinding
|
||||
import com.denizd.substitutionplan.models.Colour
|
||||
@@ -325,7 +325,7 @@ internal class SettingsFragment : Fragment(), View.OnClickListener, View.OnLongC
|
||||
colourRecycler.apply {
|
||||
hasFixedSize()
|
||||
layoutManager = GridLayoutManager(mContext, 1)
|
||||
adapter = ColourPickerAdapter(HelperFunctions.getColourArray(mContext).toList(), this@SettingsFragment)
|
||||
adapter = ColourPickerAdapter(SubstUtil.getColourArray(mContext).toList(), this@SettingsFragment)
|
||||
}
|
||||
colourPickerDialog = colourPickerBuilder.setView(dialogView).create()
|
||||
colourPickerDialog.show()
|
||||
@@ -376,7 +376,7 @@ internal class SettingsFragment : Fragment(), View.OnClickListener, View.OnLongC
|
||||
|
||||
private fun createRingtoneDialog() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
HelperFunctions.getNotificationChannel(mContext, prefs)
|
||||
SubstUtil.getNotificationChannel(mContext, prefs)
|
||||
val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).apply {
|
||||
putExtra(Settings.EXTRA_APP_PACKAGE, mContext.applicationContext.packageName)
|
||||
putExtra(Settings.EXTRA_CHANNEL_ID, "general")
|
||||
@@ -487,8 +487,8 @@ internal class SettingsFragment : Fragment(), View.OnClickListener, View.OnLongC
|
||||
this == "_viewstacktrace" -> {
|
||||
createDialog("Debug Stack Trace", prefs.getString("debug_recent_exception", "") ?: "")
|
||||
}
|
||||
this == "_write" -> HelperFunctions.writePrefsToXml(prefs, mContext, activity!!)
|
||||
this == "_read" -> HelperFunctions.readPrefsFromXml(prefs, mContext, activity!!)
|
||||
this == "_write" -> SubstUtil.writePrefsToXml(prefs, mContext, activity!!)
|
||||
this == "_read" -> SubstUtil.readPrefsFromXml(prefs, mContext, activity!!)
|
||||
else -> makeToast(getString(R.string.invalid_code))
|
||||
}
|
||||
}
|
||||
@@ -501,7 +501,7 @@ internal class SettingsFragment : Fragment(), View.OnClickListener, View.OnLongC
|
||||
// Functions for retrieving data
|
||||
private fun getColourList(): ArrayList<Colour> {
|
||||
val colours = ArrayList<Colour>()
|
||||
val coursesNoLang = HelperFunctions.languageIndependentCourses
|
||||
val coursesNoLang = SubstUtil.languageIndependentCourses
|
||||
val courses = arrayOf(
|
||||
getString(R.string.course_deu),
|
||||
getString(R.string.course_eng),
|
||||
@@ -563,7 +563,7 @@ internal class SettingsFragment : Fragment(), View.OnClickListener, View.OnLongC
|
||||
courses[i],
|
||||
coursesNoLang[i],
|
||||
coursesIcons[i],
|
||||
HelperFunctions.getColourForString(prefs.getString("card${coursesNoLang[i]}", "") ?: "", mContext)
|
||||
SubstUtil.getColourForString(prefs.getString("card${coursesNoLang[i]}", "") ?: "", mContext)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user