Skip to content

Instantly share code, notes, and snippets.

@seanghay
seanghay / result-of.kt
Created August 27, 2020 04:05
ResultOf is a utility result class for handling data and exception.
/**
* ResultOf is a utility result class for handling data and exception.
* It's useful for LiveData and fragment/activity communication.
*/
sealed class ResultOf<out T> {
data class Success<out T>(val value: T) : ResultOf<T>()
data class Failure(
val message: String?,
val throwable: Throwable?
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Currency;
import java.util.Locale;
export function isHexColorValid(hex: string): boolean {
return /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/.test(hex);
}
class PostsListAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(
parent: ViewGroup,
@LayoutRes viewType: Int
): RecyclerView.ViewHolder {
val view = LayoutInflator.from(parent.context).inflate(viewType, parent, false)
return when(viewType) {
R.layout.item_post -> PostViewHolder(view)
R.layout.item_header -> HeaderViewHolder(view)
/* Copyright 2020 Seanghay Yath (@seanghay)
SPDX-License-Identifier: Apache-2.0 */
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
viewModel.posts.observe(viewLifecycleOwner, Observer {result ->
val (items) = result.withDefault { postsFromDb() }
adapter.submitList(items)
})
/* Copyright 2020 Seanghay Yath (@seanghay)
SPDX-License-Identifier: Apache-2.0 */
inline fun <T> ResultOf<T>.withDefault(value: () -> T): ResultOf.Success<T> {
return when (this) {
is ResultOf.Success -> this
is ResultOf.Failure -> ResultOf.Success(value())
}
}
/* Copyright 2020 Seanghay Yath (@seanghay)
SPDX-License-Identifier: Apache-2.0 */
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
viewModel.posts.observe(viewLifecycleOwner, Observer {result ->
result.map { it.posts.first() }.doIfSuccess { post ->
addToSomewhere(post)
}
})
}
/* Copyright 2020 Seanghay Yath (@seanghay)
SPDX-License-Identifier: Apache-2.0 */
inline fun <reified T, reified R> ResultOf<T>.map(transform: (T) -> R): ResultOf<R> {
return when (this) {
is ResultOf.Success -> ResultOf.Success(transform(value))
is ResultOf.Failure -> this
}
}
/* Copyright 2020 Seanghay Yath (@seanghay)
SPDX-License-Identifier: Apache-2.0 */
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
viewModel.posts.observe(viewLifecycleOwner, Observer {result ->
result.doIfSuccess {items ->
adapter.submitList(items)
}
/* Copyright 2020 Seanghay Yath (@seanghay)
SPDX-License-Identifier: Apache-2.0 */
inline fun <reified T> ResultOf<T>.doIfFailure(callback: (error: String?, throwable: Throwable?) -> Unit) {
if (this is ResultOf.Failure) {
callback(message, throwable)
}
}
inline fun <reified T> ResultOf<T>.doIfSuccess(callback: (value: T) -> Unit) {