Proper callback to trigger UI update

This commit is contained in:
topjohnwu 2018-08-03 23:04:35 +08:00
parent 0e0240c4ab
commit cf17e21ad3
4 changed files with 25 additions and 14 deletions

View File

@ -27,7 +27,7 @@ android {
productFlavors {
full {
versionCode 131
versionCode 132
versionName "5.8.3"
}
stub {

View File

@ -34,7 +34,7 @@ public class ReposFragment extends BaseFragment implements Topic.Subscriber {
@BindView(R.id.empty_rv) TextView emptyRv;
@BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;
public static ReposAdapter adapter;
private ReposAdapter adapter;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
@ -62,12 +62,6 @@ public class ReposFragment extends BaseFragment implements Topic.Subscriber {
return view;
}
@Override
public void onPause() {
super.onPause();
adapter = null;
}
@Override
public int[] getSubscribedTopics() {
return new int[] {Topic.MODULE_LOAD_DONE, Topic.REPO_LOAD_DONE};
@ -77,12 +71,12 @@ public class ReposFragment extends BaseFragment implements Topic.Subscriber {
public void onPublish(int topic, Object[] result) {
if (topic == Topic.MODULE_LOAD_DONE) {
adapter = new ReposAdapter(mm.repoDB, (Map<String, Module>) result[0]);
mm.repoDB.registerAdapter(adapter);
recyclerView.setAdapter(adapter);
recyclerView.setVisibility(View.VISIBLE);
emptyRv.setVisibility(View.GONE);
}
if (Topic.isPublished(getSubscribedTopics())) {
adapter.notifyDBChanged();
mSwipeRefreshLayout.setRefreshing(false);
recyclerView.setVisibility(adapter.getItemCount() == 0 ? View.GONE : View.VISIBLE);
emptyRv.setVisibility(adapter.getItemCount() == 0 ? View.VISIBLE : View.GONE);
@ -125,6 +119,7 @@ public class ReposFragment extends BaseFragment implements Topic.Subscriber {
@Override
public void onDestroyView() {
super.onDestroyView();
mm.repoDB.unregisterAdapter();
unbinder.unbind();
}
}

View File

@ -6,7 +6,6 @@ import android.os.AsyncTask;
import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.Data;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.ReposFragment;
import com.topjohnwu.magisk.container.Repo;
import com.topjohnwu.magisk.utils.Logger;
import com.topjohnwu.magisk.utils.Topic;
@ -80,10 +79,6 @@ public class UpdateRepos {
set.remove(id);
repo.update(date);
mm.repoDB.addRepo(repo);
Data.mainHandler.post(() -> {
if (ReposFragment.adapter != null)
ReposFragment.adapter.notifyDBChanged();
});
} catch (Repo.IllegalRepoException e) {
Logger.debug(e.getMessage());
mm.repoDB.removeRepo(id);

View File

@ -8,6 +8,7 @@ import android.database.sqlite.SQLiteOpenHelper;
import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.Data;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.adapters.ReposAdapter;
import com.topjohnwu.magisk.container.Repo;
import java.util.HashSet;
@ -20,6 +21,7 @@ public class RepoDatabaseHelper extends SQLiteOpenHelper {
private SQLiteDatabase mDb;
private MagiskManager mm;
private ReposAdapter adapter;
public RepoDatabaseHelper(Context context) {
super(context, "repo.db", null, DATABASE_VER);
@ -63,15 +65,18 @@ public class RepoDatabaseHelper extends SQLiteOpenHelper {
public void clearRepo() {
mDb.delete(TABLE_NAME, null, null);
notifyAdapter();
}
public void removeRepo(String id) {
mDb.delete(TABLE_NAME, "id=?", new String[] { id });
notifyAdapter();
}
public void removeRepo(Repo repo) {
mDb.delete(TABLE_NAME, "repo_name=?", new String[] { repo.getRepoName() });
notifyAdapter();
}
public void removeRepo(Iterable<String> list) {
@ -79,10 +84,12 @@ public class RepoDatabaseHelper extends SQLiteOpenHelper {
if (id == null) continue;
mDb.delete(TABLE_NAME, "id=?", new String[] { id });
}
notifyAdapter();
}
public void addRepo(Repo repo) {
mDb.replace(TABLE_NAME, null, repo.getContentValues());
notifyAdapter();
}
public Repo getRepo(String id) {
@ -121,4 +128,18 @@ public class RepoDatabaseHelper extends SQLiteOpenHelper {
}
return set;
}
public void registerAdapter(ReposAdapter a) {
adapter = a;
}
public void unregisterAdapter() {
adapter = null;
}
private void notifyAdapter() {
if (adapter != null) {
Data.mainHandler.post(adapter::notifyDBChanged);
}
}
}