Skip to content

Commit

Permalink
Create Kotlin extensions to work with PendingTask and PendingTaskErro…
Browse files Browse the repository at this point in the history
…rs more easily.
  • Loading branch information
levibostian committed Mar 1, 2018
1 parent 0ccfbb6 commit b998e26
Show file tree
Hide file tree
Showing 30 changed files with 355 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ env:
jobs:
include:
- stage: danger
script: bundle exec danger --fail-on-errors=true
script: bundle exec danger
- stage: pr
script: ./bin/install_android_sdk.sh && ./gradlew androidDependencies && ./bin/pr-tasks.sh

Expand Down
4 changes: 4 additions & 0 deletions Dangerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ if github.branch_for_base == "master"
warn 'Did you remember to generate documentation via dokku? (Hint: `./gradlew dokka`)'
end
end

if git.modified_files.include? "build.gradle" or git.modified_files.include? "wendy/build.gradle"
warn "I see you edited a `build.gradle` file. Keep in mind that unless you are simply upgrading version numbers of libraries, that is ok. If you are adding dependencies, you may get your PR denied to keep the library slim."
end
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ dependencies {
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.2'
implementation "com.android.support:support-compat:27.1.0"
implementation 'com.android.support:appcompat-v7:27.1.0'
implementation 'com.android.support:recyclerview-v7:27.1.0'
testImplementation 'junit:junit:4.12'
implementation project(':wendy')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
Expand Down
11 changes: 10 additions & 1 deletion app/src/main/java/com/levibostian/wendyexample/FooPendingTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.levibostian.wendyexample

import android.os.Handler
import android.os.Looper
import com.levibostian.wendy.extension.recordError
import com.levibostian.wendy.service.PendingTask
import com.levibostian.wendy.types.PendingTaskResult
import java.util.*
Expand All @@ -12,6 +13,8 @@ class FooPendingTask(manuallyRun: Boolean,

companion object {
fun blank(): FooPendingTask { return FooPendingTask(false, null, "") }

const val RANDOMLY_GENERATED_ERROR_ERROR_ID = "randomlyGeneratedErrorErrorId"
}

override fun runTask(): PendingTaskResult {
Expand All @@ -21,7 +24,13 @@ class FooPendingTask(manuallyRun: Boolean,

val rand = Random()
val n = rand.nextInt(100) + 1 // random number between 1 and 100
val successful: Boolean = n <= 25 // fail 25% of the time.
val successful: Boolean = n <= 50 // fail 50% of the time.
if (!successful) {
if (rand.nextInt(100) <= 50) {
this.recordError("Random error....", RANDOMLY_GENERATED_ERROR_ERROR_ID)
return PendingTaskResult.FAILED_DO_NOT_RESCHEDULE
}
}

return if (successful) PendingTaskResult.SUCCESSFUL else PendingTaskResult.FAILED_RESCHEDULE
}
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/levibostian/wendyexample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.levibostian.wendyexample
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.support.v4.app.NotificationCompat
import android.support.v4.app.NotificationManagerCompat
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.CompoundButton
Expand Down Expand Up @@ -76,5 +78,21 @@ class MainActivity : AppCompatActivity(), TaskRunnerListener {
}
override fun allTasksComplete() {
}
override fun errorRecorded(task: PendingTask, errorMessage: String?, errorId: String?) {
Handler().postDelayed({
refreshListOfTasks()
}, 1000)

val notificationManager = NotificationManagerCompat.from(this)
val notification = NotificationCompat.Builder(this, NotificationChannelUtil.ERROR_OCCURRED_CHANNEL_ID)
.setSmallIcon(android.R.drawable.ic_notification_clear_all)
.setContentTitle("Error occurred!")
.setContentText(errorMessage)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
notificationManager.notify(0, notification)
}
override fun errorResolved(task: PendingTask) {
}

}
20 changes: 20 additions & 0 deletions app/src/main/java/com/levibostian/wendyexample/MainApplication.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
package com.levibostian.wendyexample

import android.annotation.SuppressLint
import android.app.Application
import com.curiosityio.wendyexample.BuildConfig
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import com.levibostian.wendy.service.PendingTasks
import android.os.Build
import android.support.v4.app.NotificationCompat
import android.support.v4.app.NotificationManagerCompat
import com.curiosityio.wendyexample.R
import com.levibostian.wendyexample.NotificationChannelUtil.Companion.ERROR_OCCURRED_CHANNEL_DESCRIPTION
import com.levibostian.wendyexample.NotificationChannelUtil.Companion.ERROR_OCCURRED_CHANNEL_ID
import com.levibostian.wendyexample.NotificationChannelUtil.Companion.ERROR_OCCURRED_CHANNEL_NAME

class MainApplication : Application() {

@SuppressLint("NewApi")
override fun onCreate() {
super.onCreate()

PendingTasks.init(this, WendyExamplePendingTasksFactory())
.debug(BuildConfig.DEBUG)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(ERROR_OCCURRED_CHANNEL_ID, ERROR_OCCURRED_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT)
channel.description = ERROR_OCCURRED_CHANNEL_DESCRIPTION

val notificationManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.levibostian.wendyexample

class NotificationChannelUtil {
companion object {
const val ERROR_OCCURRED_CHANNEL_ID = "ERROR_OCCURRED_CHANNEL_ID"
const val ERROR_OCCURRED_CHANNEL_NAME = "PendingTask errors"
const val ERROR_OCCURRED_CHANNEL_DESCRIPTION = "Recorded errors for PendingTasks"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.levibostian.wendyexample
import android.annotation.TargetApi
import android.content.Context
import android.os.Build.VERSION_CODES.LOLLIPOP
import android.support.v4.content.ContextCompat
import android.util.AttributeSet
import android.widget.TextView
import com.levibostian.wendy.listeners.PendingTaskStatusListener
Expand Down Expand Up @@ -37,10 +38,22 @@ class PendingStatusTextView : TextView, PendingTaskStatusListener {
}
override fun running(taskId: Long) {
text = "Running"
setTextColor(ContextCompat.getColor(mContext, android.R.color.holo_blue_dark))
}

override fun complete(taskId: Long, successful: Boolean, rescheduled: Boolean) {
text = if (successful) "Success!" else "Failed!"
setTextColor(ContextCompat.getColor(mContext, if (successful) android.R.color.holo_green_dark else android.R.color.holo_orange_dark))
}

override fun errorRecorded(taskId: Long, errorMessage: String?, errorId: String?) {
text = errorMessage
setTextColor(ContextCompat.getColor(mContext, android.R.color.holo_red_dark))
}

override fun errorResolved(taskId: Long) {
text = "Error gone!"
setTextColor(ContextCompat.getColor(mContext, android.R.color.holo_red_light))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class PendingTasksRecyclerViewAdapter(val data: List<PendingTask>) : RecyclerVie

override fun getItemCount(): Int = data.count()

override fun onBindViewHolder(holder: PendingTasksRecyclerViewAdapter.ViewHolder?, position: Int) {
override fun onBindViewHolder(holder: PendingTasksRecyclerViewAdapter.ViewHolder, position: Int) {
val adapterItem: PendingTask = data[position]

holder!!.idTextView.text = String.format("task id: %d", adapterItem.task_id)
holder.idTextView.text = String.format("task id: %d", adapterItem.task_id)
holder.dataIdTextView.text = String.format("data id: %s", adapterItem.data_id)
holder.groupIdTextView.text = String.format("group id: %s", adapterItem.group_id)
holder.manuallyRunTextView.text = String.format("manually run: %s", adapterItem.manually_run.toString())
Expand All @@ -55,8 +55,8 @@ class PendingTasksRecyclerViewAdapter(val data: List<PendingTask>) : RecyclerVie
}
}

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): PendingTasksRecyclerViewAdapter.ViewHolder {
return ViewHolder(LayoutInflater.from(parent!!.context).inflate(R.layout.adapter_pending_task_recyclerview, parent, false))
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PendingTasksRecyclerViewAdapter.ViewHolder {
return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.adapter_pending_task_recyclerview, parent, false))
}

}
2 changes: 1 addition & 1 deletion bin/install_android_sdk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ unzip -qq -n $HOME/android-sdk-dl/sdk-tools.zip -d $HOME/android-sdk
# Install or update Android SDK components (will not do anything if already up to date thanks to the cache mechanism)
echo y | $HOME/android-sdk/tools/bin/sdkmanager 'tools' > /dev/null
echo y | $HOME/android-sdk/tools/bin/sdkmanager 'platform-tools' > /dev/null
echo y | $HOME/android-sdk/tools/bin/sdkmanager 'build-tools;27.0.3' > /dev/null
echo y | $HOME/android-sdk/tools/bin/sdkmanager 'build-tools;27.1.0' > /dev/null
echo y | $HOME/android-sdk/tools/bin/sdkmanager 'platforms;android-27' > /dev/null
echo y | $HOME/android-sdk/tools/bin/sdkmanager 'extras;google;m2repository' > /dev/null
6 changes: 0 additions & 6 deletions docs/wendy/alltypes/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ <h3>All Types</h3>
<tbody>
<tr>
<td>
<a href="../com.levibostian.wendy.extension/-create-grocery-store-item-pending-task/index.html">com.levibostian.wendy.extension.CreateGroceryStoreItemPendingTask</a></td>
<td>
</td>
</tr>
<tr>
<td>
<a href="../com.levibostian.wendy.service/-pending-task/index.html">com.levibostian.wendy.service.PendingTask</a></td>
<td>
<p>Represents a single task to perform. Usually used to sync offline data stored on the device with online remote storage.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ <h3>Properties</h3>
<p><a href="pending_task.html">pending_task</a></p>
</td>
<td>
<code><span class="keyword">lateinit</span> <span class="keyword">var </span><span class="identifier">pending_task</span><span class="symbol">: </span><a href="../../com.levibostian.wendy.service/-pending-task/index.html"><span class="identifier">PendingTask</span></a></code></td>
<code><span class="keyword">lateinit</span> <span class="keyword">var </span><span class="identifier">pending_task</span><span class="symbol">: </span><a href="../../com.levibostian.wendy.service/-pending-task/index.html"><span class="identifier">PendingTask</span></a></code>
<p>The <a href="../../com.levibostian.wendy.service/-pending-task/index.html">PendingTask</a> this error is associated with.</p>
</td>
</tr>
<tr>
<td>
Expand All @@ -85,5 +87,19 @@ <h3>Functions</h3>
</tr>
</tbody>
</table>
<h3>Extension Functions</h3>
<table>
<tbody>
<tr>
<td>
<p><a href="../../com.levibostian.wendy.extension/resolve-error.html">resolveError</a></p>
</td>
<td>
<code><span class="keyword">fun </span><a href="./index.html"><span class="identifier">PendingTaskError</span></a><span class="symbol">.</span><span class="identifier">resolveError</span><span class="symbol">(</span><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>Extension to <a href="../../com.levibostian.wendy.service/-pending-tasks/resolve-error.html">PendingTasks.resolveError</a> easily from a <a href="./index.html">PendingTaskError</a> instance.</p>
</td>
</tr>
</tbody>
</table>
</BODY>
</HTML>
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
<h1>pending_task</h1>
<a name="com.levibostian.wendy.db.PendingTaskError$pending_task"></a>
<code><span class="keyword">lateinit</span> <span class="keyword">var </span><span class="identifier">pending_task</span><span class="symbol">: </span><a href="../../com.levibostian.wendy.service/-pending-task/index.html"><span class="identifier">PendingTask</span></a></code>
<p>The <a href="../../com.levibostian.wendy.service/-pending-task/index.html">PendingTask</a> this error is associated with.</p>
<h3>Property</h3>
<p><a name="pending_task"></a>
<code>pending_task</code> - The <a href="../../com.levibostian.wendy.service/-pending-task/index.html">PendingTask</a> this error is associated with.</p>
</BODY>
</HTML>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<HTML>
<HEAD>
<meta charset="UTF-8">
<title>addTaskStatusListenerForTask - wendy</title>
<link rel="stylesheet" href="../../style.css">
</HEAD>
<BODY>
<a href="../index.html">wendy</a>&nbsp;/&nbsp;<a href="index.html">com.levibostian.wendy.extension</a>&nbsp;/&nbsp;<a href="./add-task-status-listener-for-task.html">addTaskStatusListenerForTask</a><br/>
<br/>
<h1>addTaskStatusListenerForTask</h1>
<a name="com.levibostian.wendy.extension$addTaskStatusListenerForTask(com.levibostian.wendy.service.PendingTask, com.levibostian.wendy.listeners.PendingTaskStatusListener)"></a>
<code><span class="keyword">fun </span><a href="../com.levibostian.wendy.service/-pending-task/index.html"><span class="identifier">PendingTask</span></a><span class="symbol">.</span><span class="identifier">addTaskStatusListenerForTask</span><span class="symbol">(</span><span class="identifier" id="com.levibostian.wendy.extension$addTaskStatusListenerForTask(com.levibostian.wendy.service.PendingTask, 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/-unit/index.html"><span class="identifier">Unit</span></a></code>
<p>Extension to <a href="../com.levibostian.wendy/-wendy-config/add-task-status-listener-for-task.html">WendyConfig.addTaskStatusListenerForTask</a> easily from a <a href="../com.levibostian.wendy.service/-pending-task/index.html">PendingTask</a> instance.</p>
</BODY>
</HTML>
15 changes: 15 additions & 0 deletions docs/wendy/com.levibostian.wendy.extension/get-latest-error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<HTML>
<HEAD>
<meta charset="UTF-8">
<title>getLatestError - wendy</title>
<link rel="stylesheet" href="../../style.css">
</HEAD>
<BODY>
<a href="../index.html">wendy</a>&nbsp;/&nbsp;<a href="index.html">com.levibostian.wendy.extension</a>&nbsp;/&nbsp;<a href="./get-latest-error.html">getLatestError</a><br/>
<br/>
<h1>getLatestError</h1>
<a name="com.levibostian.wendy.extension$getLatestError(com.levibostian.wendy.service.PendingTask)"></a>
<code><span class="keyword">fun </span><a href="../com.levibostian.wendy.service/-pending-task/index.html"><span class="identifier">PendingTask</span></a><span class="symbol">.</span><span class="identifier">getLatestError</span><span class="symbol">(</span><span class="symbol">)</span><span class="symbol">: </span><a href="../com.levibostian.wendy.db/-pending-task-error/index.html"><span class="identifier">PendingTaskError</span></a><span class="symbol">?</span></code>
<p>Extension to <a href="../com.levibostian.wendy.service/-pending-tasks/get-latest-error.html">PendingTasks.getLatestError</a> easily from a <a href="../com.levibostian.wendy.service/-pending-task/index.html">PendingTask</a> instance.</p>
</BODY>
</HTML>
Loading

0 comments on commit b998e26

Please sign in to comment.