use of com.mikhaellopez.circularprogressbar.CircularProgressBar in project iNaturalistAndroid by inaturalist.
the class ObservationCursorAdapter method getView.
public View getView(int position, View convertView, ViewGroup parent) {
final View view = super.getView(position, convertView, parent);
ViewHolder holder;
Cursor c = this.getCursor();
if (c.getCount() == 0) {
return view;
}
c.moveToPosition(position);
final Long obsId = c.getLong(c.getColumnIndexOrThrow(Observation._ID));
final Long externalObsId = c.getLong(c.getColumnIndexOrThrow(Observation.ID));
Long updatedAt = c.getLong(c.getColumnIndexOrThrow(Observation._UPDATED_AT));
final String obsUUID = c.getString(c.getColumnIndexOrThrow(Observation.UUID));
String speciesGuessValue = c.getString(c.getColumnIndexOrThrow(Observation.SPECIES_GUESS));
String[] photoInfo = obsUUID != null ? mPhotoInfo.get(obsUUID) : null;
boolean hasErrors = (mApp.getErrorsForObservation(externalObsId.intValue()).length() > 0);
boolean isBeingSynced = (mApp.getObservationIdBeingSynced() == obsId);
if (convertView == null) {
holder = new ViewHolder((ViewGroup) view);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
Observation observation = new Observation(c);
if ((holder.obsId == obsId) && (holder.observation != null) && (holder.observation.equals(observation))) {
String photoFilename = photoInfo != null ? (photoInfo[2] != null ? photoInfo[2] : photoInfo[0]) : null;
if ((holder.photoFilename == photoFilename) && (holder.hasErrors == hasErrors) && (holder.isBeingSynced == isBeingSynced) && (holder.isBeingSynced == (mApp.getObservationIdBeingSynced() == obsId))) {
int desiredDimension = 0, currentDimension = 0;
if (mGrid != null) {
// Make sure the screen hasn't been rotated (and that means the image will be of a different size)
desiredDimension = mGrid.getColumnWidth();
currentDimension = holder.obsImage.getLayoutParams().width;
}
if (desiredDimension == currentDimension) {
// This view is already showing the current obs
return view;
}
}
}
}
final ImageView obsImage = holder.obsImage;
ImageView obsIconicImage = holder.obsIconicImage;
TextView speciesGuess = holder.speciesGuess;
TextView dateObserved = holder.dateObserved;
ViewGroup commentIdContainer = holder.commentIdContainer;
ViewGroup leftContainer = holder.leftContainer;
ImageView commentIcon = holder.commentIcon;
ImageView idIcon = holder.idIcon;
TextView commentCount = holder.commentCount;
TextView idCount = holder.idCount;
TextView placeGuess = holder.placeGuess;
ImageView locationIcon = holder.locationIcon;
View progress = holder.progress;
View progressInner = holder.progressInner;
String placeGuessValue = c.getString(c.getColumnIndexOrThrow(Observation.PLACE_GUESS));
String privatePlaceGuessValue = c.getString(c.getColumnIndexOrThrow(Observation.PRIVATE_PLACE_GUESS));
Double latitude = c.getDouble(c.getColumnIndexOrThrow(Observation.LATITUDE));
Double longitude = c.getDouble(c.getColumnIndexOrThrow(Observation.LONGITUDE));
Double privateLatitude = c.getDouble(c.getColumnIndexOrThrow(Observation.PRIVATE_LATITUDE));
Double privateLongitude = c.getDouble(c.getColumnIndexOrThrow(Observation.PRIVATE_LONGITUDE));
if (mIsGrid) {
mDimension = mGrid.getColumnWidth();
obsImage.setLayoutParams(new RelativeLayout.LayoutParams(mDimension, mDimension));
progress.setLayoutParams(new RelativeLayout.LayoutParams(mDimension, mDimension));
// So final image size will be 48% of original size
int newDimension = (int) (mDimension * 0.48);
int speciesGuessHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, mContext.getResources().getDisplayMetrics());
int leftRightMargin = (mDimension - newDimension) / 2;
int topBottomMargin = (mDimension - speciesGuessHeight - newDimension) / 2;
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(newDimension, newDimension);
layoutParams.setMargins(leftRightMargin, topBottomMargin, leftRightMargin, 0);
obsIconicImage.setLayoutParams(layoutParams);
}
String iconicTaxonName = c.getString(c.getColumnIndexOrThrow(Observation.ICONIC_TAXON_NAME));
int iconResource = getIconicTaxonDrawable(iconicTaxonName);
obsIconicImage.setVisibility(View.VISIBLE);
obsIconicImage.setImageResource(iconResource);
obsImage.setVisibility(View.INVISIBLE);
if (photoInfo != null) {
String photoFilename = photoInfo[2] != null ? photoInfo[2] : photoInfo[0];
if (mIsGrid && (convertView == null)) {
obsImage.setLayoutParams(new RelativeLayout.LayoutParams(mDimension, mDimension));
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
mDimension = mGrid.getColumnWidth();
obsImage.setLayoutParams(new RelativeLayout.LayoutParams(mDimension, mDimension));
}
});
}
loadObsImage(position, obsImage, photoFilename, photoInfo[2] != null);
holder.photoFilename = photoFilename;
} else {
obsImage.setVisibility(View.INVISIBLE);
holder.photoFilename = null;
}
Long observationTimestamp = c.getLong(c.getColumnIndexOrThrow(Observation.TIME_OBSERVED_AT));
if (!mIsGrid) {
if (observationTimestamp == 0) {
// No observation date set - don't show it
dateObserved.setVisibility(View.INVISIBLE);
} else {
dateObserved.setVisibility(View.VISIBLE);
Timestamp observationDate = new Timestamp(observationTimestamp);
dateObserved.setText(CommentsIdsAdapter.formatIdDate(mContext, observationDate));
}
}
Long commentsCount = c.getLong(c.getColumnIndexOrThrow(Observation.COMMENTS_COUNT));
Long idsCount = c.getLong(c.getColumnIndexOrThrow(Observation.IDENTIFICATIONS_COUNT));
Long lastCommentsCount = c.getLong(c.getColumnIndexOrThrow(Observation.LAST_COMMENTS_COUNT));
Long lastIdCount = c.getLong(c.getColumnIndexOrThrow(Observation.LAST_IDENTIFICATIONS_COUNT));
if (commentsCount + idsCount == 0) {
// No comments/IDs - don't display the indicator
commentIdContainer.setVisibility(View.INVISIBLE);
commentIdContainer.setClickable(false);
} else {
commentIdContainer.setClickable(true);
commentIdContainer.setVisibility(View.VISIBLE);
if ((lastCommentsCount == null) || (lastCommentsCount < commentsCount) || (lastIdCount == null) || (lastIdCount < idsCount)) {
// There are unread comments/IDs
commentIdContainer.setVisibility(View.VISIBLE);
if (mIsGrid) {
commentIdContainer.setBackgroundColor(Color.parseColor("#EA118D"));
} else {
commentCount.setTextColor(Color.parseColor("#EA118D"));
idCount.setTextColor(Color.parseColor("#EA118D"));
commentIcon.setColorFilter(Color.parseColor("#EA118D"));
idIcon.setColorFilter(Color.parseColor("#EA118D"));
}
} else {
if (mIsGrid) {
// Don't show comment/id count if no unread ones are available
commentIdContainer.setVisibility(View.INVISIBLE);
} else {
commentCount.setTextColor(Color.parseColor("#959595"));
idCount.setTextColor(Color.parseColor("#959595"));
commentIcon.setColorFilter(Color.parseColor("#707070"));
idIcon.setColorFilter(Color.parseColor("#707070"));
}
}
if (commentsCount > 0) {
commentCount.setText(String.valueOf(commentsCount));
commentCount.setVisibility(View.VISIBLE);
commentIcon.setVisibility(View.VISIBLE);
} else {
commentCount.setVisibility(View.GONE);
commentIcon.setVisibility(View.GONE);
}
if (idsCount > 0) {
idCount.setText(String.valueOf(idsCount));
idCount.setVisibility(View.VISIBLE);
idIcon.setVisibility(View.VISIBLE);
} else {
idCount.setVisibility(View.GONE);
idIcon.setVisibility(View.GONE);
}
commentIdContainer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isNetworkAvailable()) {
Toast.makeText(mContext.getApplicationContext(), R.string.not_connected, Toast.LENGTH_LONG).show();
return;
}
// Show the comments/IDs for the observation
Uri uri = ContentUris.withAppendedId(Observation.CONTENT_URI, obsId);
Intent intent = new Intent(Intent.ACTION_VIEW, uri, mContext, ObservationViewerActivity.class);
intent.putExtra(ObservationViewerActivity.SHOW_COMMENTS, true);
mContext.startActivity(intent);
}
});
if (!mIsGrid) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) leftContainer.getLayoutParams();
if (dateObserved.getText().length() > String.format(" %d %d", idsCount, commentsCount).length()) {
params.addRule(RelativeLayout.LEFT_OF, R.id.date);
} else {
params.addRule(RelativeLayout.LEFT_OF, R.id.comment_id_container);
}
leftContainer.setLayoutParams(params);
}
}
Long syncedAt = c.getLong(c.getColumnIndexOrThrow(Observation._SYNCED_AT));
Boolean syncNeeded = (syncedAt == null) || (updatedAt > syncedAt);
// if there's a photo and it is local
if (syncNeeded == false && photoInfo != null && photoInfo[2] == null && photoInfo[3] != null) {
if (photoInfo[4] == null) {
syncNeeded = true;
} else {
Long photoSyncedAt = Long.parseLong(photoInfo[4]);
Long photoUpdatedAt = Long.parseLong(photoInfo[3]);
if (photoUpdatedAt > photoSyncedAt) {
syncNeeded = true;
}
}
}
if (!syncNeeded) {
// See if it's an existing observation with a new photo
Cursor opc = mContext.getContentResolver().query(ObservationPhoto.CONTENT_URI, new String[] { ObservationPhoto._ID, ObservationPhoto._OBSERVATION_ID, ObservationPhoto._PHOTO_ID, ObservationPhoto.PHOTO_URL, ObservationPhoto._UPDATED_AT, ObservationPhoto._SYNCED_AT }, "_observation_id = ? AND photo_url IS NULL AND _synced_at IS NULL", new String[] { String.valueOf(obsId) }, ObservationPhoto._ID);
if (opc.getCount() > 0) {
syncNeeded = true;
}
opc.close();
}
if (!mIsGrid) {
if (((placeGuessValue == null) || (placeGuessValue.length() == 0)) && ((privatePlaceGuessValue == null) || (privatePlaceGuessValue.length() == 0))) {
if ((longitude != 0f) || (latitude != 0f) || (privateLatitude != 0f) || (privateLongitude != 0f)) {
// Show coordinates instead
placeGuess.setText(String.format(mContext.getString(R.string.location_coords_no_acc), String.format("%.4f...", latitude != 0f ? latitude : privateLatitude), String.format("%.4f...", longitude != 0f ? longitude : privateLongitude)));
} else {
// No place at all
placeGuess.setText(R.string.no_location);
}
} else {
placeGuess.setText((privatePlaceGuessValue != null) && (privatePlaceGuessValue.length() > 0) ? privatePlaceGuessValue : placeGuessValue);
}
}
holder.syncNeeded = syncNeeded;
String description = c.getString(c.getColumnIndexOrThrow(Observation.DESCRIPTION));
String preferredCommonName = c.getString(c.getColumnIndexOrThrow(Observation.PREFERRED_COMMON_NAME));
progress.setVisibility(View.GONE);
if (!mIsGrid) {
placeGuess.setTextColor(Color.parseColor("#666666"));
dateObserved.setVisibility(View.VISIBLE);
speciesGuess.setTextColor(Color.parseColor("#000000"));
}
if (preferredCommonName != null) {
speciesGuess.setText(preferredCommonName);
} else if ((speciesGuessValue != null) && (speciesGuessValue.trim().length() > 0)) {
speciesGuess.setText("\"" + speciesGuessValue + "\"");
} else if ((description != null) && (description.length() > 0)) {
speciesGuess.setText(description);
} else {
speciesGuess.setText(R.string.unknown_species);
}
holder.hasErrors = hasErrors;
if (hasErrors) {
view.setBackgroundResource(R.drawable.observation_item_error_background);
if (!mIsGrid) {
placeGuess.setText(R.string.needs_your_attention);
locationIcon.setVisibility(View.GONE);
}
} else {
if (!mIsGrid) {
locationIcon.setVisibility(View.VISIBLE);
view.setBackgroundResource(R.drawable.observation_item_background);
} else {
view.setBackgroundColor(Color.parseColor("#DDDDDD"));
}
}
holder.isBeingSynced = (mApp.getObservationIdBeingSynced() == obsId);
if (mApp.getObservationIdBeingSynced() == obsId) {
CircularProgressBar currentProgressBar = (CircularProgressBar) (progressInner != null ? progressInner : progress);
if (currentProgressBar != mCurrentProgressBar) {
currentProgressBar.setProgress(0);
}
mCurrentProgressBar = currentProgressBar;
// Observation is currently being uploaded
view.setBackgroundResource(R.drawable.observation_item_uploading_background);
if (!mIsGrid) {
placeGuess.setText(R.string.uploading);
placeGuess.setTextColor(Color.parseColor("#74Ac00"));
locationIcon.setVisibility(View.GONE);
dateObserved.setVisibility(View.GONE);
}
progress.setVisibility(View.VISIBLE);
commentIdContainer.setVisibility(View.INVISIBLE);
} else if (syncNeeded && (mApp.getObservationIdBeingSynced() != obsId)) {
// This observation needs to be synced (and waiting to be synced)
if (!hasErrors) {
view.setBackgroundResource(R.drawable.observation_item_uploading_background);
if (!mIsGrid) {
placeGuess.setText(R.string.waiting_to_upload);
locationIcon.setVisibility(View.GONE);
}
}
} else {
if (!hasErrors) {
if (!mIsGrid) {
view.setBackgroundResource(R.drawable.observation_item_background);
} else {
view.setBackgroundColor(Color.parseColor("#DDDDDD"));
}
}
}
holder.obsId = obsId;
holder.updatedAt = updatedAt;
holder.observation = new Observation(c);
return view;
}
Aggregations