[app] Sort module list
This commit is contained in:
parent
81f6756e82
commit
1ba29cd32a
|
|
@ -20,6 +20,8 @@
|
|||
|
||||
package io.github.lsposed.manager.repo;
|
||||
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
|
@ -29,12 +31,15 @@ import java.nio.charset.StandardCharsets;
|
|||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import io.github.lsposed.manager.App;
|
||||
import io.github.lsposed.manager.repo.model.OnlineModule;
|
||||
import io.github.lsposed.manager.util.ModuleUtil;
|
||||
import okhttp3.Call;
|
||||
import okhttp3.Callback;
|
||||
import okhttp3.Request;
|
||||
|
|
@ -43,10 +48,15 @@ import okhttp3.ResponseBody;
|
|||
|
||||
public class RepoLoader {
|
||||
private static RepoLoader instance = null;
|
||||
private OnlineModule[] onlineModules = new OnlineModule[0];
|
||||
private Map<String, OnlineModule> onlineModules = new HashMap<>();
|
||||
private final Path repoFile = Paths.get(App.getInstance().getFilesDir().getAbsolutePath(), "repo.json");
|
||||
private final List<Listener> listeners = new CopyOnWriteArrayList<>();
|
||||
private boolean isLoading = false;
|
||||
private boolean repoLoaded = false;
|
||||
|
||||
public boolean isRepoLoaded() {
|
||||
return repoLoaded;
|
||||
}
|
||||
|
||||
public static synchronized RepoLoader getInstance() {
|
||||
if (instance == null) {
|
||||
|
|
@ -68,6 +78,7 @@ public class RepoLoader {
|
|||
.build()).enqueue(new Callback() {
|
||||
@Override
|
||||
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
||||
Toast.makeText(App.getInstance(), e.getMessage(), Toast.LENGTH_LONG).show();
|
||||
synchronized (this) {
|
||||
isLoading = false;
|
||||
}
|
||||
|
|
@ -79,7 +90,10 @@ public class RepoLoader {
|
|||
if (body != null) {
|
||||
String bodyString = body.string();
|
||||
Gson gson = new Gson();
|
||||
onlineModules = gson.fromJson(bodyString, OnlineModule[].class);
|
||||
Map<String, OnlineModule> modules = new HashMap<>();
|
||||
OnlineModule[] repoModules = gson.fromJson(bodyString, OnlineModule[].class);
|
||||
Arrays.stream(repoModules).forEach(onlineModule -> modules.put(onlineModule.getName(), onlineModule));
|
||||
onlineModules = modules;
|
||||
Files.write(repoFile, bodyString.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
for (Listener listener : listeners) {
|
||||
|
|
@ -87,6 +101,7 @@ public class RepoLoader {
|
|||
}
|
||||
synchronized (this) {
|
||||
isLoading = false;
|
||||
repoLoaded = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
@ -101,8 +116,12 @@ public class RepoLoader {
|
|||
listeners.remove(listener);
|
||||
}
|
||||
|
||||
public OnlineModule[] getOnlineModules() {
|
||||
return onlineModules;
|
||||
public OnlineModule getOnlineModule(String packageName) {
|
||||
return onlineModules.get(packageName);
|
||||
}
|
||||
|
||||
public Collection<OnlineModule> getOnlineModules() {
|
||||
return onlineModules.values();
|
||||
}
|
||||
|
||||
public interface Listener {
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@ package io.github.lsposed.manager.ui.activity;
|
|||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcelable;
|
||||
import android.text.SpannableStringBuilder;
|
||||
import android.text.TextUtils;
|
||||
import android.view.LayoutInflater;
|
||||
|
|
@ -41,7 +40,7 @@ import androidx.recyclerview.widget.DividerItemDecoration;
|
|||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
|
|
@ -100,6 +99,12 @@ public class RepoActivity extends BaseActivity implements RepoLoader.Listener {
|
|||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
repoLoader.removeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
|
|
@ -150,7 +155,8 @@ public class RepoActivity extends BaseActivity implements RepoLoader.Listener {
|
|||
holder.itemView.setOnClickListener(v -> {
|
||||
Intent intent = new Intent();
|
||||
intent.setClass(RepoActivity.this, RepoItemActivity.class);
|
||||
intent.putExtra("module", (Parcelable) module);
|
||||
intent.putExtra("modulePackageName", module.getName());
|
||||
intent.putExtra("moduleName", module.getDescription());
|
||||
startActivity(intent);
|
||||
});
|
||||
}
|
||||
|
|
@ -160,15 +166,16 @@ public class RepoActivity extends BaseActivity implements RepoLoader.Listener {
|
|||
return showList.size();
|
||||
}
|
||||
|
||||
public void setData(OnlineModule[] modules) {
|
||||
fullList = Arrays.asList(modules);
|
||||
public void setData(Collection<OnlineModule> modules) {
|
||||
fullList = new ArrayList<>(modules);
|
||||
fullList.sort((o1, o2) -> o1.getDescription().compareToIgnoreCase(o2.getDescription()));
|
||||
String queryStr = searchView != null ? searchView.getQuery().toString() : "";
|
||||
runOnUiThread(() -> getFilter().filter(queryStr));
|
||||
}
|
||||
|
||||
public void initData() {
|
||||
OnlineModule[] modules = repoLoader.getOnlineModules();
|
||||
if (modules.length == 0) {
|
||||
Collection<OnlineModule> modules = repoLoader.getOnlineModules();
|
||||
if (!repoLoader.isRepoLoaded()) {
|
||||
binding.swipeRefreshLayout.setRefreshing(true);
|
||||
repoLoader.loadRemoteData();
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -48,6 +48,7 @@ import io.github.lsposed.manager.databinding.ActivityModuleDetailBinding;
|
|||
import io.github.lsposed.manager.databinding.ItemRepoReadmeBinding;
|
||||
import io.github.lsposed.manager.databinding.ItemRepoReleaseBinding;
|
||||
import io.github.lsposed.manager.databinding.ItemRepoReleasesBinding;
|
||||
import io.github.lsposed.manager.repo.RepoLoader;
|
||||
import io.github.lsposed.manager.repo.model.OnlineModule;
|
||||
import io.github.lsposed.manager.repo.model.Release;
|
||||
import io.github.lsposed.manager.util.GlideApp;
|
||||
|
|
@ -67,19 +68,21 @@ public class RepoItemActivity extends BaseActivity {
|
|||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
binding = ActivityModuleDetailBinding.inflate(getLayoutInflater());
|
||||
String modulePackageName = getIntent().getStringExtra("modulePackageName");
|
||||
String moduleName = getIntent().getStringExtra("moduleName");
|
||||
setContentView(binding.getRoot());
|
||||
setSupportActionBar(binding.toolbar);
|
||||
module = getIntent().getParcelableExtra("module");
|
||||
binding.toolbar.setNavigationOnClickListener(view -> onBackPressed());
|
||||
ActionBar bar = getSupportActionBar();
|
||||
assert bar != null;
|
||||
bar.setTitle(module.getDescription());
|
||||
bar.setSubtitle(module.getName());
|
||||
bar.setTitle(moduleName);
|
||||
bar.setSubtitle(modulePackageName);
|
||||
bar.setDisplayHomeAsUpEnabled(true);
|
||||
markwon = Markwon.builder(this)
|
||||
.usePlugin(GlideImagesPlugin.create(GlideApp.with(this)))
|
||||
.usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS))
|
||||
.build();
|
||||
module = RepoLoader.getInstance().getOnlineModule(modulePackageName);
|
||||
binding.viewPager.setAdapter(new PagerAdapter());
|
||||
binding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
|
||||
@Override
|
||||
|
|
|
|||
Loading…
Reference in New Issue