Saket Poddar
05/22/2020, 8:26 AMahmad
05/22/2020, 9:54 AMShreyas Patil
05/22/2020, 6:38 PMJoshua
05/23/2020, 4:35 PMDeepti M
05/24/2020, 8:02 AMArchie
05/24/2020, 3:35 PMclass AViewModel() : ViewModel() {
val liveData = MutableLiveData<SomeData>()
...
fun doSomething() {
viewModelScope.launch {
//... do some long operation ....
liveData.value = resultOfLongOperation
}
}
}
and observe the live data in Fragment or Activity like:
...
val viewModel by viewModels<AViewModel> { ... }
...
fun onViewCreate(...) {
button.setOnclickListener {
viewModel.doSomething()
}
viewModel.liveData.observe(viewLifecycle, Observer { result ->
textView.text = result
})
}
...
I was wondering how to do UI test for the Code above. Since the doSomething()
takes a while to complete, my Espresso
test is failing. Can anyone share how they do their test? Thank you very much.Deepti M
05/25/2020, 12:59 AMDenys
05/25/2020, 7:55 AMKaushik
05/25/2020, 8:32 AMGeert
05/25/2020, 8:48 AMReddyTintaya
05/25/2020, 11:38 PMFile(photoPath).asRequestBody("image/jpg".toMediaTypeOrNull())
//REQUEST_CODE_PICK_IMAGE = 101
Intent(Intent.ACTION_PICK).also {
it.type = "*/*"
val mimeTypes = arrayOf("image/jpeg", "image/png", "application/pdf")
it.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
startActivityForResult(it, REQUEST_CODE_PICK_IMAGE)
}
and then i use this
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
when (requestCode) {
REQUEST_CODE_PICK_IMAGE -> {
selectedImageUri = data?.data
Log.i("develop", "data: ... $data")
image.setImageURI(selectedImageUri)
homeViewModel.selectedImageUri = selectedImageUri?.lastPathSegment.toString()
}
}
}
}
Orhan Tozan
05/26/2020, 9:41 AMMerseyside
05/26/2020, 1:16 PMRemon Shehata
05/26/2020, 3:38 PMdewildte
05/27/2020, 3:34 AMElyes
05/27/2020, 12:41 PMArchie
05/27/2020, 2:41 PMSlackbot
05/27/2020, 3:27 PMMauricio Hernandez
05/27/2020, 5:21 PMjava.lang.RuntimeException: Unable to start activity ComponentInfo{com.securitas.aviation.satt/com.auxo.satt.main.MainActivity}: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.auxo.satt.main.fragment.MainFragment: could not find Fragment constructor
Steve
05/27/2020, 6:43 PMwilliam
05/27/2020, 7:23 PM*::class.java.name
in my code, and I have proguard/r8 set up for that class to obfuscate it, the return value of that code would be the obfuscated version too?Brady Aiello
05/27/2020, 9:38 PMRemon Shehata
05/27/2020, 11:40 PMKaran Sharma
05/28/2020, 6:14 AMMohamed Ibrahim
05/28/2020, 11:32 AMT
05/28/2020, 12:41 PMprivate const val BASE_URL = "<https://newsapi.org/v2/>"
private const val API_KEY = "1234"
private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
private val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create())
.baseUrl(BASE_URL)
.build()
interface NewsApiService {
@GET("top-headlines?country=us&apiKey=${API_KEY}")
fun getProperties():
Call<String>
}
object NewsApi {
val retrofitService : NewsApiService by lazy {
retrofit.create(NewsApiService::class.java) }
}
class NewsViewModel : ViewModel() {
// The internal MutableLiveData String that stores the most recent response
private val _response = MutableLiveData<String>()
// The external immutable LiveData for the response String
val response: LiveData<String>
get() = _response
/**
* Call getNewsData() on init so we can display status immediately.
*/
init {
getNewsData()
}
/**
* Sets the value of the status LiveData to the News API status.
*/
private fun getNewsData() {
_response.value = NewsApi.retrofitService.getProperties().enqueue(
object: Callback<String> {
override fun onFailure(call: Call<String>, t: Throwable){
_response.value = "Failure: " + t.message
}
override fun onResponse(call: Call<String>, response: Response<String>) {
_response.value = response.body()
}
}).toString()
}
}
aipok
05/28/2020, 1:58 PMviewLifecycleOwner.lifecycleScope.launch
If I’m trying to update live data in my VM from .value
other fragments with same VM do not get it from observer. I have to use postValue
and in that case it delivered properly. Does anyone know why could it be? Previously I have been using uiScope
uiScope = CoroutineScope(Dispatchers.Main + job)
to launch coroutines inside my fragments and it was working fine with .value
but after I switched to viewLifecycleOwner.lifecycleScope
it is not anymore.Elka
05/28/2020, 3:00 PMDisplay.getMetrics
but the API is not consistent... On some devices the method includes the status bar height while on others it gives only the usable area of the screen...Klaus
05/28/2020, 8:37 PMDividerItemDecoration
is only shown between sections and not between rows?Slackbot
05/28/2020, 8:48 PMSlackbot
05/28/2020, 8:48 PMSaurabh
05/29/2020, 4:40 AM