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

99 lines
3.2 KiB
Java
Raw Normal View History

package com.topjohnwu.magisk;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
2016-10-17 02:11:26 +00:00
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
2016-10-17 02:11:26 +00:00
import android.widget.SearchView;
import android.widget.TextView;
2016-11-07 16:09:08 +00:00
import com.topjohnwu.magisk.adapters.ReposAdapter;
import com.topjohnwu.magisk.asyncs.UpdateRepos;
2017-02-06 18:01:32 +00:00
import com.topjohnwu.magisk.components.Fragment;
2017-02-06 20:09:49 +00:00
import com.topjohnwu.magisk.utils.CallbackEvent;
2016-09-25 15:11:57 +00:00
import com.topjohnwu.magisk.utils.Logger;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
2017-07-22 16:12:15 +00:00
public class ReposFragment extends Fragment implements CallbackEvent.Listener {
2016-12-25 07:11:59 +00:00
private Unbinder unbinder;
@BindView(R.id.recyclerView) RecyclerView recyclerView;
2017-01-27 17:10:50 +00:00
@BindView(R.id.empty_rv) TextView emptyRv;
@BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;
2016-10-17 02:11:26 +00:00
2017-07-18 15:18:57 +00:00
private ReposAdapter adapter;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
2017-01-25 09:07:23 +00:00
View view = inflater.inflate(R.layout.fragment_repos, container, false);
unbinder = ButterKnife.bind(this, view);
2016-09-27 14:57:20 +00:00
adapter = new ReposAdapter(getApplication().repoDB, getApplication().moduleMap);
2017-07-18 15:18:57 +00:00
recyclerView.setAdapter(adapter);
2016-10-17 02:11:26 +00:00
2016-09-27 14:57:20 +00:00
mSwipeRefreshLayout.setRefreshing(true);
mSwipeRefreshLayout.setOnRefreshListener(() -> {
recyclerView.setVisibility(View.GONE);
new UpdateRepos(getActivity()).exec();
2016-09-25 13:59:54 +00:00
});
2016-09-27 14:57:20 +00:00
2017-07-22 16:12:15 +00:00
getActivity().setTitle(R.string.downloads);
2016-09-27 14:57:20 +00:00
2016-09-28 17:42:25 +00:00
return view;
}
2016-12-25 07:11:59 +00:00
@Override
2017-07-22 16:12:15 +00:00
public void onTrigger(CallbackEvent event) {
2016-12-25 07:11:59 +00:00
Logger.dev("ReposFragment: UI refresh triggered");
mSwipeRefreshLayout.setRefreshing(false);
adapter.notifyDBChanged();
recyclerView.setVisibility(adapter.getItemCount() == 0 ? View.GONE : View.VISIBLE);
emptyRv.setVisibility(adapter.getItemCount() == 0 ? View.VISIBLE : View.GONE);
2016-12-25 07:11:59 +00:00
}
2017-07-22 16:12:15 +00:00
@Override
public CallbackEvent[] getRegisterEvents() {
return new CallbackEvent[] { getApplication().repoLoadDone };
}
2016-09-13 20:44:07 +00:00
@Override
2016-10-17 02:11:26 +00:00
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_repo, menu);
2017-07-18 15:18:57 +00:00
SearchView search = (SearchView) menu.findItem(R.id.repo_search).getActionView();
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.filter(newText);
2017-07-18 15:18:57 +00:00
return false;
}
});
2016-09-13 20:44:07 +00:00
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
}