优化方法名,去掉无用代码

This commit is contained in:
tongtong committed 2025-10-31 10:09:05 +08:00
1 parent 5cc7caf34f
commit 59b6c9db76
7 files changed
+41 -123

No files matched your search

@@ -10,9 +10,9 @@ import androidx.lifecycle.MutableLiveData;
import com.trello.rxlifecycle4.RxLifecycle;
import com.trello.rxlifecycle4.android.ActivityEvent;
import com.ttstd.dialer.base.mvvm.BaseViewModel;
import com.ttstd.dialer.databinding.ActivityAppListBinding;
import com.ttstd.dialer.db.app.AppRepository;
import com.ttstd.dialer.db.app.DesktopSortApp;
import com.ttstd.dialer.databinding.ActivityAppListBinding;
import com.ttstd.dialer.utils.ApkUtils;
import java.text.Collator;
@@ -43,35 +43,35 @@ public class AppListViewModel extends BaseViewModel<ActivityAppListBinding, Acti
public MutableLiveData<List<DesktopSortApp>> mDesktopSortAppData = new MutableLiveData<>();
public void getDbAppList(){
public void getDbAppList() {
Observable.fromCallable(new Callable<List<DesktopSortApp>>() {
@Override
public List<DesktopSortApp> call() throws Exception {
return mAppRepository.getAllContacts();
return mAppRepository.getAllApp();
}
}) .compose(RxLifecycle.bindUntilEvent(getLifecycle(), ActivityEvent.DESTROY))
}).compose(RxLifecycle.bindUntilEvent(getLifecycle(), ActivityEvent.DESTROY))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<DesktopSortApp>>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
Log.e("getDbAppList", "onSubscribe: " );
Log.e("getDbAppList", "onSubscribe: ");
}
@Override
public void onNext(@NonNull List<DesktopSortApp> desktopSortApps) {
Log.e("getDbAppList", "onNext: "+desktopSortApps );
Log.e("getDbAppList", "onNext: " + desktopSortApps);
mDesktopSortAppData.setValue(desktopSortApps);
}
@Override
public void onError(@NonNull Throwable e) {
Log.e("getDbAppList", "onError: " +e.getMessage());
Log.e("getDbAppList", "onError: " + e.getMessage());
}
@Override
public void onComplete() {
Log.e("getDbAppList", "onComplete: " );
Log.e("getDbAppList", "onComplete: ");
}
});
@@ -102,7 +102,7 @@ public class MainActivity extends BaseMvvmActivity<MainViewModel, ActivityMainBi
@Override
protected void initData() {
mViewModel.updateAppList();
}
@Override
@@ -1,37 +1,15 @@
package com.ttstd.dialer.activity.main;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ResolveInfo;
import android.util.Log;
import com.trello.rxlifecycle4.RxLifecycle;
import com.trello.rxlifecycle4.android.ActivityEvent;
import com.ttstd.dialer.base.mvvm.BaseViewModel;
import com.ttstd.dialer.databinding.ActivityMainBinding;
import com.ttstd.dialer.db.app.AppRepository;
import com.ttstd.dialer.db.app.DesktopSortApp;
import com.ttstd.dialer.utils.ApkUtils;
import java.text.Collator;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.schedulers.Schedulers;
public class MainViewModel extends BaseViewModel<ActivityMainBinding, ActivityEvent> {
private static final String TAG = "MainViewModel";
private AppRepository mAppRepository;
@Override
@@ -40,73 +18,4 @@ public class MainViewModel extends BaseViewModel<ActivityMainBinding, ActivityEv
mAppRepository = new AppRepository(context);
}
public void updateAppList() {
Observable.fromCallable(new Callable<List<ResolveInfo>>() {
@Override
public List<ResolveInfo> call() throws Exception {
return ApkUtils.getAllLauncherResolveInfo(getSafeContext());
}
})
.compose(RxLifecycle.bindUntilEvent(getLifecycle(), ActivityEvent.DESTROY))
.subscribeOn(Schedulers.io())
.map(new Function<List<ResolveInfo>, List<ComponentName>>() {
@Override
public List<ComponentName> apply(List<ResolveInfo> resolveInfos) throws Throwable {
List<ComponentName> componentNames = resolveInfos.stream().map(new java.util.function.Function<ResolveInfo, ComponentName>() {
@Override
public ComponentName apply(ResolveInfo resolveInfo) {
String packageName = resolveInfo.activityInfo.packageName;
String className = resolveInfo.activityInfo.name;
ComponentName componentName = new ComponentName(packageName, className);
return componentName;
}
}).collect(Collectors.toList());
return componentNames;
}
})
.compose(RxLifecycle.bindUntilEvent(getLifecycle(), ActivityEvent.DESTROY))
.subscribeOn(Schedulers.io())
.map(new Function<List<ComponentName>, List<DesktopSortApp>>() {
@Override
public List<DesktopSortApp> apply(List<ComponentName> componentNames) throws Throwable {
List<DesktopSortApp> desktopSortApps = componentNames.stream().map(new java.util.function.Function<ComponentName, DesktopSortApp>() {
@Override
public DesktopSortApp apply(ComponentName componentName) {
DesktopSortApp desktopSortApp = new DesktopSortApp(getSafeContext(), componentName);
return desktopSortApp;
}
}).sorted(new Comparator<DesktopSortApp>() {
@Override
public int compare(DesktopSortApp o1, DesktopSortApp o2) {
return Collator.getInstance(Locale.CHINESE).compare(o1.getLabel(), o2.getLabel());
}
}).collect(Collectors.toList());
return desktopSortApps;
}
})
.compose(RxLifecycle.bindUntilEvent(getLifecycle(), ActivityEvent.DESTROY))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<DesktopSortApp>>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onNext(@NonNull List<DesktopSortApp> desktopSortApps) {
Log.e(TAG, "getLauncherAppList" + "onNext: " + desktopSortApps);
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
}
});
}
}