Skip to content
This repository was archived by the owner on Jul 13, 2020. It is now read-only.

Commit f1f3033

Browse files
committed
Add some Javadoc comments
1 parent f8cc478 commit f1f3033

File tree

7 files changed

+212
-121
lines changed

7 files changed

+212
-121
lines changed

anko/library/static/common/src/Async.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,27 @@ import java.util.concurrent.ExecutorService
2525
import java.util.concurrent.Executors
2626
import java.util.concurrent.Future
2727

28+
/**
29+
* Execute [f] on the application UI thread.
30+
*/
2831
fun Context.runOnUiThread(f: Context.() -> Unit) {
2932
if (ContextHelper.mainThread == Thread.currentThread()) f() else ContextHelper.handler.post { f() }
3033
}
3134

35+
/**
36+
* Execute [f] on the application UI thread.
37+
*/
3238
inline fun Fragment.runOnUiThread(crossinline f: () -> Unit) {
3339
activity?.runOnUiThread { f() }
3440
}
3541

3642
class AnkoAsyncContext<T>(val weakRef: WeakReference<T>)
3743

44+
/**
45+
* Execute [f] on the application UI thread.
46+
* If the [async] receiver still exists (was not collected by GC),
47+
* [f] gets it as a parameter ([f] gets null if the receiver does not exist anymore).
48+
*/
3849
fun <T> AnkoAsyncContext<T>.onComplete(f: (T?) -> Unit) {
3950
val ref = weakRef.get()
4051
if (ContextHelper.mainThread == Thread.currentThread()) {
@@ -44,6 +55,11 @@ fun <T> AnkoAsyncContext<T>.onComplete(f: (T?) -> Unit) {
4455
}
4556
}
4657

58+
/**
59+
* Execute [f] on the application UI thread.
60+
* [async] receiver will be passed to [f].
61+
* If the receiver does not exist anymore (it was collected by GC), [f] will not be executed.
62+
*/
4763
fun <T> AnkoAsyncContext<T>.uiThread(f: (T) -> Unit): Boolean {
4864
val ref = weakRef.get() ?: return false
4965
if (ContextHelper.mainThread == Thread.currentThread()) {
@@ -54,6 +70,11 @@ fun <T> AnkoAsyncContext<T>.uiThread(f: (T) -> Unit): Boolean {
5470
return true
5571
}
5672

73+
/**
74+
* Execute [f] on the application UI thread if the underlying [Activity] still exists and is not finished.
75+
* The receiver [Activity] will be passed to [f].
76+
* If it is not exist anymore or if it was finished, [f] will not be called.
77+
*/
5778
fun <T: Activity> AnkoAsyncContext<T>.activityUiThread(f: (T) -> Unit): Boolean {
5879
val activity = weakRef.get() ?: return false
5980
if (activity.isFinishing) return false
@@ -100,6 +121,13 @@ fun <T: Fragment> AnkoAsyncContext<T>.fragmentUiThreadWithContext(f: Context.(T)
100121
return true
101122
}
102123

124+
/**
125+
* Execute [task] asynchronously.
126+
*
127+
* @param exceptionHandler optional exception handler.
128+
* If defined, any exceptions thrown inside [task] will be passed to it. If not, exceptions will be ignored.
129+
* @param task the code to execute asynchronously.
130+
*/
103131
fun <T> T.doAsync(
104132
exceptionHandler: ((Throwable) -> Unit)? = null,
105133
task: AnkoAsyncContext<T>.() -> Unit

anko/library/static/common/src/Arrays.kt renamed to anko/library/static/common/src/collections/Arrays.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,35 @@ import android.util.SparseBooleanArray
2121
import android.util.SparseIntArray
2222
import java.util.*
2323

24+
/**
25+
* Iterate the receiver [Array] using an index.
26+
*
27+
* @f an action to invoke on each array element.
28+
*/
2429
inline fun <T> Array<T>.forEachByIndex(f: (T) -> Unit) {
2530
val lastIndex = size - 1
2631
for (i in 0..lastIndex) {
2732
f(get(i))
2833
}
2934
}
3035

36+
/**
37+
* Iterate the receiver [Array] using an index.
38+
*
39+
* @f an action to invoke on each array element (index, element).
40+
*/
3141
inline fun <T> Array<T>.forEachWithIndex(f: (Int, T) -> Unit) {
3242
val lastIndex = size - 1
3343
for (i in 0..lastIndex) {
3444
f(i, get(i))
3545
}
3646
}
3747

48+
/**
49+
* Iterate the receiver [Array] backwards using an index.
50+
*
51+
* @f an action to invoke on each array element.
52+
*/
3853
inline fun <T> Array<T>.forEachReversedByIndex(f: (T) -> Unit) {
3954
var i = size - 1
4055
while (i >= 0) {
@@ -43,6 +58,11 @@ inline fun <T> Array<T>.forEachReversedByIndex(f: (T) -> Unit) {
4358
}
4459
}
4560

61+
/**
62+
* Iterate the receiver [Array] backwards using an index.
63+
*
64+
* @f an action to invoke on each array element (index, element).
65+
*/
4666
inline fun <T> Array<T>.forEachReversedWithIndex(f: (Int, T) -> Unit) {
4767
var i = size - 1
4868
while (i >= 0) {
@@ -51,10 +71,19 @@ inline fun <T> Array<T>.forEachReversedWithIndex(f: (Int, T) -> Unit) {
5171
}
5272
}
5373

74+
/**
75+
* Create a [Sequence] instance that wraps the original [SparseArray] returning its elements when being iterated.
76+
*/
5477
fun <T> SparseArray<T>.asSequence(): Sequence<T> = SparseArraySequence(this)
5578

79+
/**
80+
* Create a [Sequence] instance that wraps the original [SparseBooleanArray] returning its elements when being iterated.
81+
*/
5682
fun <T> SparseBooleanArray.asSequence(): Sequence<Boolean> = SparseBooleanArraySequence(this)
5783

84+
/**
85+
* Create a [Sequence] instance that wraps the original [SparseIntArray] returning its elements when being iterated.
86+
*/
5887
fun <T> SparseIntArray.asSequence(): Sequence<Int> = SparseIntArraySequence(this)
5988

6089
private class SparseArraySequence<T>(private val a: SparseArray<T>) : Sequence<T> {

anko/library/static/common/src/Collections.kt renamed to anko/library/static/common/src/collections/Collections.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.jetbrains.anko.collections
1818

1919
/**
20-
* Iterate the [List] using an index.
20+
* Iterate the receiver [List] using an index.
2121
*
2222
* @f an action to invoke on each list element.
2323
*/
@@ -29,7 +29,7 @@ inline fun <T> List<T>.forEachByIndex(f: (T) -> Unit) {
2929
}
3030

3131
/**
32-
* Iterate the [List] using an index.
32+
* Iterate the receiver [List] using an index.
3333
*
3434
* @f an action to invoke on each list element (index, element).
3535
*/
@@ -41,7 +41,7 @@ inline fun <T> List<T>.forEachWithIndex(f: (Int, T) -> Unit) {
4141
}
4242

4343
/**
44-
* Iterate the [List] backwards using an index.
44+
* Iterate the receiver [List] backwards using an index.
4545
*
4646
* @f an action to invoke on each list element.
4747
*/
@@ -54,7 +54,7 @@ inline fun <T> List<T>.forEachReversedByIndex(f: (T) -> Unit) {
5454
}
5555

5656
/**
57-
* Iterate the [List] backwards using an index.
57+
* Iterate the receiver [List] backwards using an index.
5858
*
5959
* @f an action to invoke on each list element (index, element).
6060
*/

anko/library/static/common/src/Dialogs.kt renamed to anko/library/static/common/src/dialogs/Dialogs.kt

Lines changed: 0 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -22,93 +22,6 @@ import android.app.ProgressDialog
2222
import android.content.Context
2323
import android.widget.Toast
2424

25-
/**
26-
* Display the simple Toast message with the [Toast.LENGTH_SHORT] duration.
27-
*
28-
* @param message the message text resource.
29-
*/
30-
inline fun AnkoContext<*>.toast(message: Int) = ctx.toast(message)
31-
32-
/**
33-
* Display the simple Toast message with the [Toast.LENGTH_SHORT] duration.
34-
*
35-
* @param message the message text resource.
36-
*/
37-
inline fun Fragment.toast(message: Int): Unit = activity.toast(message)
38-
39-
/**
40-
* Display the simple Toast message with the [Toast.LENGTH_SHORT] duration.
41-
*
42-
* @param message the message text resource.
43-
*/
44-
fun Context.toast(message: Int) = Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
45-
46-
/**
47-
* Display the simple Toast message with the [Toast.LENGTH_SHORT] duration.
48-
*
49-
* @param message the message text.
50-
*/
51-
inline fun AnkoContext<*>.toast(message: CharSequence) = ctx.toast(message)
52-
53-
/**
54-
* Display the simple Toast message with the [Toast.LENGTH_SHORT] duration.
55-
*
56-
* @param message the message text.
57-
*/
58-
inline fun Fragment.toast(message: CharSequence): Unit = activity.toast(message)
59-
60-
/**
61-
* Display the simple Toast message with the [Toast.LENGTH_SHORT] duration.
62-
*
63-
* @param message the message text.
64-
*/
65-
fun Context.toast(message: CharSequence) = Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
66-
67-
/**
68-
* Display the simple Toast message with the [Toast.LENGTH_LONG] duration.
69-
*
70-
* @param message the message text resource.
71-
*/
72-
inline fun AnkoContext<*>.longToast(message: Int) = ctx.longToast(message)
73-
74-
/**
75-
* Display the simple Toast message with the [Toast.LENGTH_LONG] duration.
76-
*
77-
* @param message the message text resource.
78-
*/
79-
inline fun Fragment.longToast(message: Int): Unit = activity.longToast(message)
80-
81-
/**
82-
* Display the simple Toast message with the [Toast.LENGTH_LONG] duration.
83-
*
84-
* @param message the message text resource.
85-
*/
86-
fun Context.longToast(message: Int) = Toast.makeText(this, message, Toast.LENGTH_LONG).show()
87-
88-
/**
89-
* Display the simple Toast message with the [Toast.LENGTH_LONG] duration.
90-
*
91-
* @param message the message text.
92-
*/
93-
inline fun AnkoContext<*>.longToast(message: CharSequence) = ctx.longToast(message)
94-
95-
/**
96-
* Display the simple Toast message with the [Toast.LENGTH_LONG] duration.
97-
*
98-
* @param message the message text.
99-
*/
100-
inline fun Fragment.longToast(message: CharSequence): Unit = activity.longToast(message)
101-
102-
/**
103-
* Display the simple Toast message with the [Toast.LENGTH_LONG] duration.
104-
*
105-
* @param message the message text.
106-
*/
107-
fun Context.longToast(message: CharSequence) = Toast.makeText(this, message, Toast.LENGTH_LONG).show()
108-
109-
110-
/* Alerts */
111-
11225
inline fun AnkoContext<*>.alert(
11326
message: String,
11427
title: String? = null,
@@ -160,9 +73,6 @@ inline fun Fragment.alert(noinline init: AlertDialogBuilder.() -> Unit): AlertDi
16073

16174
fun Context.alert(init: AlertDialogBuilder.() -> Unit) = AlertDialogBuilder(this).apply { init() }
16275

163-
164-
/* Progress dialogs */
165-
16676
inline fun AnkoContext<*>.progressDialog(
16777
message: Int? = null,
16878
title: Int? = null,
@@ -251,31 +161,4 @@ private fun Context.progressDialog(
251161
if (title != null) setTitle(title)
252162
if (init != null) init()
253163
show()
254-
}
255-
256-
257-
/* Selectors */
258-
259-
inline fun AnkoContext<*>.selector(
260-
title: CharSequence? = null,
261-
items: List<CharSequence>,
262-
noinline onClick: (Int) -> Unit
263-
): Unit = ctx.selector(title, items, onClick)
264-
265-
inline fun Fragment.selector(
266-
title: CharSequence? = null,
267-
items: List<CharSequence>,
268-
noinline onClick: (Int) -> Unit
269-
): Unit = activity.selector(title, items, onClick)
270-
271-
fun Context.selector(
272-
title: CharSequence? = null,
273-
items: List<CharSequence>,
274-
onClick: (Int) -> Unit
275-
) {
276-
with(AlertDialogBuilder(this)) {
277-
if (title != null) title(title)
278-
items(items, onClick)
279-
show()
280-
}
281164
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2016 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
@file:Suppress("NOTHING_TO_INLINE")
18+
package org.jetbrains.anko
19+
20+
import android.app.Fragment
21+
import android.content.Context
22+
23+
inline fun AnkoContext<*>.selector(
24+
title: CharSequence? = null,
25+
items: List<CharSequence>,
26+
noinline onClick: (Int) -> Unit
27+
): Unit = ctx.selector(title, items, onClick)
28+
29+
inline fun Fragment.selector(
30+
title: CharSequence? = null,
31+
items: List<CharSequence>,
32+
noinline onClick: (Int) -> Unit
33+
): Unit = activity.selector(title, items, onClick)
34+
35+
fun Context.selector(
36+
title: CharSequence? = null,
37+
items: List<CharSequence>,
38+
onClick: (Int) -> Unit
39+
) {
40+
with(AlertDialogBuilder(this)) {
41+
if (title != null) title(title)
42+
items(items, onClick)
43+
show()
44+
}
45+
}

0 commit comments

Comments
 (0)