This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function isHexColorValid(hex: string): boolean { | |
return /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/.test(hex); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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) | |
} | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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) { |