Skip to content

Commit

Permalink
Use ListView to show startup stage errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kekkyojin committed Dec 9, 2020
1 parent c1324ef commit daf4e5a
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 199 deletions.
63 changes: 27 additions & 36 deletions app/src/main/java/io/lbry/browser/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

Expand Down Expand Up @@ -136,6 +137,7 @@

import io.lbry.browser.adapter.NavigationMenuAdapter;
import io.lbry.browser.adapter.NotificationListAdapter;
import io.lbry.browser.adapter.StartupStageAdapter;
import io.lbry.browser.adapter.UrlSuggestionListAdapter;
import io.lbry.browser.data.DatabaseHelper;
import io.lbry.browser.dialog.ContentScopeDialogFragment;
Expand All @@ -155,6 +157,7 @@
import io.lbry.browser.model.Claim;
import io.lbry.browser.model.ClaimCacheKey;
import io.lbry.browser.model.NavMenuItem;
import io.lbry.browser.model.StartupStage;
import io.lbry.browser.model.Tag;
import io.lbry.browser.model.UrlSuggestion;
import io.lbry.browser.model.WalletBalance;
Expand Down Expand Up @@ -2742,22 +2745,10 @@ public void showActionBar() {
actionBar.show();
}
}
private void renderStartupFailed(Map<Integer, Boolean> startupStages) {
Map<Integer, Integer> startupStageIconIds = new HashMap<>();
startupStageIconIds.put(STARTUP_STAGE_INSTALL_ID_LOADED, R.id.startup_stage_icon_install_id);
startupStageIconIds.put(STARTUP_STAGE_KNOWN_TAGS_LOADED, R.id.startup_stage_icon_known_tags);
startupStageIconIds.put(STARTUP_STAGE_EXCHANGE_RATE_LOADED, R.id.startup_stage_icon_exchange_rate);
startupStageIconIds.put(STARTUP_STAGE_USER_AUTHENTICATED, R.id.startup_stage_icon_user_authenticated);
startupStageIconIds.put(STARTUP_STAGE_NEW_INSTALL_DONE, R.id.startup_stage_icon_install_new);
startupStageIconIds.put(STARTUP_STAGE_SUBSCRIPTIONS_LOADED, R.id.startup_stage_icon_subscriptions_loaded);
startupStageIconIds.put(STARTUP_STAGE_SUBSCRIPTIONS_RESOLVED, R.id.startup_stage_icon_subscriptions_resolved);

for (Integer key : startupStages.keySet()) {
boolean stageDone = startupStages.get(key);
ImageView icon = findViewById(startupStageIconIds.get(key));
icon.setImageResource(stageDone ? R.drawable.ic_check : R.drawable.ic_close);
icon.setColorFilter(stageDone ? Color.WHITE : Color.RED);
}
private void renderStartupFailed(List<StartupStage> startupStages) {
ListView listView = findViewById(R.id.startup_stage_error_listview);
StartupStageAdapter adapter = new StartupStageAdapter(this, startupStages);
listView.setAdapter(adapter);

findViewById(R.id.splash_view_loading_container).setVisibility(View.GONE);
findViewById(R.id.splash_view_error_container).setVisibility(View.VISIBLE);
Expand All @@ -2770,16 +2761,16 @@ private void startup() {

// perform some tasks before launching
(new AsyncTask<Void, Void, Boolean>() {
private Map<Integer, Boolean> startupStages = new HashMap<>();
private final List<StartupStage> startupStages = new ArrayList<>(7);

private void initStartupStages() {
startupStages.put(STARTUP_STAGE_INSTALL_ID_LOADED, false);
startupStages.put(STARTUP_STAGE_KNOWN_TAGS_LOADED, false);
startupStages.put(STARTUP_STAGE_EXCHANGE_RATE_LOADED, false);
startupStages.put(STARTUP_STAGE_USER_AUTHENTICATED, false);
startupStages.put(STARTUP_STAGE_NEW_INSTALL_DONE, false);
startupStages.put(STARTUP_STAGE_SUBSCRIPTIONS_LOADED, false);
startupStages.put(STARTUP_STAGE_SUBSCRIPTIONS_RESOLVED, false);
startupStages.add(new StartupStage(STARTUP_STAGE_INSTALL_ID_LOADED, false));
startupStages.add(new StartupStage(STARTUP_STAGE_KNOWN_TAGS_LOADED, false));
startupStages.add(new StartupStage(STARTUP_STAGE_EXCHANGE_RATE_LOADED, false));
startupStages.add(new StartupStage(STARTUP_STAGE_USER_AUTHENTICATED, false));
startupStages.add(new StartupStage(STARTUP_STAGE_NEW_INSTALL_DONE, false));
startupStages.add(new StartupStage(STARTUP_STAGE_SUBSCRIPTIONS_LOADED, false));
startupStages.add(new StartupStage(STARTUP_STAGE_SUBSCRIPTIONS_RESOLVED, false));
}
protected void onPreExecute() {
hideActionBar();
Expand All @@ -2799,26 +2790,26 @@ protected Boolean doInBackground(Void... params) {
String installId = reader.readLine();
if (Helper.isNullOrEmpty(installId)) {
// no install_id found (first run didn't start the sdk successfully?)
startupStages.put(STARTUP_STAGE_INSTALL_ID_LOADED, false);
startupStages.set(STARTUP_STAGE_INSTALL_ID_LOADED - 1, new StartupStage(STARTUP_STAGE_INSTALL_ID_LOADED, false));
return false;
}

Lbry.INSTALLATION_ID = installId;
startupStages.put(STARTUP_STAGE_INSTALL_ID_LOADED, true);
startupStages.set(STARTUP_STAGE_INSTALL_ID_LOADED - 1, new StartupStage(STARTUP_STAGE_INSTALL_ID_LOADED, true));

SQLiteDatabase db = dbHelper.getReadableDatabase();
List<Tag> fetchedTags = DatabaseHelper.getTags(db);
Lbry.knownTags = Helper.mergeKnownTags(fetchedTags);
Collections.sort(Lbry.knownTags, new Tag());
Lbry.followedTags = Helper.filterFollowedTags(Lbry.knownTags);
startupStages.put(STARTUP_STAGE_KNOWN_TAGS_LOADED, true);
startupStages.set(STARTUP_STAGE_KNOWN_TAGS_LOADED - 1, new StartupStage(STARTUP_STAGE_KNOWN_TAGS_LOADED, true));

// load the exchange rate
Lbryio.loadExchangeRate();
if (Lbryio.LBCUSDRate == 0) {
return false;
}
startupStages.put(STARTUP_STAGE_EXCHANGE_RATE_LOADED, true);
startupStages.set(STARTUP_STAGE_EXCHANGE_RATE_LOADED - 1, new StartupStage(STARTUP_STAGE_EXCHANGE_RATE_LOADED, true));

try {
Lbryio.authenticate(context);
Expand All @@ -2830,10 +2821,10 @@ protected Boolean doInBackground(Void... params) {
if (Lbryio.currentUser == null) {
throw new Exception("Did not retrieve authenticated user.");
}
startupStages.put(STARTUP_STAGE_USER_AUTHENTICATED, true);
startupStages.set(STARTUP_STAGE_USER_AUTHENTICATED - 1, new StartupStage(STARTUP_STAGE_USER_AUTHENTICATED, true));

Lbryio.newInstall(context);
startupStages.put(STARTUP_STAGE_NEW_INSTALL_DONE, true);
startupStages.set(STARTUP_STAGE_NEW_INSTALL_DONE - 1, new StartupStage(STARTUP_STAGE_NEW_INSTALL_DONE, true));

// (light) fetch subscriptions
if (Lbryio.subscriptions.size() == 0) {
Expand All @@ -2854,23 +2845,23 @@ protected Boolean doInBackground(Void... params) {
subUrls.add(url.toString());
}
Lbryio.subscriptions = subscriptions;
startupStages.put(STARTUP_STAGE_SUBSCRIPTIONS_LOADED, true);
startupStages.set(STARTUP_STAGE_SUBSCRIPTIONS_LOADED - 1, new StartupStage(STARTUP_STAGE_SUBSCRIPTIONS_LOADED, true));

// resolve subscriptions
if (subUrls.size() > 0 && Lbryio.cacheResolvedSubscriptions.size() != Lbryio.subscriptions.size()) {
List<Claim> resolvedSubs = Lbry.resolve(subUrls, Lbry.LBRY_TV_CONNECTION_STRING);
Lbryio.cacheResolvedSubscriptions = resolvedSubs;
}
// if no exceptions occurred here, subscriptions have been loaded and resolved
startupStages.put(STARTUP_STAGE_SUBSCRIPTIONS_RESOLVED, true);
startupStages.set(STARTUP_STAGE_SUBSCRIPTIONS_RESOLVED - 1, new StartupStage(STARTUP_STAGE_SUBSCRIPTIONS_RESOLVED, true));
} else {
// user has not subscribed to anything
startupStages.put(STARTUP_STAGE_SUBSCRIPTIONS_LOADED, true);
startupStages.put(STARTUP_STAGE_SUBSCRIPTIONS_RESOLVED, true);
startupStages.set(STARTUP_STAGE_SUBSCRIPTIONS_LOADED - 1, new StartupStage(STARTUP_STAGE_SUBSCRIPTIONS_LOADED, true));
startupStages.set(STARTUP_STAGE_SUBSCRIPTIONS_RESOLVED - 1, new StartupStage(STARTUP_STAGE_SUBSCRIPTIONS_RESOLVED, true));
}
} else {
startupStages.put(STARTUP_STAGE_SUBSCRIPTIONS_LOADED, true);
startupStages.put(STARTUP_STAGE_SUBSCRIPTIONS_RESOLVED, true);
startupStages.set(STARTUP_STAGE_SUBSCRIPTIONS_LOADED - 1, new StartupStage(STARTUP_STAGE_SUBSCRIPTIONS_LOADED, true));
startupStages.set(STARTUP_STAGE_SUBSCRIPTIONS_RESOLVED - 1, new StartupStage(STARTUP_STAGE_SUBSCRIPTIONS_RESOLVED, true));
}
} catch (Exception ex) {
// nopecd
Expand Down
68 changes: 68 additions & 0 deletions app/src/main/java/io/lbry/browser/adapter/StartupStageAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package io.lbry.browser.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import io.lbry.browser.R;
import io.lbry.browser.model.StartupStage;

public class StartupStageAdapter extends BaseAdapter {
private final List<StartupStage> list;
private final LayoutInflater inflater;
private final String[] stagesString;

public StartupStageAdapter(Context ctx, List<StartupStage> rows) {
this.list = rows;
this.inflater = LayoutInflater.from(ctx);

stagesString = new String[7];

stagesString[0] = ctx.getResources().getString(R.string.installation_id_loaded);
stagesString[1] = ctx.getResources().getString(R.string.known_tags_loaded);
stagesString[2] = ctx.getResources().getString(R.string.exchange_rate_loaded);
stagesString[3] = ctx.getResources().getString(R.string.user_authenticated);
stagesString[4] = ctx.getResources().getString(R.string.installation_registered);
stagesString[5] = ctx.getResources().getString(R.string.subscriptions_loaded);
stagesString[6] = ctx.getResources().getString(R.string.subscriptions_resolved);
}
@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int i) {
return list.get(i);
}

@Override
public long getItemId(int i) {
return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.list_item_startupstage, viewGroup, false);

ImageView iconView = view.findViewById(R.id.startup_stage_icon);
TextView textView = view.findViewById(R.id.startup_stage_text);

StartupStage item = (StartupStage) getItem(i);

iconView.setImageResource(item.stageDone ? R.drawable.ic_check : R.drawable.ic_close);
iconView.setColorFilter(item.stageDone ? Color.WHITE : Color.RED);

textView.setText(stagesString[item.stage - 1]);
}
return view;
}
}
11 changes: 11 additions & 0 deletions app/src/main/java/io/lbry/browser/model/StartupStage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.lbry.browser.model;

public class StartupStage {
public Integer stage;
public Boolean stageDone;

public StartupStage(Integer stage, Boolean stageDone) {
this.stage = stage;
this.stageDone = stageDone;
}
}
Loading

0 comments on commit daf4e5a

Please sign in to comment.