Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Andrew Chen yongjhih

🏠
Working from home
View GitHub Profile
View LocalizationContextX.dart
extension LocalizationContextX<T extends BuildContext> on T {
Locale locale() {
return Localizations.localeOf(this);
}
Locale localeOrNull() {
return Localizations.localeOf(this, nullOk: true);
}
}
View SharedPreferences.getOrNull.dart
extension SharedPreferencesX<T extends SharedPreferences> on T {
bool getBoolOrNull(String key) => as<bool>(get(key));
int getIntOrNull(String key) => as<int>(get(key));
double getDoubleOrNull(String key) => as<double>(get(key));
String getStringOrNull(String key) => as<String>(get(key));
View ContourViewHolder.kt
class UserView(context: Context) : ContourLayout(context) {
init {
contourHeightOf { avatar.bottom() + 5.dip }
}
val name: TextView =
TextView(context).apply {
text = ""
applyLayout(
View TypedViewHolder.kt
abstract class TypedViewHolder<T, V : View>(parent: ViewGroup, val view: V)
: BindViewHolder<T>(
LinearLayout(parent.context).apply {
layoutParams = RecyclerView.LayoutParams(
RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT)
addView(view)
}
)
View GitHubUserDataSource.kt
class UserDataSource(
private val query: String,
private val githubService: GitHubService,
private val compositeDisposable: CompositeDisposable
) : PageKeyedDataSource<String, User>() {
val lifecycle by lazy { MutableLiveData<DataSourceLifecycle>() }
override fun loadInitial(
params: LoadInitialParams<String>,
View DataSourceLifecycleSingleTransformer.kt
class DataSourceLifecycleSingleTransformer<T>(
private val lifecycle: MutableLiveData<DataSourceLifecycle>
) : SingleTransformer<T, T> {
override fun apply(upstream: Single<T>): SingleSource<T> {
return upstream
.doOnSubscribe {
lifecycle.postValue(Loading)
}
.doOnSuccess {
lifecycle.postValue(Loaded)
View LoadingPagedListAdapter.kt
open class LoadingPagedListAdapter<T, VH : RecyclerView.ViewHolder>(
private val onOnCreateViewHolder: LoadingPagedListAdapter<T, VH>.(parent: ViewGroup, viewType: Int) -> VH,
private val onOnBindViewHolder: LoadingPagedListAdapter<T, VH>.(holder: VH, position: Int) -> Unit,
private val onGetItemViewType: LoadingPagedListAdapter<T, VH>.(Int) -> Int = { 0 },
private val lifecycle: LiveData<DataSourceLifecycle>,
differ: DiffUtil.ItemCallback<T>,
private val onCreateLoadingViewHolder: (LoadingPagedListAdapter<T, VH>.(parent: ViewGroup, viewType: Int) -> RecyclerView.ViewHolder)? = null
) : PagedListAdapter<T, RecyclerView.ViewHolder>(differ) {
companion object {
const val LOADING_TYPE = 19841119
View DateTimePicker.dart
// ref. https://medium.com/@gauthamns/date-time-input-fields-in-flutter-ae74ffbf4ef2
class DateTimePicker extends StatefulWidget {
const DateTimePicker({
Key key,
this.labelText,
@required
this.selectedDate,
@required
this.selectedTime,
this.onSelectDate,
View ResultOnMain.kt
fun Result.onMain(): ResultOnMain {
return if (this is ResultOnMain) {
this
} else {
ResultOnMain(this)
}
}
class ResultOnMain internal constructor(private val result: Result) : Result {
private val handler: Handler by lazy {
View merge-sorted-array.kt
class Solution {
fun merge(nums1: IntArray, m: Int, nums2: IntArray, n: Int): Unit {
var i = m - 1
var j = n - 1
var k = (m + n) - 1
val nums = nums1;
while (k >= 0) {
if (j >= 0 && (i < 0 || nums1[i] < nums2[j])) {
nums[k] = nums2[j]
j -= 1
You can’t perform that action at this time.