diff options
Diffstat (limited to 'main/src/cgeo/geocaching/LogCacheActivity.java')
-rw-r--r-- | main/src/cgeo/geocaching/LogCacheActivity.java | 160 |
1 files changed, 91 insertions, 69 deletions
diff --git a/main/src/cgeo/geocaching/LogCacheActivity.java b/main/src/cgeo/geocaching/LogCacheActivity.java index bc87525..6fbd7bf 100644 --- a/main/src/cgeo/geocaching/LogCacheActivity.java +++ b/main/src/cgeo/geocaching/LogCacheActivity.java @@ -1,6 +1,7 @@ package cgeo.geocaching; import butterknife.ButterKnife; +import butterknife.InjectView; import cgeo.geocaching.activity.ShowcaseViewBuilder; import cgeo.geocaching.connector.ILoggingManager; @@ -11,12 +12,14 @@ import cgeo.geocaching.enumerations.LogType; import cgeo.geocaching.enumerations.LogTypeTrackable; import cgeo.geocaching.enumerations.StatusCode; import cgeo.geocaching.gcvote.GCVote; +import cgeo.geocaching.gcvote.GCVoteRatingBarUtil; +import cgeo.geocaching.gcvote.GCVoteRatingBarUtil.OnRatingChangeListener; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.twitter.Twitter; import cgeo.geocaching.ui.dialog.DateDialog; import cgeo.geocaching.ui.dialog.Dialogs; import cgeo.geocaching.utils.AsyncTaskWithProgress; -import cgeo.geocaching.utils.DateUtils; +import cgeo.geocaching.utils.CalendarUtils; import cgeo.geocaching.utils.Formatter; import cgeo.geocaching.utils.Log; import cgeo.geocaching.utils.LogTemplateProvider; @@ -44,8 +47,6 @@ import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.LinearLayout; -import android.widget.RatingBar; -import android.widget.RatingBar.OnRatingBarChangeListener; import android.widget.TextView; import java.util.ArrayList; @@ -54,8 +55,6 @@ import java.util.Date; import java.util.List; public class LogCacheActivity extends AbstractLoggingActivity implements DateDialog.DateDialogParent { - static final String EXTRAS_GEOCODE = "geocode"; - static final String EXTRAS_ID = "id"; private static final String SAVED_STATE_RATING = "cgeo.geocaching.saved_state_rating"; private static final String SAVED_STATE_TYPE = "cgeo.geocaching.saved_state_type"; @@ -72,9 +71,9 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia private String text = null; private List<LogType> possibleLogTypes = new ArrayList<>(); private List<TrackableLog> trackables = null; - private CheckBox tweetCheck = null; - private LinearLayout tweetBox = null; - private LinearLayout logPasswordBox = null; + protected @InjectView(R.id.tweet) CheckBox tweetCheck; + protected @InjectView(R.id.tweet_box) LinearLayout tweetBox; + protected @InjectView(R.id.log_password_box) LinearLayout logPasswordBox; private SparseArray<TrackableLog> actionButtons; private ILoggingManager loggingManager; @@ -88,9 +87,7 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia private Uri imageUri; private boolean sendButtonEnabled; - public void onLoadFinished() { - if (loggingManager.hasLoaderError()) { showErrorLoadingData(); return; @@ -111,6 +108,8 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia showToast(res.getString(R.string.info_log_type_changed)); } + initializeRatingBar(); + enablePostButton(true); initializeTrackablesAction(); @@ -141,7 +140,7 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia } actionButtons = new SparseArray<>(); - final LinearLayout inventoryView = (LinearLayout) findViewById(R.id.inventory); + final LinearLayout inventoryView = ButterKnife.findById(this, R.id.inventory); inventoryView.removeAllViews(); for (final TrackableLog tb : trackables) { @@ -165,7 +164,7 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia final String tbCode = tb.trackCode; inventoryItem.setClickable(true); - inventoryItem.findViewById(R.id.info).setOnClickListener(new View.OnClickListener() { + ButterKnife.findById(inventoryItem, R.id.info).setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View view) { @@ -179,10 +178,10 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia } if (inventoryView.getChildCount() > 0) { - findViewById(R.id.inventory_box).setVisibility(View.VISIBLE); + ButterKnife.findById(this, R.id.inventory_box).setVisibility(View.VISIBLE); } if (inventoryView.getChildCount() > 1) { - final LinearLayout inventoryChangeAllView = (LinearLayout) findViewById(R.id.inventory_changeall); + final LinearLayout inventoryChangeAllView = ButterKnife.findById(this, R.id.inventory_changeall); final Button changeButton = ButterKnife.findById(inventoryChangeAllView, R.id.changebutton); changeButton.setOnClickListener(new View.OnClickListener() { @@ -208,9 +207,9 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia // Get parameters from intent and basic cache information from database final Bundle extras = getIntent().getExtras(); if (extras != null) { - geocode = extras.getString(EXTRAS_GEOCODE); + geocode = extras.getString(Intents.EXTRA_GEOCODE); if (StringUtils.isBlank(geocode)) { - final String cacheid = extras.getString(EXTRAS_ID); + final String cacheid = extras.getString(Intents.EXTRA_ID); if (StringUtils.isNotBlank(cacheid)) { geocode = DataStore.getGeocodeForGuid(cacheid); } @@ -227,13 +226,7 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia setTitle(res.getString(R.string.log_new_log) + ": " + cache.getGeocode()); } - // Get ids for later use - tweetBox = (LinearLayout) findViewById(R.id.tweet_box); - tweetCheck = (CheckBox) findViewById(R.id.tweet); - logPasswordBox = (LinearLayout) findViewById(R.id.log_password_box); - - final RatingBar ratingBar = (RatingBar) findViewById(R.id.gcvoteRating); - initializeRatingBar(ratingBar); + initializeRatingBar(); // initialize with default values setDefaultValues(); @@ -261,7 +254,7 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia } enablePostButton(false); - final Button typeButton = (Button) findViewById(R.id.type); + final Button typeButton = ButterKnife.findById(this, R.id.type); typeButton.setText(typeSelected.getL10n()); typeButton.setOnClickListener(new View.OnClickListener() { @@ -271,11 +264,11 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia } }); - final Button dateButton = (Button) findViewById(R.id.date); + final Button dateButton = ButterKnife.findById(this, R.id.date); setDate(date); dateButton.setOnClickListener(new DateListener()); - final EditText logView = (EditText) findViewById(R.id.log); + final EditText logView = ButterKnife.findById(this, R.id.log); if (StringUtils.isBlank(currentLogText()) && StringUtils.isNotBlank(text)) { logView.setText(text); Dialogs.moveCursorToEnd(logView); @@ -288,27 +281,19 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia loggingManager = cache.getLoggingManager(this); loggingManager.init(); + requestKeyboardForLogging(); } - private void initializeRatingBar(final RatingBar ratingBar) { - final TextView label = (TextView) findViewById(R.id.gcvoteLabel); + private void initializeRatingBar() { if (GCVote.isVotingPossible(cache)) { - ratingBar.setVisibility(View.VISIBLE); - label.setVisibility(View.VISIBLE); - } - ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() { + GCVoteRatingBarUtil.initializeRatingBar(cache, getWindow().getDecorView().getRootView(), new OnRatingChangeListener() { - @Override - public void onRatingChanged(final RatingBar ratingBar, final float stars, final boolean fromUser) { - // 0.5 is not a valid rating, therefore we must limit - rating = GCVote.isValidRating(stars) ? stars : 0; - if (rating < stars) { - ratingBar.setRating(rating); + @Override + public void onRatingChanged(final float stars) { + rating = stars; } - label.setText(GCVote.getDescription(rating)); - } - }); - ratingBar.setRating(cache.getMyVote()); + }); + } } private void setDefaultValues() { @@ -316,7 +301,7 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia rating = GCVote.NO_RATING; typeSelected = cache.getDefaultLogType(); // it this is an attended event log, use the event date by default instead of the current date - if (cache.isEventCache() && DateUtils.isPastEvent(cache) && typeSelected == LogType.ATTENDED) { + if (cache.isEventCache() && CalendarUtils.isPastEvent(cache) && typeSelected == LogType.ATTENDED) { date.setTime(cache.getHiddenDate()); } text = null; @@ -333,9 +318,9 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia setType(typeSelected); setDate(date); - final EditText logView = (EditText) findViewById(R.id.log); + final EditText logView = ButterKnife.findById(this, R.id.log); logView.setText(StringUtils.EMPTY); - final EditText logPasswordView = (EditText) findViewById(R.id.log_password); + final EditText logPasswordView = ButterKnife.findById(this, R.id.log_password); logPasswordView.setText(StringUtils.EMPTY); showToast(res.getString(R.string.info_log_cleared)); @@ -368,12 +353,12 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia public void setDate(final Calendar dateIn) { date = dateIn; - final Button dateButton = (Button) findViewById(R.id.date); + final Button dateButton = ButterKnife.findById(this, R.id.date); dateButton.setText(Formatter.formatShortDateVerbally(date.getTime().getTime())); } public void setType(final LogType type) { - final Button typeButton = (Button) findViewById(R.id.type); + final Button typeButton = ButterKnife.findById(this, R.id.type); typeSelected = type; typeButton.setText(typeSelected.getL10n()); @@ -419,13 +404,13 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia final String log = logTexts[0]; final String logPwd = logTexts.length > 1 ? logTexts[1] : null; try { - final LogResult logResult = loggingManager.postLog(cache, typeSelected, date, log, logPwd, trackables); + final LogResult logResult = loggingManager.postLog(typeSelected, date, log, logPwd, trackables); if (logResult.getPostLogResult() == StatusCode.NO_ERROR) { // update geocache in DB - if (typeSelected == LogType.FOUND_IT || typeSelected == LogType.ATTENDED || typeSelected == LogType.WEBCAM_PHOTO_TAKEN) { + if (typeSelected.isFoundLog()) { cache.setFound(true); - cache.setVisitedDate(new Date().getTime()); + cache.setVisitedDate(date.getTimeInMillis()); } DataStore.saveChangedCache(cache); @@ -434,7 +419,7 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia final LogEntry logNow = new LogEntry(date.getTimeInMillis(), typeSelected, log); logNow.friend = true; newLogs.add(0, logNow); - DataStore.saveLogsWithoutTransaction(cache.getGeocode(), newLogs); + DataStore.saveLogs(cache.getGeocode(), newLogs); // update offline log in DB cache.clearOfflineLog(); @@ -444,8 +429,13 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia Twitter.postTweetCache(geocode, logNow); } } - if (GCVote.isValidRating(rating)) { - GCVote.setRating(cache, rating); + if (GCVote.isValidRating(rating) && GCVote.isVotingPossible(cache)) { + if (GCVote.setRating(cache, rating)) { + cache.setMyVote(rating); + DataStore.saveChangedCache(cache); + } else { + showToast(res.getString(R.string.err_gcvote_send_rating)); + } } if (StringUtils.isNotBlank(imageUri.getPath())) { @@ -453,7 +443,7 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia final String uploadedImageUrl = imageResult.getImageUri(); if (StringUtils.isNotEmpty(uploadedImageUrl)) { logNow.addLogImage(new Image(uploadedImageUrl, imageCaption, imageDescription)); - DataStore.saveLogsWithoutTransaction(cache.getGeocode(), newLogs); + DataStore.saveLogs(cache.getGeocode(), newLogs); } return imageResult.getPostResult(); } @@ -461,7 +451,7 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia return logResult.getPostLogResult(); } catch (final RuntimeException e) { - Log.e("VisitCacheActivity.Poster.doInBackgroundInternal", e); + Log.e("LogCacheActivity.Poster.doInBackgroundInternal", e); } return StatusCode.LOG_POST_ERROR; @@ -491,16 +481,17 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia // again will be easy using "Clear" while retyping the text may not be. if (force || (StringUtils.isNotEmpty(log) && !StringUtils.equals(log, text))) { cache.logOffline(this, log, date, typeSelected); + Settings.setLastCacheLog(log); } text = log; } private String currentLogText() { - return ((EditText) findViewById(R.id.log)).getText().toString(); + return ButterKnife.<EditText>findById(this, R.id.log).getText().toString(); } private String currentLogPassword() { - return ((EditText) findViewById(R.id.log_password)).getText().toString(); + return ButterKnife.<EditText>findById(this, R.id.log_password).getText().toString(); } @Override @@ -578,9 +569,9 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia private void selectImage() { final Intent selectImageIntent = new Intent(this, ImageSelectActivity.class); - selectImageIntent.putExtra(ImageSelectActivity.EXTRAS_CAPTION, imageCaption); - selectImageIntent.putExtra(ImageSelectActivity.EXTRAS_DESCRIPTION, imageDescription); - selectImageIntent.putExtra(ImageSelectActivity.EXTRAS_URI_AS_STRING, imageUri.toString()); + selectImageIntent.putExtra(Intents.EXTRA_CAPTION, imageCaption); + selectImageIntent.putExtra(Intents.EXTRA_DESCRIPTION, imageDescription); + selectImageIntent.putExtra(Intents.EXTRA_URI_AS_STRING, imageUri.toString()); startActivityForResult(selectImageIntent, SELECT_IMAGE); } @@ -589,9 +580,9 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) { if (requestCode == SELECT_IMAGE) { if (resultCode == RESULT_OK) { - imageCaption = data.getStringExtra(ImageSelectActivity.EXTRAS_CAPTION); - imageDescription = data.getStringExtra(ImageSelectActivity.EXTRAS_DESCRIPTION); - imageUri = Uri.parse(data.getStringExtra(ImageSelectActivity.EXTRAS_URI_AS_STRING)); + imageCaption = data.getStringExtra(Intents.EXTRA_CAPTION); + imageDescription = data.getStringExtra(Intents.EXTRA_DESCRIPTION); + imageUri = Uri.parse(data.getStringExtra(Intents.EXTRA_URI_AS_STRING)); } else if (resultCode != RESULT_CANCELED) { // Image capture failed, advise user showToast(getResources().getString(R.string.err_select_logimage_failed)); @@ -603,7 +594,7 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia public boolean onOptionsItemSelected(final MenuItem item) { switch (item.getItemId()) { case R.id.menu_send: - sendLog(); + sendLogAndConfirm(); return true; case R.id.menu_image: selectImage(); @@ -622,18 +613,37 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia return super.onOptionsItemSelected(item); } - private void sendLog() { + private void sendLogAndConfirm() { if (!sendButtonEnabled) { Dialogs.message(this, R.string.log_post_not_possible); + return; + } + if (CalendarUtils.isFuture(date)) { + Dialogs.message(this, R.string.log_date_future_not_allowed); + return; + } + if (typeSelected.mustConfirmLog()) { + Dialogs.confirm(this, R.string.confirm_log_title, res.getString(R.string.confirm_log_message, typeSelected.getL10n()), new OnClickListener() { + + @Override + public void onClick(final DialogInterface dialog, final int which) { + sendLogInternal(); + } + }); } else { - final String message = res.getString(StringUtils.isBlank(imageUri.getPath()) ? - R.string.log_saving : - R.string.log_saving_and_uploading); - new Poster(this, message).execute(currentLogText(), currentLogPassword()); + sendLogInternal(); } } + private void sendLogInternal() { + final String message = res.getString(StringUtils.isBlank(imageUri.getPath()) ? + R.string.log_saving : + R.string.log_saving_and_uploading); + new Poster(this, message).execute(currentLogText(), currentLogPassword()); + Settings.setLastCacheLog(currentLogText()); + } + @Override public boolean onCreateOptionsMenu(final Menu menu) { super.onCreateOptionsMenu(menu); @@ -650,4 +660,16 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia .setTarget(new ActionItemTarget(this, R.id.menu_send)) .setContent(R.string.showcase_logcache_title, R.string.showcase_logcache_text); } + + public static Intent getLogCacheIntent(final Activity context, final String cacheId, final String geocode) { + final Intent logVisitIntent = new Intent(context, LogCacheActivity.class); + logVisitIntent.putExtra(Intents.EXTRA_ID, cacheId); + logVisitIntent.putExtra(Intents.EXTRA_GEOCODE, geocode); + return logVisitIntent; + } + + @Override + protected String getLastLog() { + return Settings.getLastCacheLog(); + } } |