smilecs
03/02/2018, 2:02 PModay
03/06/2018, 8:01 AMbjartek
03/18/2018, 9:26 PModay
03/20/2018, 11:10 AMjtravis
04/02/2018, 9:59 PMshouldThrow
method takes an exception, the other takes a callback with the exception as the first arg, so I don’t understand why it wouldn’t be able to figure that out…oday
05/08/2018, 11:10 AModay
05/08/2018, 11:12 AMHamza
06/11/2018, 8:21 AMAyden
06/14/2018, 2:42 PMSwipeRefreshLayout
?
Am I doing something redundant?
/*
Fetching data from Firebase and
passing the value to Adapter
*/
val addValueEventListener = firebaseReference.addValueEventListener(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onDataChange(p0: DataSnapshot) {
if (p0!!.exists()) {
val vibeList = mutableListOf<Vibes>()
// V stands for Vibe
for (v in p0.children) {
val vibe = v.getValue(Vibes::class.java)
vibeList.add(vibe!!)
}
recyclerView.adapter = VibeAdapter(vibeList)
}
}
})
/*
Fetch data from Firebase and
passing the value to Adapter whenever
swipe refresh triggered
*/
swipeRefreshLayout.setOnRefreshListener {
val addValueEventListener = firebaseReference.addValueEventListener(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onDataChange(p0: DataSnapshot) {
if (p0!!.exists()) {
val vibeList = mutableListOf<Vibes>()
// V stands for Vibe
for (v in p0.children) {
val vibe = v.getValue(Vibes::class.java)
vibeList.add(vibe!!)
}
recyclerView.adapter = VibeAdapter(vibeList)
}
}
})
}
snowe
06/16/2018, 4:42 PMvar resultValidator = testExecutor.whenever(wheneverCommand!!)
resultValidator = expects.events?.let { resultValidator.expectEvents(*it.toTypedArray()) } ?: resultValidator
resultValidator = expects.eventsMatching?.let { resultValidator.expectEventsMatching(it) } ?: resultValidator
resultValidator = expects.returnValue?.let { resultValidator.expectReturnValue(it) } ?: resultValidator
resultValidator = expects.returnValueMatching?.let { resultValidator.expectReturnValueMatching(it) } ?: resultValidator
Hamza
06/21/2018, 2:47 PM"INVALID REQUEST" -> html("The PayPal API failed us. INVALID_REQUEST")
"AUTHENTICATION_FAILURE" -> html("I'm sure we authenticated. Right, guys? AUTHENTICATION_FAILURE")
"NOT_AUTHORIZED" -> html("What do you mean we are not authorized? >:( NOT_AUTHORIZED")
"RESOURCE_NOT_FOUND" -> html("Guys, we need to update our API. RESOURCE_NOT_FOUND")
"METHOD_NOT_SUPPORTED" -> html("This wasn't suppose to happen... METHOD_NOT_SUPPORTED")
"MEDIA_TYPE_NOT_ACCEPTABLE" -> html("This wasn't suppose to happen... MEDIA_TYPE_NOT_ACCEPTABLE")
"UNPROCESSABLE_ENTITY" -> html("Guys, we broke PayPal! UNPROCESSABLE_ENTITY")
"RATE_LIMIT_REACHED" -> html("We've reached max capacity. Please try again later. RATE_LIMIT_REACHED")
"INTERNAL_SERVICE_ERROR" -> html("Look. PayPal. You need to fix your system. INTERNAL_SERVICE_ERROR")
"SERVICE_UNAVAILABLE" -> html("PayPal is not available at the moment. Please try again later. SERVICE_UNAVAILABLE")
is there a better way?ianbrandt
06/25/2018, 3:25 AMfor
loops in Kotlin? Perhaps a functional approach?
fun primeFactorsOf(integer: Int): List<Int> {
val primeFactors = mutableListOf<Int>()
var n = integer
var divisor = 2
while (n > 1) {
while (n % divisor == 0) {
primeFactors.add(divisor)
n /= divisor
}
divisor++
}
return primeFactors
}
For comparison, a Java implementation with C-style loops:
public static List<Integer> primeFactorsOf(int integer) {
List<Integer> primeFactors = new ArrayList<>();
for (int divisor = 2; integer > 1; divisor++)
for (; integer % divisor == 0; integer /= divisor)
primeFactors.add(divisor);
return primeFactors;
}
Daniel
06/25/2018, 7:51 PMdelayer
) then I would use (this is a question):
argumentCaptor<() -> Unit>().apply {
whenever(delayer.delay(capture())).thenAnswer {
firstValue()
}
}
?
Now suppose I only want to invoke the lambda a defined number of times because the code calles delay(...)
recursively until the user stops it.
How would I do this?
I came up with:
argumentCaptor<() -> Unit>().apply {
var invocationCount = 0
whenever(delayer.delay(capture())).thenAnswer {
if(invocationCount < times) {
++invocationCount
firstValue()
}
}
}
But that does not seem like a clean solutionAyden
06/26/2018, 2:06 PMval httpLoggingInterceptor = HttpLoggingInterceptor().level = HttpLoggingInterceptor.Level.BODY
Hamza
06/29/2018, 4:22 PMHamza
06/29/2018, 4:31 PMprintln("hello".apply {
replace("h", "H")
})
}
Shawn
07/03/2018, 5:48 AMbuttonRegister.isEnabled = ! (isEmpty(email) && isEmpty(password) && isEmpty(confirmPassword))
but !
is a source of contention among a lot of developers since it’s easy to miss or forget to useAyden
07/03/2018, 7:04 AMkristofdho
07/12/2018, 3:04 PMfillAdIds
is also only used once so why define a function for itAndreas Sinz
07/12/2018, 4:19 PMtempAdIds
, currentAdId
, ...) which can lead to nasty race conditions/other kind of bugs later onrobin
07/24/2018, 1:53 PMtianhao
07/25/2018, 6:01 AMaccessing non-final property in constructor
not sure how is the fix for that? Thanks
class KafkaService {
val producer: KafkaProducer<String, String>
init {
val props = Properties()
props[ProducerConfig.BOOTSTRAP_SERVERS_CONFIG] = "127.0.0.1:9092"
props[ProducerConfig.CLIENT_ID_CONFIG] = "DemoProducer"
props[ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG] = StringSerializer::class.java.name
props[ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG] = StringSerializer::class.java.name
producer = KafkaProducer(props)
}
fun sendToKafka(topic: String, message: String) {
val producerRecord: ProducerRecord<String?, String> = ProducerRecord(topic, null, message)
producer.send(producerRecord)
}
}
Ayden
07/27/2018, 9:21 AMx80486
08/07/2018, 2:50 AMclass Response(val message: String, val path: String) {
val timestamp: Long = Instant.now().toEpochMilli()
var status: Int = 0
lateinit var error: String
companion object {
fun clientError(message: String, path: String): Response {
return Response(message, path).apply { status = 400; error = "Bad Request" }
}
fun serverError(message: String, path: String): Response {
return Response(message, path).apply { status = 500; error = "Internal Server Error" }
}
}
}
twisterrob
08/09/2018, 11:42 PM?.
vs if
Which one is better in the following example and why? (open to improvements)louiscad
08/11/2018, 8:46 AM/** Matches [android.support.v7.app.AppCompatViewInflater.createView] content. */
val appCompatViewFactory = { clazz: Class<out View>, context: Context, _: Style<out View>? ->
when (clazz) {
TextView::class.java -> AppCompatTextView(context)
Button::class.java -> AppCompatButton(context)
ImageView::class.java -> AppCompatImageView(context)
EditText::class.java -> AppCompatEditText(context)
...
else -> null
}
}
The issue is that there's no type check, I could return anything for anytype and the compiler won't fail. I want to make sure each when branch returns an object of the type of the case on which it matched.kotlin_questions
08/11/2018, 4:02 PMgriffio
08/12/2018, 7:11 PMoperator fun Double.times(vec: Vec) = when (vec) {
is Vec2 -> vec.copy(this * vec.x, this * vec.y)
is Vec3 -> vec.copy(this * vec.x, this * vec.y, this * vec.z)
}
Andreas Sinz
08/12/2018, 7:47 PMas T
-cast is needed, because the return type of your when
is Vec
and not T
Ayden
08/13/2018, 7:07 AMif (TextUtils.isEmpty(name)) {
textInputName.isErrorEnabled = true
textInputName.error = "Name cannot be empty."
}
if (TextUtils.isEmpty(email)) {
textInputEmail.isErrorEnabled = true
textInputEmail.error = "Email cannot be empty."
}
if (TextUtils.isEmpty(password)) {
textInputPassword.isErrorEnabled = true
textInputPassword.error = "Password cannot be empty."
}
if (TextUtils.isEmpty(password)) {
textInputConfirmPassword.isErrorEnabled = true
textInputConfirmPassword.error = "Confirm password cannot be empty."
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
textInputEmail.isErrorEnabled = true
textInputEmail.error = "Email format does not valid."
}
if (!(password.length >= 8)) {
textInputPassword.isErrorEnabled = true
textInputPassword.error = "Password length must be greater than 8 character."
}
if (!(confirmPassword.length >= 8)) {
textInputConfirmPassword.isErrorEnabled = true
textInputConfirmPassword.error = "Confirm password length must be greater than 8 character."
}
if (!(password.equals(confirmPassword))) {
textInputConfirmPassword.isErrorEnabled = true
textInputConfirmPassword.error = "Confirm Password does not match with Password"
}
}