aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/context/AppNotificationManager.kt
blob: 29f72dee179488e7830588916d5a834db1756e00 (plain)
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
/*
 * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon
 * Copyright (C) 2017-2023 Pacien TRAN-GIRARD
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package org.pacien.tincapp.context

import android.content.Context
import android.content.SharedPreferences.OnSharedPreferenceChangeListener

/**
 * @author pacien
 */
class AppNotificationManager(private val context: Context) {
  data class ErrorNotification(
    val title: String,
    val message: String,
    val manualLink: String?
  )

  companion object {
    private val STORE_NAME = this::class.java.`package`!!.name
    private const val STORE_KEY_TITLE = "title"
    private const val STORE_KEY_MESSAGE = "message"
    private const val STORE_KEY_MANUAL_LINK = "manual_link"
  }

  private val store by lazy { context.getSharedPreferences(STORE_NAME, Context.MODE_PRIVATE)!! }

  fun getError(): ErrorNotification? {
    if (!store.contains(STORE_KEY_TITLE)) return null;

    return ErrorNotification(
      store.getString(STORE_KEY_TITLE, null)!!,
      store.getString(STORE_KEY_MESSAGE, null)!!,
      store.getString(STORE_KEY_MANUAL_LINK, null)
    )
  }

  fun notifyError(title: String, message: String, manualLink: String? = null) {
    store
      .edit()
      .putString(STORE_KEY_TITLE, title)
      .putString(STORE_KEY_MESSAGE, message)
      .putString(STORE_KEY_MANUAL_LINK, manualLink)
      .apply()
  }

  fun dismissAll() {
    store
      .edit()
      .clear()
      .apply()
  }

  fun registerListener(listener: OnSharedPreferenceChangeListener) {
    store.registerOnSharedPreferenceChangeListener(listener)
  }

  fun unregisterListener(listener: OnSharedPreferenceChangeListener) {
    store.unregisterOnSharedPreferenceChangeListener(listener)
  }
}