Permalink
Browse files

Use livedata-ktx's observe Kotlin extension

  • Loading branch information...
ianhanniballake committed Dec 18, 2018
1 parent 353028d commit 0d3b6b7628b5d51285aa291b55ae6ba87ff221a1
Showing with 73 additions and 79 deletions.
  1. +1 −1 android-client-common/build.gradle
  2. +1 −1 example-watchface/build.gradle
  3. +1 −1 extensions/build.gradle
  4. +0 −6 extensions/src/main/java/com/google/android/apps/muzei/util/LiveDataExt.kt
  5. +1 −1 main/build.gradle
  6. +1 −1 main/src/main/java/com/google/android/apps/muzei/ChooseProviderFragment.kt
  7. +1 −1 main/src/main/java/com/google/android/apps/muzei/MuzeiActivity.kt
  8. +1 −1 main/src/main/java/com/google/android/apps/muzei/MuzeiWallpaperService.kt
  9. +1 −1 main/src/main/java/com/google/android/apps/muzei/browse/BrowseProviderFragment.kt
  10. +1 −1 main/src/main/java/com/google/android/apps/muzei/quicksettings/NextArtworkTileService.kt
  11. +1 −1 main/src/main/java/com/google/android/apps/muzei/shortcuts/ArtworkInfoShortcutController.kt
  12. +1 −1 main/src/main/java/com/google/android/apps/muzei/sources/SourceManager.kt
  13. +49 −51 main/src/main/java/com/google/android/apps/muzei/sources/SourceSettingsActivity.kt
  14. +1 −1 main/src/main/java/com/google/android/apps/muzei/sources/SourceSetupActivity.kt
  15. +1 −1 main/src/main/java/com/google/android/apps/muzei/widget/WidgetUpdater.kt
  16. +1 −1 source-gallery/build.gradle
  17. +2 −2 ...-gallery/src/main/java/com/google/android/apps/muzei/gallery/GalleryImportPhotosDialogFragment.kt
  18. +1 −0 source-gallery/src/main/java/com/google/android/apps/muzei/gallery/GallerySettingsActivity.kt
  19. +2 −2 source-gallery/src/main/java/com/google/android/apps/muzei/gallery/GallerySetupActivity.kt
  20. +1 −1 source-single/build.gradle
  21. +1 −0 wearable/build.gradle
  22. +2 −2 wearable/src/main/java/com/google/android/apps/muzei/MuzeiActivity.kt
  23. +1 −1 wearable/src/main/java/com/google/android/apps/muzei/MuzeiWatchFace.kt
