|
| 1 | +package contributors |
| 2 | + |
| 3 | +import contributors.Contributors.LoadingStatus.* |
| 4 | +import contributors.Variant.* |
| 5 | +import kotlinx.coroutines.* |
| 6 | +import kotlinx.coroutines.swing.Swing |
| 7 | +import tasks.* |
| 8 | +import java.awt.event.ActionListener |
| 9 | +import javax.swing.SwingUtilities |
| 10 | +import kotlin.coroutines.CoroutineContext |
| 11 | + |
| 12 | +enum class Variant { |
| 13 | + BLOCKING, // Request1Blocking |
| 14 | + BACKGROUND, // Request2Background |
| 15 | + CALLBACKS, // Request3Callbacks |
| 16 | + SUSPEND, // Request4Coroutine |
| 17 | + CONCURRENT, // Request5Concurrent |
| 18 | + NOT_CANCELLABLE, // Request6NotCancellable |
| 19 | + PROGRESS, // Request6Progress |
| 20 | + CHANNELS // Request7Channels |
| 21 | +} |
| 22 | + |
| 23 | +interface Contributors: CoroutineScope { |
| 24 | + |
| 25 | + val job: Job |
| 26 | + |
| 27 | + override val coroutineContext: CoroutineContext |
| 28 | + get() = job + Dispatchers.Main |
| 29 | + |
| 30 | + fun init() { |
| 31 | + // Start a new loading on 'load' click |
| 32 | + addLoadListener { |
| 33 | + saveParams() |
| 34 | + loadContributors() |
| 35 | + } |
| 36 | + |
| 37 | + // Save preferences and exit on closing the window |
| 38 | + addOnWindowClosingListener { |
| 39 | + job.cancel() |
| 40 | + saveParams() |
| 41 | + System.exit(0) |
| 42 | + } |
| 43 | + |
| 44 | + // Load stored params (user & password values) |
| 45 | + loadInitialParams() |
| 46 | + } |
| 47 | + |
| 48 | + fun loadContributors() { |
| 49 | + val (username, password, org, _) = getParams() |
| 50 | + val req = RequestData(username, password, org) |
| 51 | + |
| 52 | + clearResults() |
| 53 | + val service = createGitHubService(req.username, req.password) |
| 54 | + |
| 55 | + val startTime = System.currentTimeMillis() |
| 56 | + when (getSelectedVariant()) { |
| 57 | + BLOCKING -> { // Blocking UI thread |
| 58 | + val users = loadContributorsBlocking(service, req) |
| 59 | + updateResults(users, startTime) |
| 60 | + } |
| 61 | + BACKGROUND -> { // Blocking a background thread |
| 62 | + loadContributorsBackground(service, req) { users -> |
| 63 | + SwingUtilities.invokeLater { |
| 64 | + updateResults(users, startTime) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + CALLBACKS -> { // Using callbacks |
| 69 | + loadContributorsCallbacks(service, req) { users -> |
| 70 | + SwingUtilities.invokeLater { |
| 71 | + updateResults(users, startTime) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + SUSPEND -> { // Using coroutines |
| 76 | + launch { |
| 77 | + val users = loadContributorsSuspend(service, req) |
| 78 | + updateResults(users, startTime) |
| 79 | + }.setUpCancellation() |
| 80 | + } |
| 81 | + CONCURRENT -> { // Performing requests concurrently |
| 82 | + launch(Dispatchers.Default) { |
| 83 | + val users = loadContributorsConcurrent(service, req) |
| 84 | + withContext(Dispatchers.Main) { |
| 85 | + updateResults(users, startTime) |
| 86 | + } |
| 87 | + }.setUpCancellation() |
| 88 | + } |
| 89 | + NOT_CANCELLABLE -> { // Performing requests in a non-cancellable way |
| 90 | + launch { |
| 91 | + val users = loadContributorsNotCancellable(service, req) |
| 92 | + updateResults(users, startTime) |
| 93 | + }.setUpCancellation() |
| 94 | + } |
| 95 | + PROGRESS -> { // Showing progress |
| 96 | + launch(Dispatchers.Default) { |
| 97 | + loadContributorsProgress(service, req) { users, completed -> |
| 98 | + withContext(Dispatchers.Main) { |
| 99 | + updateResults(users, startTime, completed) |
| 100 | + } |
| 101 | + } |
| 102 | + }.setUpCancellation() |
| 103 | + } |
| 104 | + CHANNELS -> { // Performing requests concurrently and showing progress |
| 105 | + launch(Dispatchers.Default) { |
| 106 | + loadContributorsChannels(service, req) { users, completed -> |
| 107 | + withContext(Dispatchers.Main) { |
| 108 | + updateResults(users, startTime, completed) |
| 109 | + } |
| 110 | + } |
| 111 | + }.setUpCancellation() |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private enum class LoadingStatus { COMPLETED, CANCELED, IN_PROGRESS } |
| 117 | + |
| 118 | + private fun clearResults() { |
| 119 | + updateContributors(listOf()) |
| 120 | + updateLoadingStatus(IN_PROGRESS) |
| 121 | + setActionsStatus(newLoadingEnabled = false) |
| 122 | + } |
| 123 | + |
| 124 | + private fun updateResults( |
| 125 | + users: List<User>, |
| 126 | + startTime: Long, |
| 127 | + completed: Boolean = true |
| 128 | + ) { |
| 129 | + updateContributors(users) |
| 130 | + updateLoadingStatus(if (completed) COMPLETED else IN_PROGRESS, startTime) |
| 131 | + if (completed) { |
| 132 | + setActionsStatus(newLoadingEnabled = true) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private fun updateLoadingStatus( |
| 137 | + status: LoadingStatus, |
| 138 | + startTime: Long? = null |
| 139 | + ) { |
| 140 | + val time = if (startTime != null) { |
| 141 | + val time = System.currentTimeMillis() - startTime |
| 142 | + "${(time / 1000)}.${time % 1000 / 100} sec" |
| 143 | + } else "" |
| 144 | + |
| 145 | + val text = "Loading status: " + |
| 146 | + when (status) { |
| 147 | + COMPLETED -> "completed in $time" |
| 148 | + IN_PROGRESS -> "in progress $time" |
| 149 | + CANCELED -> "canceled" |
| 150 | + } |
| 151 | + setLoadingStatus(text, status == IN_PROGRESS) |
| 152 | + } |
| 153 | + |
| 154 | + private fun Job.setUpCancellation() { |
| 155 | + // make active the 'cancel' button |
| 156 | + setActionsStatus(newLoadingEnabled = false, cancellationEnabled = true) |
| 157 | + |
| 158 | + val loadingJob = this |
| 159 | + |
| 160 | + // cancel the loading job if the 'cancel' button was clicked |
| 161 | + val listener = ActionListener { |
| 162 | + loadingJob.cancel() |
| 163 | + updateLoadingStatus(CANCELED) |
| 164 | + } |
| 165 | + addCancelListener(listener) |
| 166 | + |
| 167 | + // update the status and remove the listener after the loading job is completed |
| 168 | + launch { |
| 169 | + loadingJob.join() |
| 170 | + setActionsStatus(newLoadingEnabled = true) |
| 171 | + removeCancelListener(listener) |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + fun loadInitialParams() { |
| 176 | + setParams(loadStoredParams()) |
| 177 | + } |
| 178 | + |
| 179 | + fun saveParams() { |
| 180 | + val params = getParams() |
| 181 | + if (params.username.isEmpty() && params.password.isEmpty()) { |
| 182 | + removeStoredParams() |
| 183 | + } |
| 184 | + else { |
| 185 | + saveParams(params) |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + fun getSelectedVariant(): Variant |
| 190 | + |
| 191 | + fun updateContributors(users: List<User>) |
| 192 | + |
| 193 | + fun setLoadingStatus(text: String, iconRunning: Boolean) |
| 194 | + |
| 195 | + fun setActionsStatus(newLoadingEnabled: Boolean, cancellationEnabled: Boolean = false) |
| 196 | + |
| 197 | + fun addCancelListener(listener: ActionListener) |
| 198 | + |
| 199 | + fun removeCancelListener(listener: ActionListener) |
| 200 | + |
| 201 | + fun addLoadListener(listener: () -> Unit) |
| 202 | + |
| 203 | + fun addOnWindowClosingListener(listener: () -> Unit) |
| 204 | + |
| 205 | + fun setParams(params: Params) |
| 206 | + |
| 207 | + fun getParams(): Params |
| 208 | +} |
0 commit comments