Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added listener remove functions #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/wendy/com.levibostian.wendy/-wendy-config/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,26 @@ <h3>Companion Object Functions</h3>
<p>Listen to updates about a specific task and how it is going. Get updated when this specific task gets run by the task runner, then if it fails/succeeds/gets skipped.</p>
</td>
</tr>
<tr>
<td>
<p><a href="remove-task-runner-listener.html">removeTaskRunnerListener</a></p>
</td>
<td>
<code><span class="keyword">fun </span><span class="identifier">removeTaskRunnerListener</span><span class="symbol">(</span><span class="identifier" id="com.levibostian.wendy.WendyConfig.Companion$removeTaskRunnerListener(com.levibostian.wendy.listeners.TaskRunnerListener)/listener">listener</span><span class="symbol">:</span>&nbsp;<a href="../../com.levibostian.wendy.listeners/-task-runner-listener/index.html"><span class="identifier">TaskRunnerListener</span></a><span class="symbol">)</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html"><span class="identifier">Boolean</span></a></code>
<p>Removes a listener instance added one or more times with <a href="add-task-runner-listener.html">addTaskRunnerListener</a>
Note: if you added the same instance more than one times, this function will remove all of them</p>
</td>
</tr>
<tr>
<td>
<p><a href="remove-task-status-listener.html">removeTaskStatusListener</a></p>
</td>
<td>
<code><span class="keyword">fun </span><span class="identifier">removeTaskStatusListener</span><span class="symbol">(</span><span class="identifier" id="com.levibostian.wendy.WendyConfig.Companion$removeTaskStatusListener(com.levibostian.wendy.listeners.PendingTaskStatusListener)/listener">listener</span><span class="symbol">:</span>&nbsp;<a href="../../com.levibostian.wendy.listeners/-pending-task-status-listener/index.html"><span class="identifier">PendingTaskStatusListener</span></a><span class="symbol">)</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html"><span class="identifier">Boolean</span></a></code>
<p>Removes a listener instance added one or more times with <a href="add-task-status-listener-for-task.html">addTaskStatusListenerForTask</a>
Note: if you added the same instance more than one times, this function will remove all of them</p>
</td>
</tr>
</tbody>
</table>
</BODY>
Expand Down
29 changes: 29 additions & 0 deletions wendy/src/main/java/com/levibostian/wendy/WendyConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ class WendyConfig {
* @see TaskRunnerListener to learn more about what callbacks to expect.
*/
fun addTaskRunnerListener(listener: TaskRunnerListener) = taskRunnerListeners.add(WeakReference(listener))

/**
* Removes a listener instance added one or more times with [addTaskRunnerListener]
* Note: if you added the same instance more than one time, this function will remove all of them
*
* @param listener the listener instance to be removed
*
* @return true if the listener was removed at least once, false if it was collected by the GC
* or you didn't added it before
*/
fun removeTaskRunnerListener(listener: TaskRunnerListener): Boolean {
return taskRunnerListeners
.removeAll { it.get()?.equals(listener) ?: false }
}

internal fun getTaskRunnerListeners(): List<TaskRunnerListener> {
if (Looper.getMainLooper().thread != Thread.currentThread()) throw RuntimeException("You must be on UI thread.")
return taskRunnerListeners
Expand Down Expand Up @@ -90,6 +105,20 @@ class WendyConfig {
listener.errorRecorded(taskId, latestError.errorMessage, latestError.errorId)
}
}

/**
* Removes a listener instance added one or more times with [addTaskStatusListenerForTask]
* Note: if you added the same instance more than one time, this function will remove all of them
*
* @param listener the listener instance to be removed
*
* @return true if the listener was removed at least once, false if it was collected by the GC
* or you didn't added it before
*/
fun removeTaskStatusListener(listener: PendingTaskStatusListener): Boolean {
return taskStatusListeners
.removeAll { it.listener.get()?.equals(listener) ?: false }
}
}

internal class TaskStatusListener(val taskId: Long, val listener: WeakReference<PendingTaskStatusListener>)
Expand Down