-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewScope.kt
More file actions
94 lines (82 loc) · 3.09 KB
/
ViewScope.kt
File metadata and controls
94 lines (82 loc) · 3.09 KB
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
/*
* Copyright (C) 2020 Sam Lu
*
* 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.
*/
@file:Suppress("unused")
package <change_to_your_package_name>.ViewScope
import android.util.Log
import android.view.View
import androidx.core.view.ViewCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import java.io.Closeable
import kotlin.coroutines.CoroutineContext
private const val KEY_VIEW_SCOPE = R.id.view_scope
private const val LOG_TAG = "ViewScope"
/**
* [CoroutineScope] tied to this [View].
* This scope will be canceled when the view is detached from a window.
*
* This scope is bound to [Dispatchers.Main.immediate]
*
* An example:
* ```
* val view: View = ...
* view.viewScope.launch {
* // this statement will be run in the UI thread
* withContext(Dispatchers.Default) {
* // statements of this block are running in a worker thread and will be canceled when
* // this view is detached from a window (I.e. after Activity.onDestroy(),
* // Fragment.onDestroyView() called or you manually removed this view from the view tree).
* }
* // this statement will be run in the UI thread
* }
* ```
*/
val View.viewScope: CoroutineScope
get() {
getTag(KEY_VIEW_SCOPE)?.let {
if (it is CoroutineScope)
return it
else
Log.e(LOG_TAG, "check why the value of KEY_VIEW_SCOPE is ${it.javaClass.name}")
}
//////////////////////
val scope = CloseableCoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
setTag(KEY_VIEW_SCOPE, scope)
//////////////////////
if (!ViewCompat.isAttachedToWindow(this)) {
Log.w(LOG_TAG,
"Creating a CoroutineScope before ${javaClass.name} attaches to a window. " +
"Coroutine jobs won't be canceled if the view has never been attached to a window.")
}
//////////////////////
addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
override fun onViewAttachedToWindow(view: View) {}
override fun onViewDetachedFromWindow(view: View) {
removeOnAttachStateChangeListener(this)
setTag(KEY_VIEW_SCOPE, null)
scope.close()
}
})
return scope
}
internal class CloseableCoroutineScope(context: CoroutineContext) : Closeable, CoroutineScope {
override val coroutineContext: CoroutineContext = context
override fun close() {
coroutineContext.cancel()
}
}