Bimba.git

ref: 2b948f4e23bba5facf231aae48cb1185adacd702

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
package ml.adamsprogs.bimba.models

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


class FavouriteStorage private constructor(context: Context) {
    companion object {
        private var favouriteStorage: FavouriteStorage? = null
        fun getFavouriteStorage(context: Context? = null): FavouriteStorage {
            if (favouriteStorage == null) {
                if (context == null)
                    throw IllegalArgumentException("requested new storage context not given")
                else {
                    favouriteStorage = FavouriteStorage(context)
                    return favouriteStorage as FavouriteStorage
                }
            } else
                return favouriteStorage as FavouriteStorage
        }
    }
    val favourites = HashMap<String, Favourite>()
    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["stop"] = jsonTimetable.asJsonObject["stop"].asString
                timetable["line"] = jsonTimetable.asJsonObject["line"].asString
                timetables.add(timetable)
            }
            favourites[name] = Favourite(name, timetables)
        }
    }

    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()
    }

    fun serialize() {
        val rootObject = JsonObject()
        for ((name, favourite) in favourites) {
            val timetables = JsonArray()
            for (timetable in favourite.timetables) {
                val element = JsonObject()
                element.addProperty("stop", timetable["stop"])
                element.addProperty("line", timetable["line"])
                timetables.add(element)
            }
            rootObject.add(name, timetables)
        }
        val favouritesString = Gson().toJson(rootObject)
        Log.i("FAB", favouritesString)
        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["stop"] = stop
        element["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<HashMap<String, String>>())
        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.name = newName
        favourites.remove(oldName)
        favourites[newName] = favourite
    }
}