Magisk/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java

123 lines
4.4 KiB
Java
Raw Normal View History

package com.topjohnwu.magisk;
2016-09-23 21:22:11 +00:00
import android.app.Fragment;
2016-09-27 14:57:20 +00:00
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
2016-09-25 15:11:57 +00:00
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.topjohnwu.magisk.module.Repo;
2016-09-20 17:08:05 +00:00
import com.topjohnwu.magisk.utils.Async;
2016-09-25 15:11:57 +00:00
import com.topjohnwu.magisk.utils.Logger;
2016-09-28 17:42:25 +00:00
import com.topjohnwu.magisk.utils.ModuleHelper;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class ReposFragment extends Fragment {
2016-09-28 17:42:25 +00:00
private List<Repo> mListRepos = new ArrayList<>();
private List<Repo> mUpdateRepos = new ArrayList<>();
private List<Repo> mInstalledRepos = new ArrayList<>();
private List<Repo> mOthersRepos = new ArrayList<>();
2016-09-27 14:57:20 +00:00
@BindView(R.id.recyclerView) RecyclerView recyclerView;
@BindView(R.id.empty_rv) TextView emptyTv;
@BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;
private SharedPreferences.OnSharedPreferenceChangeListener listener;
private SharedPreferences prefs;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
2016-09-28 17:42:25 +00:00
View view = inflater.inflate(R.layout.repos_fragment, container, false);
ButterKnife.bind(this, view);
2016-09-27 14:57:20 +00:00
mSwipeRefreshLayout.setRefreshing(true);
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
mSwipeRefreshLayout.setOnRefreshListener(() -> {
recyclerView.setVisibility(View.GONE);
prefs.edit().putBoolean("repo_done", false).apply();
new Async.LoadRepos(getActivity()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2016-09-25 13:59:54 +00:00
});
2016-09-27 14:57:20 +00:00
if (prefs.getBoolean("repo_done", false)) {
updateUI();
}
listener = (pref, s) -> {
if (s.equals("repo_done")) {
if (pref.getBoolean(s, false)) {
2016-09-27 16:33:01 +00:00
Logger.dev("ReposFragment: UI refresh triggered");
2016-09-27 14:57:20 +00:00
updateUI();
}
}
};
2016-09-28 17:42:25 +00:00
return view;
}
2016-09-13 20:44:07 +00:00
@Override
public void onAttachFragment(Fragment childFragment) {
super.onAttachFragment(childFragment);
}
@Override
public void onResume() {
super.onResume();
2016-09-27 14:57:20 +00:00
getActivity().setTitle(R.string.downloads);
prefs.registerOnSharedPreferenceChangeListener(listener);
2016-09-23 21:22:11 +00:00
}
2016-09-27 14:57:20 +00:00
@Override
public void onDestroy() {
super.onDestroy();
prefs.unregisterOnSharedPreferenceChangeListener(listener);
}
2016-09-27 14:57:20 +00:00
private void updateUI() {
2016-09-28 17:42:25 +00:00
ModuleHelper.getRepoLists(mUpdateRepos, mInstalledRepos, mOthersRepos);
mListRepos.clear();
mListRepos.addAll(mUpdateRepos);
mListRepos.addAll(mInstalledRepos);
mListRepos.addAll(mOthersRepos);
2016-09-27 14:57:20 +00:00
if (mListRepos.size() == 0) {
emptyTv.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
} else {
2016-09-28 17:42:25 +00:00
List<SimpleSectionedRecyclerViewAdapter.Section> sections = new ArrayList<>();
if (!mUpdateRepos.isEmpty()) {
sections.add(new SimpleSectionedRecyclerViewAdapter.Section(0, getString(R.string.update_available)));
}
if (!mInstalledRepos.isEmpty()) {
sections.add(new SimpleSectionedRecyclerViewAdapter.Section(mUpdateRepos.size(), getString(R.string.installed)));
}
if (!mOthersRepos.isEmpty()) {
sections.add(new SimpleSectionedRecyclerViewAdapter.Section(mUpdateRepos.size() + mInstalledRepos.size(), getString(R.string.not_installed)));
}
SimpleSectionedRecyclerViewAdapter.Section[] array = sections.toArray(new SimpleSectionedRecyclerViewAdapter.Section[sections.size()]);
SimpleSectionedRecyclerViewAdapter mSectionedAdapter = new
SimpleSectionedRecyclerViewAdapter(getActivity(), R.layout.section, R.id.section_text, new ReposAdapter(mListRepos));
mSectionedAdapter.setSections(array);
2016-09-27 14:57:20 +00:00
recyclerView.setVisibility(View.VISIBLE);
2016-09-28 17:42:25 +00:00
recyclerView.setAdapter(mSectionedAdapter);
}
2016-09-27 14:57:20 +00:00
mSwipeRefreshLayout.setRefreshing(false);
}
}