aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/utils/EnumUtils.kt
blob: b6d9b8338c51903eba6cdbad8702a9238e0323ea (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
/*
 * Copyright 2018 Allan Wang
 *
 * 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 <http://www.gnu.org/licenses/>.
 */
package com.pitchedapps.frost.utils

import android.content.Intent
import android.os.BaseBundle

/**
 * Created by Allan Wang on 29/12/17.
 *
 * Helper to set enum using its name rather than the serialized version
 * Name is used in case the enum is involved in persistent data, where updates may shift indices
 */
interface EnumBundle<E : Enum<E>> {

    val bundleContract: EnumBundleCompanion<E>

    val name: String

    val ordinal: Int

    fun put(intent: Intent) {
        intent.putExtra(bundleContract.argTag, name)
    }

    fun put(bundle: BaseBundle?) {
        bundle?.putString(bundleContract.argTag, name)
    }
}

interface EnumBundleCompanion<E : Enum<E>> {

    val argTag: String

    val values: Array<E>

    val valueMap: Map<String, E>

    operator fun get(name: String?) = if (name == null) null else valueMap[name]

    operator fun get(bundle: BaseBundle?) = get(bundle?.getString(argTag))

    operator fun get(intent: Intent?) = get(intent?.getStringExtra(argTag))
}

open class EnumCompanion<E : Enum<E>>(
    final override val argTag: String,
    final override val values: Array<E>
) : EnumBundleCompanion<E> {

    final override val valueMap: Map<String, E> = values.map { it.name to it }.toMap()

    final override fun get(name: String?) = super.get(name)

    final override fun get(bundle: BaseBundle?) = super.get(bundle)

    final override fun get(intent: Intent?) = super.get(intent)
}