raipankaj / ArchitectureComponentSample

A sample project demonstrating how to use android architecture components library
0 stars 0 forks source link

kotlin Any type only contains 3 methods: equals, toString and hashcode. #1

Closed neuberfran closed 2 years ago

neuberfran commented 4 years ago

Hi,

Please, help-me with issue in slideshare project: kotlin Any type only contains 3 methods: equals, toString and hashcode.

https://pt.slideshare.net/PankajRai3/android-architecture-components-with-cloud-firestore  

image

raipankaj commented 4 years ago

Hi @neuberfran it could be the case that the version of the arch component may be old, so try out in this way Transformations.switchMap(_registerAttendee) rather than trying directly on mutablelivedata object or update to ktx version of livedata and viewmodel to leverage the ktx benefits.

def lifecycle_version = "2.2.0" implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version" implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"

neuberfran commented 4 years ago

Hi @raipankaj Tks. I solved with Transformations.map, But not solved with KTX solution: image

I'm using map and not switchMap because I need to transform a list that comes from the firestore (image below) and use (for now) only the alarmstate field in my XML (DATABINDING): android: text = '@ {viewmodel.hotStockLiveData.alarmstate? "On off"}'

https://github.com/neuberfran/FirestoreEverything02/blob/master/app/src/main/java/neuberfran/firestoreioteverything/viewmodel/EstadoViewModel.kt

If you can help more, I’m happy.

I'm trying to change my ### repository (ProducRepository.kt and/or Repositor.kt files) To consume it in the viewmodel (binding) and meet the requirements of the XML databinding that wants 1 boolean and not a <List >

Firestore support, is very busy and hasn't answered me yet

issue121

raipankaj commented 4 years ago

The warning which you got with the ktx version is because of jvm-8, u should use java 8 version android { compileSdkVersion 29 buildToolsVersion "29.0.2" .... compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } }

In case you read the collection like "products" it will give you a list of documents whereas when you read the document directly by providing its id while doing a query from the client-side it will directly fetch the document snapshot instead of list of documents snapshot. If possible provide an id to your document inside products collection, if that is not possible then you have to fetch a list of documents and thereafter extract the document which you want. It all depends on your use case.

neuberfran commented 4 years ago

@raipankaj How can I do this without making any changes to my repository (ProducRepository.kt file) and solving the problem below:

https://github.com/neuberfran/FirestoreEverything02/blob/master/app/src/main/java/neuberfran/firestoreioteverything/viewmodel/EstadoViewModel.kt I will make changes only in the ViewModel(EstadoViewModel.kt). Which are?

image

neuberfran commented 4 years ago

@raipankaj ORIGINAL ViewModel class to be changed:

class ProductViewModel : ViewModel() { private var product: MutableLiveData? = null private var products: MutableLiveData<List>? = null

val allProducts: MutableLiveData<List<Product>>
    get() {
        if (products == null) {
            products = ProductRepository.getInstance().products
        }
        return products as MutableLiveData<List<Product>>
    }

fun getProductById(productId: String): MutableLiveData<Product> {
    if (product == null) {
        product = ProductRepository.getInstance().getProductById(productId)
    }
    return product as MutableLiveData<Product>
}

fun clearProducts() {
    products = null
}

}

raipankaj commented 4 years ago

@neuberfran get the individual record from the document using the following method Firestore.collection("products").document("xa.....").get().addOnCompleteListener()

In the case of current viewmodel then you should return the livedata from function

private var product: MutableLiveData? = null fun getProductById(productId: String): LiveData { if (product == null) { product = ProductRepository.getInstance().getProductById(productId) } return product }

or switch to

private var _productLiveData = MutableLiveData() val productLiveData = Transformation.switchMap(_productLiveData) { ProductRepository.getInstance().getProductById(productId) }

fun getProductById(productId: String) { _productLiveData.value = productId }

whenever you will call getProductById method the _productLiveData value will change that will call the Transformation.switchMap