Skip to content

Commit

Permalink
Proper callback to trigger UI update
Browse files Browse the repository at this point in the history
  • Loading branch information
topjohnwu committed Aug 3, 2018
1 parent 0e0240c commit cf17e21
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ android {

productFlavors {
full {
versionCode 131
versionCode 132
versionName "5.8.3"
}
stub {
Expand Down
11 changes: 3 additions & 8 deletions app/src/full/java/com/topjohnwu/magisk/ReposFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -62,12 +62,6 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
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};
Expand All @@ -77,12 +71,12 @@ public int[] getSubscribedTopics() {
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);
Expand Down Expand Up @@ -125,6 +119,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
@Override
public void onDestroyView() {
super.onDestroyView();
mm.repoDB.unregisterAdapter();
unbinder.unbind();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
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;
Expand Down Expand Up @@ -80,10 +79,6 @@ private boolean loadJSON(String jsonString) throws JSONException, ParseException
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand All @@ -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);
Expand Down Expand Up @@ -63,26 +65,31 @@ public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {

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) {
for (String id : list) {
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) {
Expand Down Expand Up @@ -121,4 +128,18 @@ public Set<String> getRepoIDSet() {
}
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);
}
}
}

0 comments on commit cf17e21

Please sign in to comment.