Skip to content

Instantly share code, notes, and snippets.

View AlanGinger's full-sized avatar
🏠
Working from home

Alan AlanGinger

🏠
Working from home
View GitHub Profile
@AlanGinger
AlanGinger / once.kt
Last active March 1, 2023 16:31
the method executed only once
import kotlinx.coroutines.*
val myData by lazy(LazyThreadSafetyMode.NONE) {
loadDataFromNetwork()
}
suspend fun loadDataFromNetwork(): MyData {
// 模拟网络请求
delay(1000)
return MyData(/* ... */)
@AlanGinger
AlanGinger / CountDownCoroutines.kt
Created June 16, 2022 14:56
CountDown Coroutines
fun countDownCoroutines(
total: Int,
scope: CoroutineScope,
onTick: (Int) -> Unit,
onStart: (() -> Unit)? = null,
onFinish: (() -> Unit)? = null,
): Job {
return flow {
for (i in total downTo 0) {
emit(i)
@AlanGinger
AlanGinger / CallToObservable.java
Created March 3, 2020 07:23
[Retrofit Call to Observable]#Android
public static <T extends ApiResponse> Observable<T> CallToObservable(Call<T> originalCall) {
return Observable.<T>create(subscriber -> {
Call<T> call = originalCall.clone();
CallArbiter<T> arbiter = new CallArbiter<>(call, subscriber);
subscriber.add(arbiter);
subscriber.setProducer(arbiter);
Response<T> response;
try {
response = call.execute();
if (!response.isSuccessful()) {
@AlanGinger
AlanGinger / DisableableAppBarLayoutBehavior.java
Created March 3, 2020 03:27
[disable/re-enable the expansion CoordinatorLayout behavior]#Android
public class DisableableAppBarLayoutBehavior extends AppBarLayout.Behavior {
private boolean mEnabled;
public DisableableAppBarLayoutBehavior() {
super();
}
public DisableableAppBarLayoutBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@AlanGinger
AlanGinger / expand-collapse.java
Created February 14, 2020 09:04 — forked from ZkHaider/expand-collapse.java
Simple Expand / Collapse RecyclerView Item
public static class ExampleViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
private int originalHeight = 0;
private boolean isViewExpanded = false;
private YourCustomView yourCustomView
public ExampleViewHolder(View v) {
super(v);
v.setOnClickListener(this);
@AlanGinger
AlanGinger / Android @IntDef.java
Last active January 17, 2020 03:36
[@IntDef replace enum]#Android
@IntDef({Status.IDLE, Status.PROCESSING, Status.DONE, Status.CANCELLED})
@Retention(RetentionPolicy.SOURCE)
@interface Status {
int IDLE = 0;
int PROCESSING = 1;
int DONE = 2;
int CANCELLED = 3;
}
@AlanGinger
AlanGinger / EachDirectoryPath.md
Last active January 17, 2020 03:35 — forked from granoeste/EachDirectoryPath.md
[Get the each directory path] #Android

System directories

Method Result
Environment.getDataDirectory() /data
Environment.getDownloadCacheDirectory() /cache
Environment.getRootDirectory() /system

External storage directories

@AlanGinger
AlanGinger / RecyclerView.Java
Last active July 24, 2018 09:31
[RecyclerView onItemClickListener]#Android
public class ReactiveAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
String[] mDataset = { "Data", "In", "Adapter" };
private final PublishSubject<String> onClickSubject = PublishSubject.create();
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final String element = mDataset[position];
holder.itemView.setOnClickListener(new View.OnClickListener() {