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