kotlin

When to use coroutineScope vs supervisorScope?

最后都变了- 提交于 2020-12-29 05:31:06
问题 Can someone explain what exactly is the difference between these two? When do you use one over the other? Thanks in advance. 回答1: The best way to explain the difference is to explain the mechanism of coroutineScope . Consider this code: suspend fun main() = println(compute()) suspend fun compute(): String = coroutineScope { val color = async { delay(60_000); "purple" } val height = async<Double> { delay(100); throw HttpException() } "A %s box %.1f inches tall".format(color.await(), height

How to build Google protocol buffers and Kotlin using Gradle?

南笙酒味 提交于 2020-12-29 05:29:19
问题 I'm trying to build a project that uses both Google protocol buffers and Kotlin using Gradle. I want the proto files to compile into Java source, which is then called from my Kotlin code. My source files are arranged like this: src/main/proto/*.proto src/main/kotlin/*.kt src/test/kotlin/*.kt Here's my build.gradle file: version '1.0-SNAPSHOT' apply plugin: 'kotlin' apply plugin: 'java' apply plugin: 'com.google.protobuf' repositories { mavenCentral() maven { url "http://dl.bintray.com/kotlin

How to correctly mock ViewModel on androidTest

别等时光非礼了梦想. 提交于 2020-12-29 05:24:05
问题 I'm currently writing some UI unit tests for a fragment, and one of these @Test is to see if a list of objects is correctly displayed, this is not an integration test, therefore I wish to mock the ViewModel . The fragment's vars: class FavoritesFragment : Fragment() { private lateinit var adapter: FavoritesAdapter private lateinit var viewModel: FavoritesViewModel @Inject lateinit var viewModelFactory: FavoritesViewModelFactory (...) Here's the code: @MediumTest @RunWith(AndroidJUnit4::class)

How to correctly mock ViewModel on androidTest

空扰寡人 提交于 2020-12-29 05:23:19
问题 I'm currently writing some UI unit tests for a fragment, and one of these @Test is to see if a list of objects is correctly displayed, this is not an integration test, therefore I wish to mock the ViewModel . The fragment's vars: class FavoritesFragment : Fragment() { private lateinit var adapter: FavoritesAdapter private lateinit var viewModel: FavoritesViewModel @Inject lateinit var viewModelFactory: FavoritesViewModelFactory (...) Here's the code: @MediumTest @RunWith(AndroidJUnit4::class)

2020结束前撸一个android无限循环的banner(轮播)

拈花ヽ惹草 提交于 2020-12-28 09:42:36
最近在复习自定义view的知识,结合做了一段时间的ios体会,总结出: banner的开源库已经有很多,但在实现UI或产品的需求时总是找不到合适的,不是功能实现不了,而是能改造成产品需要的交互效果有时真是很蛋疼。所以就本着全是自己来实现的原则,参考一些资料来撸一个自定义banner,整下来也就300多行代码。 原理大概是这样: 1、自定义一个viewgroup,因为它可以用来添加其它view。 这里我只会用到3个itemview,左、中、右,根据滑动后对数据重新设置可以实现多个数据的循环切换。 这里图片的数据写死了,哎,需要动态设置的话可以自行处理一下,添加一个public方法,从itemViews可以拿到你需要设置的view。 还有,图片的显示这里直接显示在imageview上了,如果有大图的话建议还是用一些库来显示,例如glide。 public class BannerView extends ViewGroup { private Context context; private List<View> itemViews;//缓存左中右3个itemview private List<Integer> data;//资源数据,这里只用一张图片id private int itemWidth;//itemview的宽 private int itemHeight;/

Kotlin - Idiomatic way to remove duplicate strings from array?

丶灬走出姿态 提交于 2020-12-27 08:27:47
问题 How to remove duplicates from an Array<String?> in kotlin? 回答1: Use the distinct extension function: val a = arrayOf("a", "a", "b", "c", "c") val b = a.distinct() // ["a", "b", "c"] There's also distinctBy function that allows one to specify how to distinguish the items: val a = listOf("a", "b", "ab", "ba", "abc") val b = a.distinctBy { it.length } // ["a", "ab", "abc"] As @mfulton26 suggested, you can also use toSet, toMutableSet and, if you don't need the original ordering to be preserved,

Crash with Android/Kotlin QR Scanner App and the latest version of Google ML Kit Scan Barcode

半腔热情 提交于 2020-12-27 06:48:26
问题 I am trying to make an app which reads QR images and get the data from the image. I am using the latest version of the google machine learning kit for scan barcodes and following the documentation about this https://developers.google.com/ml-kit/vision/barcode-scanning/android. However, at the moment that I run the app, I am getting the following crash: Image error with QR scanner App I have no idea what's going on with this due I a new with this kind of functionality. This is the code that I

Crash with Android/Kotlin QR Scanner App and the latest version of Google ML Kit Scan Barcode

∥☆過路亽.° 提交于 2020-12-27 06:46:19
问题 I am trying to make an app which reads QR images and get the data from the image. I am using the latest version of the google machine learning kit for scan barcodes and following the documentation about this https://developers.google.com/ml-kit/vision/barcode-scanning/android. However, at the moment that I run the app, I am getting the following crash: Image error with QR scanner App I have no idea what's going on with this due I a new with this kind of functionality. This is the code that I

Is there a way to filter data with Firebase UI RecyclerView?

大兔子大兔子 提交于 2020-12-27 06:33:47
问题 I'm using the Firebase UI FirestorePagingAdapter, i have a colletion of posts and i want to filter the post that are not from the current user and order them by timestamp I tried doing this but i get inequality error: override fun getPostFromLocation(viewLifecycleOwner:LifecycleOwner, location:String) :FirestorePagingOptions<Post>{ val query = firestore.collection(POST_COLLECTION) .orderBy("timestamp", Query.Direction.DESCENDING) .whereNotEqualTo("uid",auth.currentUser?.uid) return

Is there a way to filter data with Firebase UI RecyclerView?

余生长醉 提交于 2020-12-27 06:33:07
问题 I'm using the Firebase UI FirestorePagingAdapter, i have a colletion of posts and i want to filter the post that are not from the current user and order them by timestamp I tried doing this but i get inequality error: override fun getPostFromLocation(viewLifecycleOwner:LifecycleOwner, location:String) :FirestorePagingOptions<Post>{ val query = firestore.collection(POST_COLLECTION) .orderBy("timestamp", Query.Direction.DESCENDING) .whereNotEqualTo("uid",auth.currentUser?.uid) return