@@ -26,7 +26,7 @@ dependencies {
implementation project(':extensions')
implementation "androidx.core:core-ktx:$coreVersion"
implementation "androidx.exifinterface:exifinterface:$exifInterfaceVersion"
api "androidx.lifecycle:lifecycle-livedata:$lifecycleVersion"
api "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycleVersion"
implementation "android.arch.work:work-runtime-ktx:$workManagerVersion"
api "androidx.room:room-coroutines:$roomVersion"
@@ -20,7 +20,7 @@ apply plugin: 'kotlin-android'
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
compileOnly "com.google.android.wearable:wearable:$wearableVersion"
implementation "com.google.android.support:wearable:$wearableVersion"
//implementation "com.google.android.apps.muzei:muzei-api:+"
@@ -22,7 +22,7 @@ dependencies {
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion"
api "androidx.lifecycle:lifecycle-livedata-core:$lifecycleVersion"
api "androidx.lifecycle:lifecycle-livedata-core-ktx:$lifecycleVersion"
}

android {
@@ -20,12 +20,6 @@ import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer

fun <T> LiveData<T>.observe(owner: LifecycleOwner, callback: (T?) -> Unit) {
observe(owner, Observer<T?> { value ->
callback(value)
})
}

fun <T> LiveData<T?>.observeNonNull(owner: LifecycleOwner, callback: (T) -> Unit) {
observe(owner, Observer { value ->
if (value != null) {
@@ -112,7 +112,7 @@ dependencies {
implementation "androidx.browser:browser:$browserVersion"
implementation "androidx.exifinterface:exifinterface:$exifInterfaceVersion"
implementation "androidx.constraintlayout:constraintlayout:$constraintLayoutVersion"
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion"
@@ -44,6 +44,7 @@ import androidx.core.view.isVisible
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.observe
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
@@ -58,7 +59,6 @@ import com.google.android.apps.muzei.api.provider.ProviderContract
import com.google.android.apps.muzei.notifications.NotificationSettingsDialogFragment
import com.google.android.apps.muzei.room.MuzeiDatabase
import com.google.android.apps.muzei.sync.ProviderManager
import com.google.android.apps.muzei.util.observe
import com.google.android.apps.muzei.util.toast
import com.google.firebase.analytics.FirebaseAnalytics
import kotlinx.coroutines.GlobalScope
@@ -31,8 +31,8 @@ import androidx.fragment.app.commit
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.observe
import com.google.android.apps.muzei.notifications.NotificationSettingsDialogFragment
import com.google.android.apps.muzei.util.observe
import com.google.android.apps.muzei.wallpaper.WallpaperActiveState
import com.google.firebase.analytics.FirebaseAnalytics
import net.nurik.roman.muzei.BuildConfig
@@ -39,6 +39,7 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.observe
import com.google.android.apps.muzei.featuredart.BuildConfig.FEATURED_ART_AUTHORITY
import com.google.android.apps.muzei.notifications.NotificationUpdater
import com.google.android.apps.muzei.render.ImageLoader
@@ -54,7 +55,6 @@ import com.google.android.apps.muzei.shortcuts.ArtworkInfoShortcutController
import com.google.android.apps.muzei.sources.SourceManager
import com.google.android.apps.muzei.sync.ProviderManager
import com.google.android.apps.muzei.util.coroutineScope
import com.google.android.apps.muzei.util.observe
import com.google.android.apps.muzei.util.observeNonNull
import com.google.android.apps.muzei.wallpaper.LockscreenObserver
import com.google.android.apps.muzei.wallpaper.WallpaperAnalytics
@@ -30,6 +30,7 @@ import androidx.core.os.bundleOf
import androidx.core.view.ViewCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.observe
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
@@ -38,7 +39,6 @@ import com.bumptech.glide.Glide
import com.google.android.apps.muzei.room.Artwork
import com.google.android.apps.muzei.room.MuzeiDatabase
import com.google.android.apps.muzei.util.coroutineScope
import com.google.android.apps.muzei.util.observe
import com.google.android.apps.muzei.util.toast
import com.google.firebase.analytics.FirebaseAnalytics
import kotlinx.coroutines.CoroutineScope
@@ -31,12 +31,12 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import androidx.lifecycle.LiveData
import androidx.lifecycle.observe
import com.google.android.apps.muzei.MuzeiWallpaperService
import com.google.android.apps.muzei.room.MuzeiDatabase
import com.google.android.apps.muzei.room.Provider
import com.google.android.apps.muzei.sources.SourceManager
import com.google.android.apps.muzei.sources.allowsNextArtwork
import com.google.android.apps.muzei.util.observe
import com.google.android.apps.muzei.util.toast
import com.google.android.apps.muzei.wallpaper.WallpaperActiveState
import com.google.firebase.analytics.FirebaseAnalytics
@@ -24,10 +24,10 @@ import android.os.Build
import androidx.annotation.RequiresApi
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.observe
import com.google.android.apps.muzei.ArtworkInfoRedirectActivity
import com.google.android.apps.muzei.room.Artwork
import com.google.android.apps.muzei.room.MuzeiDatabase
import com.google.android.apps.muzei.util.observe
import net.nurik.roman.muzei.R

/**
@@ -34,6 +34,7 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.observe
import com.google.android.apps.muzei.api.MuzeiArtSource
import com.google.android.apps.muzei.api.internal.ProtocolConstants.ACTION_SUBSCRIBE
import com.google.android.apps.muzei.api.internal.ProtocolConstants.EXTRA_SUBSCRIBER_COMPONENT
@@ -46,7 +47,6 @@ import com.google.android.apps.muzei.room.sendAction
import com.google.android.apps.muzei.sync.ProviderManager
import com.google.android.apps.muzei.util.coroutineScope
import com.google.android.apps.muzei.util.goAsync
import com.google.android.apps.muzei.util.observe
import com.google.android.apps.muzei.util.observeNonNull
import com.google.android.apps.muzei.util.toastFromBackground
import com.google.firebase.analytics.FirebaseAnalytics
@@ -49,10 +49,10 @@ import androidx.core.text.bold
import androidx.core.text.buildSpannedString
import androidx.core.view.isVisible
import androidx.lifecycle.LiveData
import androidx.lifecycle.observe
import com.google.android.apps.muzei.api.MuzeiArtSource
import com.google.android.apps.muzei.room.MuzeiDatabase
import com.google.android.apps.muzei.room.Source
import com.google.android.apps.muzei.util.observe
import com.google.firebase.analytics.FirebaseAnalytics
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
@@ -118,61 +118,59 @@ class SourceSettingsActivity : AppCompatActivity() {
}
.create()
sourcesLiveData.observe(this) { sources ->
if (sources != null) {
if (sources.any { it.selected }) {
setResult(RESULT_OK)
if (sources.any { it.selected }) {
setResult(RESULT_OK)
}
val pm = packageManager
val sourcesViews = sources.asSequence().filterNot { source ->
source.label.isNullOrEmpty()
}.mapNotNull { source ->
try {
source to pm.getServiceInfo(source.componentName, 0)
} catch (e: PackageManager.NameNotFoundException) {
null
}
val pm = packageManager
val sourcesViews = sources.asSequence().filterNot { source ->
source.label.isNullOrEmpty()
}.mapNotNull { source ->
try {
source to pm.getServiceInfo(source.componentName, 0)
} catch (e: PackageManager.NameNotFoundException) {
null
}
}.map { (source, info) ->
SourceView(source).apply {
icon = BitmapDrawable(resources, generateSourceImage(
info.loadIcon(pm))).apply {
setColorFilter(source.color, PorterDuff.Mode.SRC_ATOP)
}
}.map { (source, info) ->
SourceView(source).apply {
icon = BitmapDrawable(resources, generateSourceImage(
info.loadIcon(pm))).apply {
setColorFilter(source.color, PorterDuff.Mode.SRC_ATOP)
}
}.sortedWith(Comparator { sourceView1, sourceView2 ->
val s1 = sourceView1.source
val s2 = sourceView2.source
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val target1IsO = s1.targetSdkVersion >= Build.VERSION_CODES.O
val target2IsO = s2.targetSdkVersion >= Build.VERSION_CODES.O
if (target1IsO && !target2IsO) {
return@Comparator 1
} else if (!target1IsO && target2IsO) {
return@Comparator -1
}
}
}.sortedWith(Comparator { sourceView1, sourceView2 ->
val s1 = sourceView1.source
val s2 = sourceView2.source
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val target1IsO = s1.targetSdkVersion >= Build.VERSION_CODES.O
val target2IsO = s2.targetSdkVersion >= Build.VERSION_CODES.O
if (target1IsO && !target2IsO) {
return@Comparator 1
} else if (!target1IsO && target2IsO) {
return@Comparator -1
}
val pn1 = s1.componentName.packageName
val pn2 = s2.componentName.packageName
if (pn1 != pn2) {
if (packageName == pn1) {
return@Comparator -1
} else if (packageName == pn2) {
return@Comparator 1
}
}
val pn1 = s1.componentName.packageName
val pn2 = s2.componentName.packageName
if (pn1 != pn2) {
if (packageName == pn1) {
return@Comparator -1
} else if (packageName == pn2) {
return@Comparator 1
}
// These labels should be non-null with the isNullOrEmpty() check above
val label1 = s1.label
?: throw IllegalStateException("Found null label for ${s1.componentName}")
val label2 = s2.label
?: throw IllegalStateException("Found null label for ${s2.componentName}")
label1.compareTo(label2)
}).toList()
adapter.clear()
adapter.addAll(sourcesViews)
if (!dialog.isShowing) {
FirebaseAnalytics.getInstance(this).logEvent(FirebaseAnalytics.Event.VIEW_ITEM_LIST,
bundleOf(FirebaseAnalytics.Param.ITEM_CATEGORY to "sources"))
dialog.show()
}
// These labels should be non-null with the isNullOrEmpty() check above
val label1 = s1.label
?: throw IllegalStateException("Found null label for ${s1.componentName}")
val label2 = s2.label
?: throw IllegalStateException("Found null label for ${s2.componentName}")
label1.compareTo(label2)
}).toList()
adapter.clear()
adapter.addAll(sourcesViews)
if (!dialog.isShowing) {
FirebaseAnalytics.getInstance(this).logEvent(FirebaseAnalytics.Event.VIEW_ITEM_LIST,
bundleOf(FirebaseAnalytics.Param.ITEM_CATEGORY to "sources"))
dialog.show()
}
}
}
@@ -25,9 +25,9 @@ import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.lifecycle.observe
import com.google.android.apps.muzei.api.MuzeiArtSource
import com.google.android.apps.muzei.room.MuzeiDatabase
import com.google.android.apps.muzei.util.observe
import net.nurik.roman.muzei.R

class SourceSetupActivity : AppCompatActivity() {
@@ -19,8 +19,8 @@ package com.google.android.apps.muzei.widget
import android.content.Context
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.observe
import com.google.android.apps.muzei.room.MuzeiDatabase
import com.google.android.apps.muzei.util.observe
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

@@ -33,7 +33,7 @@ dependencies {
implementation "androidx.recyclerview:recyclerview:$recyclerViewVersion"
implementation "androidx.constraintlayout:constraintlayout:$constraintLayoutVersion"
implementation "androidx.exifinterface:exifinterface:$exifInterfaceVersion"
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion"
implementation "androidx.room:room-coroutines:$roomVersion"
implementation "android.arch.work:work-runtime-ktx:$workManagerVersion"
@@ -12,7 +12,7 @@ import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.viewModels
import androidx.lifecycle.LiveData
import com.google.android.apps.muzei.util.observe
import androidx.lifecycle.observe

class GalleryImportPhotosDialogFragment : DialogFragment() {

@@ -54,7 +54,7 @@ class GalleryImportPhotosDialogFragment : DialogFragment() {
listener = context as? OnRequestContentListener ?: throw IllegalArgumentException(
"${context.javaClass.simpleName} must implement OnRequestContentListener")
getContentActivitiesLiveData.observe(this) { getContentActivities ->
if (getContentActivities?.isEmpty() != false) {
if (getContentActivities.isEmpty()) {
dismiss()
} else {
updateAdapter(getContentActivities)
@@ -43,6 +43,7 @@ import androidx.core.content.edit
import androidx.core.view.ViewCompat
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.observe
import androidx.paging.PagedList
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DefaultItemAnimator
@@ -23,8 +23,8 @@ import android.os.Bundle
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.observe
import com.google.android.apps.muzei.api.provider.MuzeiArtProvider
import com.google.android.apps.muzei.util.observe

class GallerySetupActivity : FragmentActivity() {

@@ -37,7 +37,7 @@ class GallerySetupActivity : FragmentActivity() {
super.onCreate(savedInstanceState)
GalleryDatabase.getInstance(this).chosenPhotoDao()
.chosenPhotos.observe(this) { chosenUris ->
val numChosenUris = chosenUris?.size ?: 0
val numChosenUris = chosenUris.size
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED || numChosenUris > 0) {
// If we have permission or have any previously selected images
@@ -25,7 +25,7 @@ dependencies {
implementation "androidx.core:core-ktx:$coreVersion"
api project(':muzei-api')
implementation project(':extensions')
api "androidx.lifecycle:lifecycle-livedata:$lifecycleVersion"
api "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
}

android {
@@ -88,6 +88,7 @@ dependencies {
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
implementation "android.arch.work:work-runtime-ktx:$workManagerVersion"
implementation "androidx.core:core-ktx:$coreVersion"
implementation "androidx.lifecycle:lifecycle-livedata-core-ktx:$lifecycleVersion"
implementation "androidx.fragment:fragment-ktx:$fragmentVersion"
implementation "androidx.exifinterface:exifinterface:$exifInterfaceVersion"
implementation "androidx.wear:wear:$wearVersion"
@@ -20,6 +20,7 @@ import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.observe
import androidx.wear.ambient.AmbientModeSupport
import androidx.wear.widget.RoundedDrawable
import com.google.android.apps.muzei.datalayer.ActivateMuzeiIntentService
@@ -30,7 +31,6 @@ import com.google.android.apps.muzei.room.MuzeiDatabase
import com.google.android.apps.muzei.room.sendAction
import com.google.android.apps.muzei.sync.ProviderManager
import com.google.android.apps.muzei.util.coroutineScope
import com.google.android.apps.muzei.util.observe
import com.google.android.apps.muzei.util.observeNonNull
import com.google.firebase.analytics.FirebaseAnalytics
import kotlinx.coroutines.Dispatchers
@@ -40,7 +40,7 @@ import net.nurik.roman.muzei.BuildConfig
import net.nurik.roman.muzei.BuildConfig.DATA_LAYER_AUTHORITY
import net.nurik.roman.muzei.R
import java.text.SimpleDateFormat
import java.util.*
import java.util.Locale

class MuzeiViewModel(application: Application) : AndroidViewModel(application) {

Oops, something went wrong.

0 comments on commit 0d3b6b7

Please sign in to comment.