Skip to content

Commit

Permalink
implemented passing explicit lifecycle owner to request observer. Rel…
Browse files Browse the repository at this point in the history
…eased 4.1.0
  • Loading branch information
gotev committed Feb 9, 2020
1 parent d08f106 commit c2e8004
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/SimpleMultipartUpload/app/build.gradle
Expand Up @@ -33,7 +33,7 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

def uploadServiceVersion = "4.0.0-rc1"
def uploadServiceVersion = "4.1.0"

implementation "net.gotev:uploadservice:$uploadServiceVersion"
}
Expand Up @@ -27,7 +27,7 @@ class App : Application() {
createNotificationChannel()

UploadServiceConfig.initialize(
namespace = packageName,
context = this,
defaultNotificationChannel = notificationChannelID,
debug = BuildConfig.DEBUG
)
Expand Down
6 changes: 3 additions & 3 deletions examples/SimpleMultipartUpload/build.gradle
Expand Up @@ -5,10 +5,10 @@ buildscript {
repositories {
google()
jcenter()

}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
classpath 'com.android.tools.build:gradle:3.5.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -19,7 +19,7 @@ allprojects {
repositories {
google()
jcenter()

}
}

Expand Down
3 changes: 2 additions & 1 deletion examples/app/demoapp/build.gradle
Expand Up @@ -52,7 +52,8 @@ dependencies {

// Support
implementation "androidx.appcompat:appcompat:$androidx_appcompat_version"
implementation 'com.google.android.material:material:1.0.0'
implementation "com.google.android.material:material:$androidx_appcompat_version"
implementation "androidx.lifecycle:lifecycle-extensions:$androidx_lifecycle_version"

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

Expand Down
Expand Up @@ -8,6 +8,8 @@
import android.os.StrictMode;
import android.util.Log;

import androidx.lifecycle.ProcessLifecycleOwner;

import com.facebook.stetho.Stetho;
import com.facebook.stetho.okhttp3.StethoInterceptor;

Expand Down Expand Up @@ -56,9 +58,7 @@ public void onCreate() {
// setup backoff multiplier
UploadServiceConfig.setRetryPolicy(new RetryPolicyConfig(1, 10, 2, 3));

createNotificationChannel();

new RequestObserver(this, new GlobalBroadcastReceiver()).register();
new RequestObserver(this, ProcessLifecycleOwner.get(), new GlobalBroadcastReceiver()).register();
}

private void createNotificationChannel() {
Expand Down
Expand Up @@ -71,7 +71,7 @@ public void onFile(UploadItem item) {

});

request.subscribe(this, new RequestObserverDelegate() {
request.subscribe(this, this, new RequestObserverDelegate() {
@Override
public void onProgress(@NotNull Context context, @NotNull UploadInfo uploadInfo) {
Log.e("LIFECYCLE", "Progress " + uploadInfo.getProgressPercent());
Expand Down
3 changes: 2 additions & 1 deletion manifest.gradle
Expand Up @@ -12,7 +12,7 @@ ext {
library_licenses = ["Apache-2.0"]
library_licenses_url = 'https://www.apache.org/licenses/LICENSE-2.0.txt'
library_project_group = 'net.gotev'
library_version = '4.0.0'
library_version = '4.1.0'
version_code = 40
min_sdk = 21
target_sdk = 29
Expand All @@ -36,6 +36,7 @@ ext {
androidx_test_espresso_version = '3.2.0'

// Library and app dependencies versions
androidx_lifecycle_version = '2.2.0'
androidx_appcompat_version = '1.1.0'
androidx_local_broadcast_version = '1.0.0'
}
2 changes: 2 additions & 0 deletions uploadservice/build.gradle
Expand Up @@ -4,6 +4,7 @@ apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.jfrog.bintray'
apply plugin: 'com.github.ben-manes.versions'
apply plugin: "org.jlleitschuh.gradle.ktlint"
apply plugin: 'kotlin-kapt'

Properties properties = new Properties()
if (project.rootProject.file("local.properties").exists()) {
Expand Down Expand Up @@ -79,6 +80,7 @@ dependencies {

// Support
implementation "androidx.appcompat:appcompat:$androidx_appcompat_version"
kapt "androidx.lifecycle:lifecycle-compiler:$androidx_lifecycle_version"

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "androidx.localbroadcastmanager:localbroadcastmanager:$androidx_local_broadcast_version"
Expand Down
Expand Up @@ -2,6 +2,7 @@ package net.gotev.uploadservice

import android.content.Context
import android.os.Parcelable
import androidx.lifecycle.LifecycleOwner
import net.gotev.uploadservice.data.UploadFile
import net.gotev.uploadservice.data.UploadNotificationConfig
import net.gotev.uploadservice.data.UploadTaskParameters
Expand Down Expand Up @@ -74,10 +75,11 @@ constructor(protected val context: Context, protected var serverUrl: String) {
/**
* Subscribe to events of this upload request by creating a new request observer.
* @param context context
* @param lifecycleOwner lifecycle to use when subscribing for events
* @param delegate Observer delegate implementation
*/
fun subscribe(context: Context, delegate: RequestObserverDelegate): RequestObserver {
return RequestObserver(context, delegate).apply { subscribe(this@UploadRequest) }
fun subscribe(context: Context, lifecycleOwner: LifecycleOwner, delegate: RequestObserverDelegate): RequestObserver {
return RequestObserver(context, lifecycleOwner, delegate).apply { subscribe(this@UploadRequest) }
}

protected abstract fun getAdditionalParameters(): Parcelable
Expand Down
Expand Up @@ -17,13 +17,14 @@ import net.gotev.uploadservice.data.UploadStatus

class RequestObserver(
private val context: Context,
lifecycleOwner: LifecycleOwner,
private val delegate: RequestObserverDelegate
) : BroadcastReceiver(), LifecycleObserver {

private var subscribedUploadID: String? = null

init {
(context as? LifecycleOwner)?.lifecycle?.addObserver(this)
lifecycleOwner.lifecycle.addObserver(this)
}

override fun onReceive(context: Context, intent: Intent?) {
Expand Down

0 comments on commit c2e8004

Please sign in to comment.