| author | Da Risk <da_risk@geekorum.com> |
| Tue, 16 Jan 2024 17:05:53 -0400 | |
| changeset 65 | 262e1b65de7d |
| parent 61 | 7a8bbdc7b290 |
| child 75 | 534a19e25217 |
| permissions | -rw-r--r-- |
| 20 | 1 |
/* |
| 1 | 2 |
* Geekdroid is a utility library for development on the Android |
3 |
* Platform. |
|
4 |
* |
|
| 61 | 5 |
* Copyright (C) 2017-2024 by Frederic-Charles Barthelery. |
| 1 | 6 |
* |
7 |
* This file is part of Geekdroid. |
|
8 |
* |
|
9 |
* Geekdroid is free software: you can redistribute it and/or modify |
|
10 |
* it under the terms of the GNU General Public License as published by |
|
11 |
* the Free Software Foundation, either version 3 of the License, or |
|
12 |
* (at your option) any later version. |
|
13 |
* |
|
14 |
* Geekdroid is distributed in the hope that it will be useful, |
|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 |
* GNU General Public License for more details. |
|
18 |
* |
|
19 |
* You should have received a copy of the GNU General Public License |
|
20 |
* along with Geekdroid. If not, see <http://www.gnu.org/licenses/>. |
|
21 |
*/ |
|
22 |
package com.geekorum.geekdroid.gms |
|
23 |
||
24 |
import com.google.android.gms.tasks.Task |
|
25 |
import kotlinx.coroutines.CompletableDeferred |
|
26 |
import kotlinx.coroutines.Deferred |
|
27 |
||
28 |
||
29 |
/** |
|
30 |
* Converts this task to an instance of [Deferred]. |
|
31 |
*/ |
|
|
35
626c1ea36cb9
geekdroid-firebase: remove Play core task extensions as latest version use gms Tasks
Da Risk <da_risk@geekorum.com>
parents:
20
diff
changeset
|
32 |
fun <T> Task<T>.asDeferred(): Deferred<T> {
|
| 1 | 33 |
if (isComplete) {
|
34 |
val e = exception |
|
35 |
return if (e == null) {
|
|
36 |
@Suppress("UNCHECKED_CAST")
|
|
37 |
CompletableDeferred<T>().apply { complete(result as T) }
|
|
38 |
} else {
|
|
39 |
CompletableDeferred<T>().apply { completeExceptionally(e) }
|
|
40 |
} |
|
41 |
} |
|
42 |
||
43 |
val result = CompletableDeferred<T>() |
|
44 |
addOnCompleteListener {
|
|
45 |
val e = it.exception |
|
46 |
if (e == null) {
|
|
47 |
@Suppress("UNCHECKED_CAST")
|
|
48 |
result.complete(it.result as T) |
|
49 |
} else {
|
|
50 |
result.completeExceptionally(e) |
|
51 |
} |
|
52 |
} |
|
53 |
return result |
|
54 |
} |
|
55 |