Skip to content

Commit 502650d

Browse files
Merge pull request #18 from Omega-R/develop
Click optional, and minor fixes
2 parents 3c1eee5 + 2a36125 commit 502650d

File tree

5 files changed

+72
-24
lines changed

5 files changed

+72
-24
lines changed

lib/src/main/java/com/omega_r/base/clickers/OmegaClickable.kt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.omega_r.base.clickers
22

3-
import android.os.Build
43
import android.view.View
54
import androidx.core.view.ViewCompat
65
import com.omega_r.base.OmegaViewFindable
@@ -23,10 +22,18 @@ interface OmegaClickable: OmegaViewFindable {
2322
findViewById<View>(id)!!.setOnClickListener(clickManager.wrap(id, listener))
2423
}
2524

25+
fun setOnClickListenerOptional(id: Int, listener: View.OnClickListener) {
26+
findViewById<View>(id)?.setOnClickListener(clickManager.wrap(id, listener))
27+
}
28+
2629
fun setOnClickListener(id: Int, block: () -> Unit) {
2730
findViewById<View>(id)!!.setOnClickListener(clickManager.wrap(id, block))
2831
}
2932

33+
fun setOnClickListenerOptional(id: Int, block: () -> Unit) {
34+
findViewById<View>(id)?.setOnClickListener(clickManager.wrap(id, block))
35+
}
36+
3037
fun setOnClickListenerWithView(id: Int, block: (View) -> Unit) {
3138
findViewById<View>(id)!!.setOnClickListener(clickManager.wrap(id, block))
3239
}
@@ -35,10 +42,18 @@ interface OmegaClickable: OmegaViewFindable {
3542
pairs.forEach { setOnClickListener(it.first, it.second) }
3643
}
3744

45+
fun setOnClickListenersOptional(vararg pairs: Pair<Int, () -> Unit>) {
46+
pairs.forEach { setOnClickListenerOptional(it.first, it.second) }
47+
}
48+
3849
fun setOnClickListeners(vararg ids: Int, block: (View) -> Unit) {
3950
ids.forEach { findViewById<View>(it)!!.setOnClickListener(clickManager.wrap(it, block)) }
4051
}
4152

53+
fun setOnClickListenersOptional(vararg ids: Int, block: (View) -> Unit) {
54+
ids.forEach { findViewById<View>(it)?.setOnClickListener(clickManager.wrap(it, block)) }
55+
}
56+
4257
fun <E> setOnClickListeners(vararg pairs: Pair<Int, E>, block: (E) -> Unit) {
4358
val list = pairs.map { it.first }
4459
val map = pairs.toMap()
@@ -47,6 +62,14 @@ interface OmegaClickable: OmegaViewFindable {
4762
}
4863
}
4964

65+
fun <E> setOnClickListenersOptional(vararg pairs: Pair<Int, E>, block: (E) -> Unit) {
66+
val list = pairs.map { it.first }
67+
val map = pairs.toMap()
68+
setOnClickListenersOptional(ids = *list.toIntArray()) {
69+
block(map[it.id]!!)
70+
}
71+
}
72+
5073
fun setMenuListener(vararg pairs: Pair<Int, () -> Unit>) {
5174
pairs.forEach { setMenuListener(it.first, it.second) }
5275
}

lib/src/main/java/com/omega_r/base/components/OmegaBottomSheetDialogFragment.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ abstract class OmegaBottomSheetDialogFragment : MvpBottomSheetDialogFragment(),
159159
}
160160
}
161161

