Bimba.git

ref: v1.1.0

app/src/main/java/ml/adamsprogs/bimba/models/FavouriteStorage.kt


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package ml.adamsprogs.bimba.models

import android.content.Context
import android.content.SharedPreferences
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject


class FavouriteStorage private constructor(context: Context) : Iterable<Favourite> {
    companion object {
        private var favouriteStorage: FavouriteStorage? = null
        fun getFavouriteStorage(context: Context? = null): FavouriteStorage {
            return if (favouriteStorage == null) {
                if (context == null)
                    throw IllegalArgumentException("requested new storage context not given")
                else {
                    favouriteStorage = FavouriteStorage(context)
                    favouriteStorage as FavouriteStorage
                }
            } else
                favouriteStorage as FavouriteStorage
        }
    }

    val favourites = HashMap<String, Favourite>()
    private val preferences: SharedPreferences = context.getSharedPreferences("ml.adamsprogs.bimba.prefs", Context.MODE_PRIVATE)
    val favouritesList: List<Favourite>
        get() {
            return favourites.values.toList()
        }

    init {
        val favouritesString = preferences.getString("favourites", "{}")
        val favouritesMap = Gson().fromJson(favouritesString, JsonObject::class.java)
        for ((name, jsonTimetables) in favouritesMap.entrySet()) {
            val timetables = ArrayList<HashMap<String, String>>()
            for (jsonTimetable in jsonTimetables.asJsonArray) {
                val timetable = HashMap<String, String>()
                timetable[Favourite.TAG_STOP] = jsonTimetable.asJsonObject[Favourite.TAG_STOP].asString
                timetable[Favourite.TAG_LINE] = jsonTimetable.asJsonObject[Favourite.TAG_LINE].asString
                timetables.add(timetable)
            }
            favourites[name] = Favourite(name, timetables)
        }
    }

    override fun iterator(): Iterator<Favourite> = favourites.values.iterator()

    fun has(name: String): Boolean = favourites.contains(name)

    fun add(name: String, timetables: ArrayList<HashMap<String, String>>) {
        if (favourites[name] == null) {
            favourites[name] = Favourite(name, timetables)
            serialize()
        }
    }

    fun add(name: String, favourite: Favourite) {
        if (favourites[name] == null) {
            favourites[name] = favourite
            serialize()
        }
    }

    fun delete(name: String) {
        favourites.remove(name)
        serialize()
    }

    fun delete(name: String, stop: String, line: String) {
        favourites[name]?.delete(stop, line)
        serialize()
    }

    private fun serialize() {
        val rootObject = JsonObject()
        for ((name, favourite) in favourites) {
            val timetables = JsonArray()
            for (timetable in favourite.timetables) {
                val element = JsonObject()
                element.addProperty(Favourite.TAG_STOP, timetable[Favourite.TAG_STOP])
                element.addProperty(Favourite.TAG_LINE, timetable[Favourite.TAG_LINE])
                timetables.add(element)
            }
            rootObject.add(name, timetables)
        }
        val favouritesString = Gson().toJson(rootObject)
        val editor = preferences.edit()
        editor.putString("favourites", favouritesString)
        editor.apply()
    }

    fun detach(name: String, stop: String, line: String, newName: String) {
        val element = HashMap<String, String>()
        element[Favourite.TAG_STOP] = stop
        element[Favourite.TAG_LINE] = line
        val array = ArrayList<HashMap<String, String>>()
        array.add(element)
        favourites[newName] = Favourite(newName, array)
        serialize()

        delete(name, stop, line)
    }

    fun merge(names: ArrayList<String>) {
        if (names.size < 2)
            return
        val newFavourite = Favourite(names[0], ArrayList())
        for (name in names) {
            newFavourite.timetables.addAll(favourites[name]!!.timetables)
            favourites.remove(name)
        }
        favourites[names[0]] = newFavourite

        serialize()
    }

    fun rename(oldName: String, newName: String) {
        val favourite = favourites[oldName] ?: return
        favourite.rename(newName)
        favourites.remove(oldName)
        favourites[newName] = favourite
        serialize()
    }
}