aboutsummaryrefslogtreecommitdiff
path: root/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KPrefBinder.kt
blob: 764831e59cb4aaf8e8c5735379697034b8d0a343 (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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright 2018 Allan Wang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ca.allanwang.kau.kpref.activity

import androidx.annotation.StringRes
import ca.allanwang.kau.kpref.activity.items.KPrefCheckbox
import ca.allanwang.kau.kpref.activity.items.KPrefColorPicker
import ca.allanwang.kau.kpref.activity.items.KPrefHeader
import ca.allanwang.kau.kpref.activity.items.KPrefItemBase
import ca.allanwang.kau.kpref.activity.items.KPrefItemCore
import ca.allanwang.kau.kpref.activity.items.KPrefPlainText
import ca.allanwang.kau.kpref.activity.items.KPrefSeekbar
import ca.allanwang.kau.kpref.activity.items.KPrefSubItems
import ca.allanwang.kau.kpref.activity.items.KPrefText
import ca.allanwang.kau.kpref.activity.items.KPrefTimePicker

/**
 * Created by Allan Wang on 2017-06-08.
 *
 * Houses all the components that can be called externally to setup the kpref mainAdapter
 */
@DslMarker
annotation class KPrefMarker

/**
 * Contains attributes shared amongst all kpref items
 */
@KPrefMarker
interface CoreAttributeContract {
    var textColor: (() -> Int)?
    var accentColor: (() -> Int)?
}

/**
 * Implementation of [CoreAttributeContract]
 */
class CoreAttributeBuilder : CoreAttributeContract {
    override var textColor: (() -> Int)? = null
    override var accentColor: (() -> Int)? = textColor
}

interface KPrefActivityContract {
    fun showNextPrefs(@StringRes toolbarTitleRes: Int, builder: KPrefAdapterBuilder.() -> Unit)
    val hasPrevPrefs: Boolean
    fun showPrevPrefs()
    fun reloadByTitle(@StringRes vararg title: Int)
}

class GlobalOptions(
    core: CoreAttributeContract,
    activity: KPrefActivityContract
) : CoreAttributeContract by core, KPrefActivityContract by activity

/**
 * Builder for kpref items
 * Contains DSLs for every possible item
 * The arguments are all the mandatory values plus an optional builder housing all the possible configurations
 * The mandatory values are final so they cannot be edited in the builder
 *
 * This function will be called asynchronously, so don't worry about blocking the thread
 * The recycler will only animate once this is completed though
 */
@KPrefMarker
class KPrefAdapterBuilder(val globalOptions: GlobalOptions) {

    @KPrefMarker
    fun header(@StringRes title: Int) = list.add(KPrefHeader(KPrefItemCore.CoreBuilder(globalOptions, title)))

    @KPrefMarker
    fun checkbox(
        @StringRes title: Int,
        getter: () -> Boolean,
        setter: KPrefItemActions.(value: Boolean) -> Unit,
        builder: KPrefItemBase.BaseContract<Boolean>.() -> Unit = {}
    ) = list.add(
        KPrefCheckbox(KPrefItemBase.BaseBuilder(globalOptions, title, getter, setter)
            .apply { builder() })
    )

    @KPrefMarker
    fun colorPicker(
        @StringRes title: Int,
        getter: () -> Int,
        setter: KPrefItemActions.(value: Int) -> Unit,
        builder: KPrefColorPicker.KPrefColorContract.() -> Unit = {}
    ) = list.add(
        KPrefColorPicker(KPrefColorPicker.KPrefColorBuilder(globalOptions, title, getter, setter)
            .apply { builder() })
    )

    @KPrefMarker
    fun <T> text(
        @StringRes title: Int,
        getter: () -> T,
        setter: KPrefItemActions.(value: T) -> Unit,
        builder: KPrefText.KPrefTextContract<T>.() -> Unit = {}
    ) = list.add(
        KPrefText(KPrefText.KPrefTextBuilder(globalOptions, title, getter, setter)
            .apply { builder() })
    )

    @KPrefMarker
    fun subItems(
        @StringRes title: Int,
        itemBuilder: KPrefAdapterBuilder.() -> Unit,
        builder: KPrefSubItems.KPrefSubItemsContract.() -> Unit
    ) = list.add(
        KPrefSubItems(KPrefSubItems.KPrefSubItemsBuilder(globalOptions, title, itemBuilder)
            .apply { builder() })
    )

    @KPrefMarker
    fun plainText(
        @StringRes title: Int,
        builder: KPrefItemBase.BaseContract<Unit>.() -> Unit = {}
    ) = list.add(
        KPrefPlainText(KPrefPlainText.KPrefPlainTextBuilder(globalOptions, title)
            .apply { builder() })
    )

    @KPrefMarker
    fun seekbar(
        @StringRes title: Int,
        getter: () -> Int,
        setter: KPrefItemActions.(value: Int) -> Unit,
        builder: KPrefSeekbar.KPrefSeekbarContract.() -> Unit = {}
    ) = list.add(
        KPrefSeekbar(KPrefSeekbar.KPrefSeekbarBuilder(globalOptions, title, getter, setter)
            .apply { builder() })
    )

    @KPrefMarker
    fun timePicker(
        @StringRes title: Int,
        getter: () -> Int,
        setter: KPrefItemActions.(value: Int) -> Unit,
        builder: KPrefTimePicker.KPrefTimeContract.() -> Unit = {}
    ) = list.add(
        KPrefTimePicker(KPrefTimePicker.KPrefTimeBuilder(globalOptions, title, getter, setter)
            .apply { builder() })
    )

    @KPrefMarker
    val list: MutableList<KPrefItemCore> = mutableListOf()
}