162+
override fun setResult(resultCode: Int) {
163+
activity?.setResult(resultCode)
164+
}
165+
166+
override fun setResult(resultCode: Int, intent: Intent) {
167+
activity?.setResult(resultCode, intent)
168+
}
169+
162170
override fun onStop() {
163171
super.onStop()
164172
dialogList.forEach {

lib/src/main/java/com/omega_r/base/data/OmegaRepository.kt

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class OmegaRepository<SOURCE : Source>(vararg sources: SOURCE) {
4242
CACHE_AND_REMOTE -> applyCacheAndRemote(block)
4343
MEMORY_ELSE_CACHE_AND_REMOTE -> {
4444
if (memoryCacheSource != null) {
45-
ignoreSourceException {
45+
ignoreException {
4646
send(block(memoryCacheSource as SOURCE))
4747
return@produce
4848
}
@@ -55,10 +55,10 @@ class OmegaRepository<SOURCE : Source>(vararg sources: SOURCE) {
5555

5656

5757
private suspend fun <R> ProducerScope<R>.applyOnlyRemote(block: suspend SOURCE.() -> R) {
58-
var remoteException: AppException? = null
58+
var remoteException: Exception? = null
5959

6060
if (remoteSource != null) {
61-
remoteException = ignoreSourceException {
61+
remoteException = ignoreException {
6262
val result = block(remoteSource)
6363
send(result)
6464
memoryCacheSource?.update(result)
@@ -68,7 +68,7 @@ class OmegaRepository<SOURCE : Source>(vararg sources: SOURCE) {
6868
}
6969

7070
if (defaultSource != null) {
71-
ignoreSourceException {
71+
ignoreException {
7272
return send(block(defaultSource))
7373
}
7474
}
@@ -82,14 +82,14 @@ class OmegaRepository<SOURCE : Source>(vararg sources: SOURCE) {
8282

8383
private suspend fun <R> ProducerScope<R>.applyOnlyCache(block: suspend SOURCE.() -> R) {
8484
if (memoryCacheSource != null) {
85-
ignoreSourceException {
85+
ignoreException {
8686
send(block(memoryCacheSource as SOURCE))
8787
return
8888
}
8989
}
9090

9191
if (fileCacheSource != null) {
92-
ignoreSourceException {
92+
ignoreException {
9393
val result = block(fileCacheSource as SOURCE)
9494
send(result)
9595
memoryCacheSource?.update(result)
@@ -98,7 +98,7 @@ class OmegaRepository<SOURCE : Source>(vararg sources: SOURCE) {
9898
}
9999

100100
if (defaultSource != null) {
101-
ignoreSourceException {
101+
ignoreException {
102102
return send(block(defaultSource))
103103
}
104104
}
@@ -107,9 +107,9 @@ class OmegaRepository<SOURCE : Source>(vararg sources: SOURCE) {
107107
}
108108

109109
private suspend fun <R> ProducerScope<R>.applyRemoteElseCache(block: suspend SOURCE.() -> R) {
110-
var remoteException: AppException? = null
110+
var remoteException: Exception? = null
111111
if (remoteSource != null) {
112-
remoteException = ignoreSourceException {
112+
remoteException = ignoreException {
113113
val result = block(remoteSource)
114114
send(result)
115115
memoryCacheSource?.update(result)
@@ -119,14 +119,14 @@ class OmegaRepository<SOURCE : Source>(vararg sources: SOURCE) {
119119
}
120120

121121
if (memoryCacheSource != null) {
122-
ignoreSourceException {
122+
ignoreException {
123123
send(block(memoryCacheSource as SOURCE))
124124
return
125125
}
126126
}
127127

128128
if (fileCacheSource != null) {
129-
ignoreSourceException {
129+
ignoreException {
130130
val result = block(fileCacheSource as SOURCE)
131131
send(result)
132132
memoryCacheSource?.update(result)
@@ -135,7 +135,7 @@ class OmegaRepository<SOURCE : Source>(vararg sources: SOURCE) {
135135
}
136136

137137
if (defaultSource != null) {
138-
ignoreSourceException {
138+
ignoreException {
139139
return send(block(defaultSource))
140140
}
141141
}
@@ -145,25 +145,25 @@ class OmegaRepository<SOURCE : Source>(vararg sources: SOURCE) {
145145

146146
private suspend fun <R> ProducerScope<R>.applyCacheElseRemote(block: suspend SOURCE.() -> R) {
147147
if (memoryCacheSource != null) {
148-
ignoreSourceException {
148+
ignoreException {
149149
send(block(memoryCacheSource as SOURCE))
150150
return
151151
}
152152
}
153153

154154
if (fileCacheSource != null) {
155-
ignoreSourceException {
155+
ignoreException {
156156
val result = block(fileCacheSource as SOURCE)
157157
send(result)
158158
memoryCacheSource?.update(result)
159159
return
160160
}
161161
}
162162

163-
var remoteException: AppException? = null
163+
var remoteException: Exception? = null
164164

165165
if (remoteSource != null) {
166-
remoteException = ignoreSourceException {
166+
remoteException = ignoreException {
167167
val result = block(remoteSource)
168168
send(result)
169169
memoryCacheSource?.update(result)
@@ -173,7 +173,7 @@ class OmegaRepository<SOURCE : Source>(vararg sources: SOURCE) {
173173
}
174174

175175
if (defaultSource != null) {
176-
ignoreSourceException {
176+
ignoreException {
177177
return send(block(defaultSource))
178178
}
179179
}
@@ -188,7 +188,7 @@ class OmegaRepository<SOURCE : Source>(vararg sources: SOURCE) {
188188
private suspend fun <R> ProducerScope<R>.applyCacheAndRemote(block: suspend SOURCE.() -> R) {
189189
val cacheReturnDeferred = async {
190190
if (memoryCacheSource != null) {
191-
ignoreSourceException {
191+
ignoreException {
192192
val result = block(memoryCacheSource as SOURCE)
193193

194194
if (isActive && !isClosedForSend) {
@@ -198,7 +198,7 @@ class OmegaRepository<SOURCE : Source>(vararg sources: SOURCE) {
198198
}
199199
}
200200
if (fileCacheSource != null) {
201-
ignoreSourceException {
201+
ignoreException {
202202
val result = block(fileCacheSource as SOURCE)
203203
if (isActive && !isClosedForSend) {
204204
send(result)
@@ -209,7 +209,7 @@ class OmegaRepository<SOURCE : Source>(vararg sources: SOURCE) {
209209
}
210210

211211
if (defaultSource != null) {
212-
ignoreSourceException {
212+
ignoreException {
213213
if (isActive && !isClosedForSend) {
214214
send(block(defaultSource))
215215
}
@@ -221,7 +221,7 @@ class OmegaRepository<SOURCE : Source>(vararg sources: SOURCE) {
221221
}
222222

223223
val remoteException = if (remoteSource != null) {
224-
ignoreSourceException {
224+
ignoreException {
225225
val result = block(remoteSource)
226226
cacheReturnDeferred.cancel()
227227
send(result)
@@ -255,11 +255,11 @@ class OmegaRepository<SOURCE : Source>(vararg sources: SOURCE) {
255255

256256
}
257257

258-
private inline fun ignoreSourceException(block: () -> Unit): AppException? {
258+
private inline fun ignoreException(block: () -> Unit): Exception? {
259259
return try {
260260
block()
261261
null
262-
} catch (exception: AppException) {
262+
} catch (exception: Exception) {
263263
exception
264264
}
265265
}

lib/src/main/java/com/omega_r/base/enitity/Identifiable.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,19 @@ interface Identifiable<T> {
1717
else -> id.hashCode().toLong()
1818
}
1919
}
20+
21+
fun <T : Identifiable<I>, I> Iterable<T>.contains(id: I): Boolean {
22+
return firstOrNull(predicate = { it.id == id }) != null
23+
}
24+
25+
fun <T : Identifiable<I>, I> Iterable<T>.indexOfFirst(id: I): Int {
26+
return indexOfFirst(predicate = { it.id == id })
27+
}
28+
29+
fun <T : Identifiable<I>, I> Iterable<T>.indexOfLast(id: I): Int {
30+
return indexOfLast(predicate = { it.id == id })
31+
}
32+
33+
fun <T : Identifiable<I>, I> Iterable<T>.firstOrNull(id: I): T? {
34+
return firstOrNull(predicate = { it.id == id })
35+
}

lib/src/main/java/com/omega_r/base/mvp/presenters/OmegaPresenter.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ open class OmegaPresenter<View : OmegaView> : MvpPresenter<View>(), CoroutineSco
4242
}
4343

4444
protected inline fun launchWithWaiting(crossinline block: suspend () -> Unit) = with(viewState) {
45+
setWaiting(true)
4546
launch {
4647
try {
4748
block()

0 commit comments

Comments
 (0)