kotlin

How to set '-Xuse-experimental=kotlin.experimental' in IntelliJ

时光怂恿深爱的人放手 提交于 2020-12-04 15:58:07
问题 while trying to build a Kotlin/Ktor application in IntelliJ, multiple warnings of the form Warning:(276, 6) Kotlin: This class can only be used with the compiler argument '-Xuse-experimental=kotlin.Experimental' are output. The warnings refer to @UseExperimental(KtorExperimentalLocationsAPI::class) so I expected to satisfy the warning by setting Settings -> Build -> Compiler -> Kotlin Compiler -> Additional command line parameters to -version -Xuse-experimental=kotlin.Experimental . (

How to show custom notification by default with collapsed layout when expand layout is enable in Android

a 夏天 提交于 2020-12-04 03:35:34
问题 I want to show my custom notification by support both small and large layout as screenshots above when user collapse or expand the notification. But the result it shows expanded notification by default. I want to show it as collapsed notification by default and only show expanded notification when user expand it. Please check my code bellow: private fun initCustomNotification() { // Get the layouts to use in the custom notification val notificationLayout = RemoteViews(packageName, R.layout

Java Stream 源码分析

天涯浪子 提交于 2020-12-03 16:47:33
前言 Java 8 的 Stream 使得代码更加简洁易懂,本篇文章深入分析 Java Stream 的工作原理,并探讨 Steam 的性能问题。 Java 8 集合中的 Stream 相当于高级版的 Iterator,它可以通过 Lambda 表达式对集合进行各种非常便利、高效的聚合操作(Aggregate Operation),或者大批量数据操作 (Bulk Data Operation)。 Stream的聚合操作与数据库SQL的聚合操作sorted、filter、map等类似。我们在应用层就可以高效地实现类似数据库SQL的聚合操作了,而在数据操作方面,Stream不仅可以通过串行的方式实现数据操作,还可以通过并行的方式处理大批量数据,提高数据的处理效率。 操作分类 官方将 Stream 中的操作分为两大类: 中间操作(Intermediate operations) ,只对操作进行了记录,即只会返回一个流,不会进行计算操作。 终结操作(Terminal operations) ,实现了计算操作。 中间操作又可以分为: 无状态(Stateless)操作 ,元素的处理不受之前元素的影响。 有状态(Stateful)操作 ,指该操作只有拿到所有元素之后才能继续下去。 终结操作又可以分为: 短路(Short-circuiting) 操作,指遇到某些符合条件的元素就可以得到最终结果

How to pass a function as parameter in kotlin - Android

强颜欢笑 提交于 2020-12-03 09:46:48
问题 How to pass a function in android using Kotlin . I can able to pass if i know the function like : fun a(b :() -> Unit){ } fun b(){ } I want to pass any function like -> fun passAnyFunc(fun : (?) ->Unit){} 回答1: Method as parameter Example: fun main(args: Array<String>) { // Here passing 2 value of first parameter but second parameter // We are not passing any value here , just body is here calculation("value of two number is : ", { a, b -> a * b} ); } // In the implementation we will received

Hilt using in android library

元气小坏坏 提交于 2020-12-03 09:12:59
问题 I would like to try Hilt DI in the android library. It is a dependency on another project, with its own submodule. The very first problem I've encountered is the requirement of marking Application with @HiltAndroidApp . Now I do not have anything that extends Application in my library ofc but would like to utilize Hilt and its predefined components. Is it possible or should I go with Dagger only in such a case? I've found a solution for Dagger, where library dependency injection is made

Hilt using in android library

为君一笑 提交于 2020-12-03 08:51:33
问题 I would like to try Hilt DI in the android library. It is a dependency on another project, with its own submodule. The very first problem I've encountered is the requirement of marking Application with @HiltAndroidApp . Now I do not have anything that extends Application in my library ofc but would like to utilize Hilt and its predefined components. Is it possible or should I go with Dagger only in such a case? I've found a solution for Dagger, where library dependency injection is made

抽丝剥茧Kotlin

流过昼夜 提交于 2020-12-03 08:33:00
前言 文章接上篇,这一篇我们好好聊一聊协程的原理,通过上一篇的学习,相信大家对于如何使用协程已经非常熟悉了。 故事还得从上次的协程分享开始,由于大家对协程的实践并不多,所以大家对下面的这段代码如何执行争论不休: GlobalScope.launch { val a = async { 1+2 } val b = async { 1+3 } val c = a + b Log.e(TAG, "result: $c " ) } 有人说,a 和 b 会串行执行,有人说,a 和 b 会并行执行,那么执行的结果到底是什么样的?我们将在下面的文章给出。 悲伤的故事 本个系列文章分为三篇,本文是第二篇: “ 《即学即用Kotlin - 协程》 《抽丝剥茧Kotlin - 协程基础篇》 《抽丝剥茧Kotlin - 协程Flow篇》 一、结构简要介绍 首先,我们得明确协程中有哪些东西,如果你会使用协程,那你肯定知道协程中有 CoroutineScope 、 CoroutineContext 和 CoroutineDispatcher ,这些都是使用过程中我们可以接触到的 API。 我简单的整理了协程中主要的基础类: 协程的类图 协程的类结构可分为三部分: CoroutineScope 、 CoroutineContext 和 Continuation 。 1. Continuation

Kotlin Vocabulary | 揭秘协程中的 suspend 修饰符

痞子三分冷 提交于 2020-12-02 22:09:44
Kotlin 协程把 suspend 修饰符 引入到了我们 Android 开发者的日常开发中。您是否好奇它的底层工作原理呢?编译器是如何转换我们的代码,使其能够挂起和恢复协程操作的呢? 了解这些将会帮您更好地理解挂起函数 (suspend function) 为什么只会在所有工作完成后才会返回,以及如何在不阻塞线程的情况下挂起代码。 本文概要: Kotlin 编译器将会为每个挂起函数创建一个状态机,这个状态机将为我们管理协程的操作! 📚 如果您是 Android 平台上协程的初学者,请查阅下面这些协程 codelab: 在 Android 应用中使用协程 协程的进阶使用: Kotlin Flow 和 Live Data 协程 101 协程简化了 Android 平台的异步操作。正如官方文档 《利用 Kotlin 协程提升应用性能》 所介绍的,我们可以使用协程管理那些以往可能阻塞主线程或者让应用卡死的异步任务。 协程也可以帮我们用命令式代码替换那些基于回调的 API。例如,下面这段使用了回调的异步代码: // 简化的只考虑了基础功能的代码 fun loginUser(userId: String, password: String, userResult: Callback<User>) { // 异步回调 userRemoteDataSource.logUserIn { user

How do I use the native JUnit 5 support in Gradle with the Kotlin DSL?

拈花ヽ惹草 提交于 2020-12-02 04:49:53
问题 I want to use the built-in JUnit 5 with the Gradle Kotlin DSL, because during build I get this warning: WARNING: The junit-platform-gradle-plugin is deprecated and will be discontinued in JUnit Platform 1.3. Please use Gradle's native support for running tests on the JUnit Platform (requires Gradle 4.6 or higher): https://junit.org/junit5/docs/current/user-guide/#running-tests-build-gradle That links tells me to put test { useJUnitPlatform() } in my build.gradle , but what is the syntax for

How do I use the native JUnit 5 support in Gradle with the Kotlin DSL?

我怕爱的太早我们不能终老 提交于 2020-12-02 04:44:49
问题 I want to use the built-in JUnit 5 with the Gradle Kotlin DSL, because during build I get this warning: WARNING: The junit-platform-gradle-plugin is deprecated and will be discontinued in JUnit Platform 1.3. Please use Gradle's native support for running tests on the JUnit Platform (requires Gradle 4.6 or higher): https://junit.org/junit5/docs/current/user-guide/#running-tests-build-gradle That links tells me to put test { useJUnitPlatform() } in my build.gradle , but what is the syntax for