Refactored repo details fragment.

This commit is contained in:
Peter Serwylo 2013-12-09 18:41:53 +11:00
parent 41e0919c6f
commit 9384bc093b

View File

@ -13,13 +13,15 @@ import android.view.*;
import android.widget.*; import android.widget.*;
import org.fdroid.fdroid.*; import org.fdroid.fdroid.*;
import java.util.List;
public class RepoDetailsFragment extends Fragment { public class RepoDetailsFragment extends Fragment {
public static final String ARG_REPO_ID = "repo_id"; public static final String ARG_REPO_ID = "repo_id";
/** /**
* If the repo has been updated at least once, then we will show * If the repo has been updated at least once, then we will show
* all of this info, otherwise it will be hidden. * all of this info, otherwise they will be hidden.
*/ */
private static final int[] SHOW_IF_EXISTS = { private static final int[] SHOW_IF_EXISTS = {
R.id.label_repo_name, R.id.label_repo_name,
@ -134,36 +136,71 @@ public class RepoDetailsFragment extends Fragment {
private void updateView(ViewGroup repoView) { private void updateView(ViewGroup repoView) {
EditText inputUrl = (EditText)repoView.findViewById(R.id.input_repo_url); EditText inputUrl = (EditText)repoView.findViewById(R.id.input_repo_url);
inputUrl.setText(repo.address);
if (repo.hasBeenUpdated()) {
updateViewForExistingRepo(repoView);
} else {
updateViewForNewRepo(repoView);
}
}
/**
* Help function to make switching between two view states easier.
* Perhaps there is a better way to do this. I recall that using Adobe
* Flex, there was a thing called "ViewStates" for exactly this. Wonder if
* that exists in Android?
*/
private static void setMultipleViewVisibility(ViewGroup parent,
int[] viewIds,
int visibility) {
for (int viewId : viewIds) {
parent.findViewById(viewId).setVisibility(visibility);
}
}
private void updateViewForNewRepo(ViewGroup repoView) {
setMultipleViewVisibility(repoView, HIDE_IF_EXISTS, View.VISIBLE);
setMultipleViewVisibility(repoView, SHOW_IF_EXISTS, View.GONE);
}
private void updateViewForExistingRepo(ViewGroup repoView) {
setMultipleViewVisibility(repoView, SHOW_IF_EXISTS, View.VISIBLE);
setMultipleViewVisibility(repoView, HIDE_IF_EXISTS, View.GONE);
TextView name = (TextView)repoView.findViewById(R.id.text_repo_name); TextView name = (TextView)repoView.findViewById(R.id.text_repo_name);
TextView numApps = (TextView)repoView.findViewById(R.id.text_num_apps); TextView numApps = (TextView)repoView.findViewById(R.id.text_num_apps);
TextView lastUpdated = (TextView)repoView.findViewById(R.id.text_last_update); TextView lastUpdated = (TextView)repoView.findViewById(R.id.text_last_update);
TextView description = (TextView)repoView.findViewById(R.id.text_description);
TextView signature = (TextView)repoView.findViewById(R.id.text_signature);
TextView signatureInfo = (TextView)repoView.findViewById(R.id.text_signature_description);
boolean hasBeenUpdated = repo.lastetag != null;
int showIfExists = hasBeenUpdated ? View.VISIBLE : View.GONE;
int hideIfExists = hasBeenUpdated ? View.GONE : View.VISIBLE;
for (int id : SHOW_IF_EXISTS) {
repoView.findViewById(id).setVisibility(showIfExists);
}
for (int id : HIDE_IF_EXISTS) {
repoView.findViewById(id).setVisibility(hideIfExists);
}
inputUrl.setText(repo.address);
name.setText(repo.getName()); name.setText(repo.getName());
numApps.setText(Integer.toString(repo.getNumberOfApps())); numApps.setText(Integer.toString(repo.getNumberOfApps()));
description.setText(repo.description);
setupSignature(repo, signature, signatureInfo);
if (repo.lastUpdated != null) { setupDescription(repoView, repo);
lastUpdated.setText(repo.lastUpdated.toString()); setupSignature(repoView, repo);
} else {
lastUpdated.setText(getString(R.string.unknown)); // Repos that existed before this feature was supported will have an
// "Unknown" last update until next time they update...
String lastUpdate = repo.lastUpdated != null
? repo.lastUpdated.toString() : getString(R.string.unknown);
lastUpdated.setText(lastUpdate);
} }
private void setupDescription(ViewGroup parent, DB.Repo repo) {
TextView descriptionLabel = (TextView)parent.findViewById(R.id.label_description);
TextView description = (TextView)parent.findViewById(R.id.text_description);
if (repo.description == null || repo.description.length() == 0) {
descriptionLabel.setVisibility(View.GONE);
description.setVisibility(View.GONE);
} else {
descriptionLabel.setVisibility(View.VISIBLE);
description.setVisibility(View.VISIBLE);
}
description.setText(repo.description);
} }
/** /**
@ -261,23 +298,26 @@ public class RepoDetailsFragment extends Fragment {
).show(); ).show();
} }
private void setupSignature(DB.Repo repo, TextView signatureView, private void setupSignature(ViewGroup parent, DB.Repo repo) {
TextView signatureDescView) { TextView signatureView = (TextView)parent.findViewById(R.id.text_signature);
TextView signatureDescView = (TextView)parent.findViewById(R.id.text_signature_description);
String signature; String signature;
String signatureDesc;
int signatureColour; int signatureColour;
if (repo.pubkey != null && repo.pubkey.length() > 0) { if (repo.pubkey != null && repo.pubkey.length() > 0) {
signature = Utils.formatFingerprint(repo.pubkey); signature = Utils.formatFingerprint(repo.pubkey);
signatureDesc = "";
signatureColour = getResources().getColor(R.color.signed); signatureColour = getResources().getColor(R.color.signed);
signatureDescView.setVisibility(View.GONE);
} else { } else {
signature = getResources().getString(R.string.unsigned); signature = getResources().getString(R.string.unsigned);
signatureDesc = getResources().getString(R.string.unsigned_description);
signatureColour = getResources().getColor(R.color.unsigned); signatureColour = getResources().getColor(R.color.unsigned);
signatureDescView.setVisibility(View.VISIBLE);
signatureDescView.setText(getResources().getString(R.string.unsigned_description));
} }
signatureView.setText(signature); signatureView.setText(signature);
signatureView.setTextColor(signatureColour); signatureView.setTextColor(signatureColour);
signatureDescView.setText(signatureDesc);
} }
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {