epoxy icon indicating copy to clipboard operation
epoxy copied to clipboard

A better way to do search controller?

Open alouanemed opened this issue 7 years ago • 12 comments

I am using epoxy controller to show the results of 2 types: Products and Product Lists. I am seeing myself repeating a lot of states changes using booleans. Is there an efficient way to do this? Thanks.

The Controller : https://gist.github.com/alouanemed/71c63c9d91e5676dd6bb1c563e56ee1b

alouanemed avatar Oct 12 '18 10:10 alouanemed

A couple suggestions

     EmptyViewModel_()
          .id("empty")
          .message(emptyMessage)
          .actionText(actionText)
          .spanSizeOverride { _, _, _ -> 2 }
          .onEmptyActionClicked(onEmptyActionClicked)
          .addTo(this)

use the generated kotlin extension functions instead to write this as

emptyViewModel {
          id("empty")
          message(emptyMessage)
          actionText(actionText)
          spanSizeOverride { _, _, _ -> 2 }
          onEmptyActionClicked(onEmptyActionClicked)
}

instead of using addIf do this

 if (showListProgress) progressViewSmallModel {
          id("ProgressViewSmallModel_0")
          spanSizeOverride { _, _, _ -> 2 }
}

instead of doing this

.message(context.getString(R.string.plutus__main_search_result_list_empty))

use @TextProp to generate a method that takes the string resource directly. this is more efficient so you don't have to look up the string while you're building models, it is deferred to when the view is bound

.message(R.string.plutus__main_search_result_list_empty)

instead of creating your id string by concatenation you can pass multiple arguments

.id("ProductItemModel_" + it.id)

do instead. this is more efficient to avoid creating a new string

.id("ProductItemModel_", it.id)

besides that the boolean logic looks as simple as it can get - i don't think you can get rid of inherent complexity

elihart avatar Oct 12 '18 19:10 elihart

Thank you Eli. Really appreciated.

alouanemed avatar Oct 12 '18 20:10 alouanemed

@elihart I seem to can't resolve this when using kotlin extensions. Is there any config should be done to enable this?

emptyViewModel {
          id("empty")
          message(emptyMessage)
          actionText(actionText)
          spanSizeOverride { _, _, _ -> 2 }
          onEmptyActionClicked(onEmptyActionClicked)
}

alouanemed avatar Oct 15 '18 12:10 alouanemed

Do you use kapt for your annotation processing? You'll have to add the epoxy-processor as kapt and not annotationProcessor in your build.gradle file for Epoxy to generate Kotlin extensions.

ShaishavGandhi avatar Oct 15 '18 12:10 ShaishavGandhi

Thanks!

alouanemed avatar Oct 17 '18 12:10 alouanemed

Sorry for reopning this,I just checked that I am indeed using kapt :

implementation "com.airbnb.android:epoxy:$versions.epoxyversion"
  kapt "com.airbnb.android:epoxy-processor:$versions.epoxyversion"

alouanemed avatar Oct 22 '18 12:10 alouanemed

have you applied the kapt plugin?

elihart avatar Oct 22 '18 14:10 elihart

Yes, I already have those on my gradle file.

alouanemed avatar Oct 22 '18 15:10 alouanemed

@alouanemed did you manage to get this working?

elihart avatar Oct 30 '18 15:10 elihart

Unfortunately, no.

alouanemed avatar Oct 30 '18 15:10 alouanemed

@alouanemed

This happened to me recently make sure you have following in your module gradle file

apply plugin: 'kotlin-kapt'

// to enable error type infering in stubs
kapt {
    correctErrorTypes = true
}

dependencies{
     implementation OtherDeps.EPOXY_CORE
     implementation OtherDeps.EPOXY_DATABINDING
     kapt OtherDeps.EPOXY_ANNOTATION_PROCESSOR
}

then clean and rebuild the project.

If the errors are still there, just highlight the word extension functions name in your case emptyViewModel start retryping the word emptyViewModel in that place you will get a suggestion called emptyViewModel{ ... } select it and press tab. This will generate the extension functions source inside modules build folder and import the extension function in your current file.

kaushalyap avatar Jan 06 '20 11:01 kaushalyap

Hi, I saw that @elihart suggest using @TextProp to do .message(R.string.plutus__main_search_result_list_empty). I'm using @EpoxyDataBindingPattern, how can I achieve the @TextProp in my case. Thank you so much

DawnNguyenAhiho avatar Mar 29 '24 03:03 DawnNguyenAhiho