Make inner class just a holder for resources.

This commit is contained in:
akwizgran
2020-04-27 17:54:41 +01:00
parent f35e87c8ad
commit ef5e2dad72
2 changed files with 31 additions and 30 deletions

View File

@@ -43,6 +43,7 @@ import java.util.logging.Logger;
import javax.inject.Inject; import javax.inject.Inject;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes; import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@@ -409,12 +410,13 @@ public class NavDrawerActivity extends BriarActivity implements
Transport t = getItem(position); Transport t = getItem(position);
ImageView icon = view.findViewById(R.id.imageView); ImageView icon = view.findViewById(R.id.imageView);
icon.setImageDrawable(ContextCompat icon.setImageDrawable(ContextCompat.getDrawable(
.getDrawable(NavDrawerActivity.this, t.iconId)); NavDrawerActivity.this, t.iconDrawable));
icon.setColorFilter(getIconColour(t.state)); icon.setColorFilter(ContextCompat.getColor(
NavDrawerActivity.this, t.iconColor));
TextView text = view.findViewById(R.id.textView); TextView text = view.findViewById(R.id.textView);
text.setText(getString(t.textId)); text.setText(getString(t.label));
return view; return view;
} }
@@ -428,23 +430,19 @@ public class NavDrawerActivity extends BriarActivity implements
R.drawable.transport_bt, R.string.transport_bt)); R.drawable.transport_bt, R.string.transport_bt));
} }
private int getIconColour(State state) { @ColorRes
int colorRes; private int getIconColor(State state) {
if (state == ACTIVE) { if (state == ACTIVE) return R.color.briar_green_light;
colorRes = R.color.briar_green_light; else if (state == ENABLING) return R.color.briar_yellow;
} else if (state == ENABLING) { else return android.R.color.tertiary_text_light;
colorRes = R.color.briar_yellow;
} else {
colorRes = android.R.color.tertiary_text_light;
}
return ContextCompat.getColor(this, colorRes);
} }
private Transport createTransport(TransportId id, @DrawableRes int iconId, private Transport createTransport(TransportId id,
@StringRes int textId) { @DrawableRes int iconDrawable, @StringRes int label) {
Transport transport = new Transport(iconId, textId); int iconColor = getIconColor(STARTING_STOPPING);
Transport transport = new Transport(iconDrawable, label, iconColor);
pluginViewModel.getPluginState(id).observe(this, state -> { pluginViewModel.getPluginState(id).observe(this, state -> {
transport.state = state; transport.iconColor = getIconColor(state);
transportsAdapter.notifyDataSetChanged(); transportsAdapter.notifyDataSetChanged();
}); });
return transport; return transport;
@@ -453,15 +451,18 @@ public class NavDrawerActivity extends BriarActivity implements
private static class Transport { private static class Transport {
@DrawableRes @DrawableRes
private final int iconId; private final int iconDrawable;
@StringRes @StringRes
private final int textId; private final int label;
private State state = STARTING_STOPPING; @ColorRes
private int iconColor;
private Transport(@DrawableRes int iconId, @StringRes int textId) { private Transport(@DrawableRes int iconDrawable, @StringRes int label,
this.iconId = iconId; @ColorRes int iconColor) {
this.textId = textId; this.iconDrawable = iconDrawable;
this.label = label;
this.iconColor = iconColor;
} }
} }
} }

View File

@@ -71,8 +71,8 @@ public class PluginViewModel extends ViewModel implements EventListener {
} }
} }
LiveData<State> getPluginState(TransportId t) { LiveData<State> getPluginState(TransportId id) {
LiveData<State> liveData = getPluginLiveData(t); LiveData<State> liveData = getPluginLiveData(id);
if (liveData == null) throw new IllegalArgumentException(); if (liveData == null) throw new IllegalArgumentException();
return liveData; return liveData;
} }
@@ -83,10 +83,10 @@ public class PluginViewModel extends ViewModel implements EventListener {
} }
@Nullable @Nullable
private MutableLiveData<State> getPluginLiveData(TransportId t) { private MutableLiveData<State> getPluginLiveData(TransportId id) {
if (t.equals(TorConstants.ID)) return torPluginState; if (id.equals(TorConstants.ID)) return torPluginState;
else if (t.equals(LanTcpConstants.ID)) return wifiPluginState; else if (id.equals(LanTcpConstants.ID)) return wifiPluginState;
else if (t.equals(BluetoothConstants.ID)) return btPluginState; else if (id.equals(BluetoothConstants.ID)) return btPluginState;
else return null; else return null;
} }
} }