mirror of
https://github.com/viq/NewsBlur.git
synced 2025-09-18 21:43:31 +00:00
Merge pull request #484 from manderson23/master
Android: Format dates on client
This commit is contained in:
commit
f0a09abcff
10 changed files with 146 additions and 11 deletions
|
@ -61,7 +61,7 @@ public class AllSharedStoriesItemListFragment extends ItemListFragment implement
|
|||
|
||||
contentResolver = getActivity().getContentResolver();
|
||||
|
||||
String[] groupFrom = new String[] { DatabaseConstants.STORY_TITLE, DatabaseConstants.STORY_AUTHORS, DatabaseConstants.STORY_TITLE, DatabaseConstants.STORY_SHORTDATE, DatabaseConstants.STORY_INTELLIGENCE_AUTHORS, DatabaseConstants.FEED_TITLE };
|
||||
String[] groupFrom = new String[] { DatabaseConstants.STORY_TITLE, DatabaseConstants.STORY_AUTHORS, DatabaseConstants.STORY_TITLE, DatabaseConstants.STORY_DATE, DatabaseConstants.STORY_INTELLIGENCE_AUTHORS, DatabaseConstants.FEED_TITLE };
|
||||
int[] groupTo = new int[] { R.id.row_item_title, R.id.row_item_author, R.id.row_item_title, R.id.row_item_date, R.id.row_item_sidebar, R.id.row_item_feedtitle };
|
||||
// TODO: defer creation of the adapter until the loader's first callback so we don't leak this first ListView cursor
|
||||
Cursor cursor = contentResolver.query(FeedProvider.ALL_SHARED_STORIES_URI, null, DatabaseConstants.getStorySelectionFromState(currentState), null, DatabaseConstants.getStorySortOrder(storyOrder));
|
||||
|
|
|
@ -59,7 +59,7 @@ public class AllStoriesItemListFragment extends StoryItemListFragment implements
|
|||
|
||||
Cursor cursor = getActivity().getContentResolver().query(FeedProvider.ALL_STORIES_URI, null, DatabaseConstants.getStorySelectionFromState(currentState), null, DatabaseConstants.getStorySortOrder(storyOrder));
|
||||
getActivity().startManagingCursor(cursor);
|
||||
String[] groupFrom = new String[] { DatabaseConstants.STORY_TITLE, DatabaseConstants.STORY_AUTHORS, DatabaseConstants.STORY_TITLE, DatabaseConstants.STORY_SHORTDATE, DatabaseConstants.STORY_INTELLIGENCE_AUTHORS, DatabaseConstants.FEED_TITLE };
|
||||
String[] groupFrom = new String[] { DatabaseConstants.STORY_TITLE, DatabaseConstants.STORY_AUTHORS, DatabaseConstants.STORY_TITLE, DatabaseConstants.STORY_DATE, DatabaseConstants.STORY_INTELLIGENCE_AUTHORS, DatabaseConstants.FEED_TITLE };
|
||||
int[] groupTo = new int[] { R.id.row_item_title, R.id.row_item_author, R.id.row_item_title, R.id.row_item_date, R.id.row_item_sidebar, R.id.row_item_feedtitle };
|
||||
|
||||
adapter = new MultipleFeedItemsAdapter(getActivity(), R.layout.row_socialitem, cursor, groupFrom, groupTo, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
|
||||
|
|
|
@ -88,7 +88,7 @@ public class FeedItemListFragment extends StoryItemListFragment implements Loade
|
|||
Feed feed = Feed.fromCursor(feedCursor);
|
||||
feedCursor.close();
|
||||
|
||||
String[] groupFrom = new String[] { DatabaseConstants.STORY_TITLE, DatabaseConstants.STORY_AUTHORS, DatabaseConstants.STORY_READ, DatabaseConstants.STORY_SHORTDATE, DatabaseConstants.STORY_INTELLIGENCE_AUTHORS };
|
||||
String[] groupFrom = new String[] { DatabaseConstants.STORY_TITLE, DatabaseConstants.STORY_AUTHORS, DatabaseConstants.STORY_READ, DatabaseConstants.STORY_DATE, DatabaseConstants.STORY_INTELLIGENCE_AUTHORS };
|
||||
int[] groupTo = new int[] { R.id.row_item_title, R.id.row_item_author, R.id.row_item_title, R.id.row_item_date, R.id.row_item_sidebar };
|
||||
|
||||
// create the adapter before starting the loader, since the callback updates the adapter
|
||||
|
|
|
@ -83,7 +83,7 @@ public class FolderItemListFragment extends StoryItemListFragment implements Loa
|
|||
Cursor cursor = contentResolver.query(FeedProvider.MULTIFEED_STORIES_URI, null, DatabaseConstants.getStorySelectionFromState(currentState), feedIds, DatabaseConstants.getStorySortOrder(storyOrder));
|
||||
getActivity().startManagingCursor(cursor);
|
||||
|
||||
String[] groupFrom = new String[] { DatabaseConstants.STORY_TITLE, DatabaseConstants.FEED_TITLE, DatabaseConstants.STORY_READ, DatabaseConstants.STORY_SHORTDATE, DatabaseConstants.STORY_INTELLIGENCE_AUTHORS, DatabaseConstants.STORY_AUTHORS };
|
||||
String[] groupFrom = new String[] { DatabaseConstants.STORY_TITLE, DatabaseConstants.FEED_TITLE, DatabaseConstants.STORY_READ, DatabaseConstants.STORY_DATE, DatabaseConstants.STORY_INTELLIGENCE_AUTHORS, DatabaseConstants.STORY_AUTHORS };
|
||||
int[] groupTo = new int[] { R.id.row_item_title, R.id.row_item_feedtitle, R.id.row_item_title, R.id.row_item_date, R.id.row_item_sidebar, R.id.row_item_author };
|
||||
|
||||
getLoaderManager().initLoader(ITEMLIST_LOADER , null, this);
|
||||
|
|
|
@ -15,7 +15,6 @@ import android.support.v4.app.DialogFragment;
|
|||
import android.support.v4.app.Fragment;
|
||||
import android.text.Html;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
|
@ -34,11 +33,11 @@ import com.newsblur.domain.UserDetails;
|
|||
import com.newsblur.network.APIManager;
|
||||
import com.newsblur.network.SetupCommentSectionTask;
|
||||
import com.newsblur.network.domain.StoryTextResponse;
|
||||
import com.newsblur.util.AppConstants;
|
||||
import com.newsblur.util.DefaultFeedView;
|
||||
import com.newsblur.util.FeedUtils;
|
||||
import com.newsblur.util.ImageLoader;
|
||||
import com.newsblur.util.PrefsUtils;
|
||||
import com.newsblur.util.StoryUtils;
|
||||
import com.newsblur.util.UIUtils;
|
||||
import com.newsblur.util.ViewUtils;
|
||||
import com.newsblur.view.FlowLayout;
|
||||
|
@ -309,7 +308,7 @@ public class ReadingItemFragment extends Fragment implements ClassifierDialogFra
|
|||
}
|
||||
|
||||
itemTitle.setText(Html.fromHtml(story.title));
|
||||
itemDate.setText(story.longDate);
|
||||
itemDate.setText(StoryUtils.formatLongDate(story.date));
|
||||
|
||||
if (!TextUtils.isEmpty(story.authors)) {
|
||||
itemAuthors.setText("• " + story.authors);
|
||||
|
|
|
@ -52,7 +52,7 @@ public class SavedStoriesItemListFragment extends ItemListFragment implements Lo
|
|||
contentResolver = getActivity().getContentResolver();
|
||||
Cursor cursor = contentResolver.query(FeedProvider.STARRED_STORIES_URI, null, null, null, DatabaseConstants.STARRED_STORY_ORDER);
|
||||
|
||||
String[] groupFrom = new String[] { DatabaseConstants.STORY_TITLE, DatabaseConstants.STORY_AUTHORS, DatabaseConstants.STORY_TITLE, DatabaseConstants.STORY_SHORTDATE, DatabaseConstants.STORY_INTELLIGENCE_AUTHORS, DatabaseConstants.FEED_TITLE };
|
||||
String[] groupFrom = new String[] { DatabaseConstants.STORY_TITLE, DatabaseConstants.STORY_AUTHORS, DatabaseConstants.STORY_TITLE, DatabaseConstants.STORY_DATE, DatabaseConstants.STORY_INTELLIGENCE_AUTHORS, DatabaseConstants.FEED_TITLE };
|
||||
int[] groupTo = new int[] { R.id.row_item_title, R.id.row_item_author, R.id.row_item_title, R.id.row_item_date, R.id.row_item_sidebar, R.id.row_item_feedtitle };
|
||||
|
||||
getLoaderManager().initLoader(ITEMLIST_LOADER , null, this);
|
||||
|
|
|
@ -60,7 +60,7 @@ public class SocialFeedItemListFragment extends ItemListFragment implements Load
|
|||
Uri uri = FeedProvider.SOCIALFEED_STORIES_URI.buildUpon().appendPath(userId).build();
|
||||
Cursor cursor = getActivity().getContentResolver().query(uri, null, DatabaseConstants.getStorySelectionFromState(currentState), null, DatabaseConstants.getStorySharedSortOrder(storyOrder));
|
||||
|
||||
groupFroms = new String[] { DatabaseConstants.FEED_FAVICON_URL, DatabaseConstants.FEED_TITLE, DatabaseConstants.STORY_TITLE, DatabaseConstants.STORY_SHORTDATE, DatabaseConstants.STORY_AUTHORS, DatabaseConstants.STORY_INTELLIGENCE_AUTHORS};
|
||||
groupFroms = new String[] { DatabaseConstants.FEED_FAVICON_URL, DatabaseConstants.FEED_TITLE, DatabaseConstants.STORY_TITLE, DatabaseConstants.STORY_DATE, DatabaseConstants.STORY_AUTHORS, DatabaseConstants.STORY_INTELLIGENCE_AUTHORS};
|
||||
groupTos = new int[] { R.id.row_item_feedicon, R.id.row_item_feedtitle, R.id.row_item_title, R.id.row_item_date, R.id.row_item_author, R.id.row_item_sidebar};
|
||||
|
||||
adapter = new MultipleFeedItemsAdapter(getActivity(), R.layout.row_socialitem, cursor, groupFroms, groupTos, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
|
||||
|
|
124
clients/android/NewsBlur/src/com/newsblur/util/StoryUtils.java
Normal file
124
clients/android/NewsBlur/src/com/newsblur/util/StoryUtils.java
Normal file
|
@ -0,0 +1,124 @@
|
|||
package com.newsblur.util;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Created by mark on 04/02/2014.
|
||||
*/
|
||||
public class StoryUtils {
|
||||
|
||||
private static final ThreadLocal<SimpleDateFormat> todayLongFormat = new ThreadLocal<SimpleDateFormat>() {
|
||||
@Override
|
||||
protected SimpleDateFormat initialValue() {
|
||||
return new SimpleDateFormat("MMMM d");
|
||||
}
|
||||
};
|
||||
|
||||
private static final ThreadLocal<SimpleDateFormat> monthLongFormat = new ThreadLocal<SimpleDateFormat>() {
|
||||
@Override
|
||||
protected SimpleDateFormat initialValue() {
|
||||
return new SimpleDateFormat("EEEE, MMMM d");
|
||||
}
|
||||
};
|
||||
|
||||
private static final ThreadLocal<SimpleDateFormat> yearLongFormat = new ThreadLocal<SimpleDateFormat>() {
|
||||
@Override
|
||||
protected SimpleDateFormat initialValue() {
|
||||
return new SimpleDateFormat("yyyy");
|
||||
}
|
||||
};
|
||||
|
||||
private static final ThreadLocal<SimpleDateFormat> twelveHourFormat = new ThreadLocal<SimpleDateFormat>() {
|
||||
@Override
|
||||
protected SimpleDateFormat initialValue() {
|
||||
return new SimpleDateFormat("h:mma");
|
||||
}
|
||||
};
|
||||
|
||||
private static final ThreadLocal<SimpleDateFormat> shortDateFormat = new ThreadLocal<SimpleDateFormat>() {
|
||||
@Override
|
||||
protected SimpleDateFormat initialValue() {
|
||||
return new SimpleDateFormat("d MMM yyyy");
|
||||
}
|
||||
};
|
||||
|
||||
public static String formatLongDate(Date storyDate) {
|
||||
|
||||
Date midnightToday = midnightToday();
|
||||
Date midnightYesterday = midnightYesterday();
|
||||
Date beginningOfMonth = beginningOfMonth();
|
||||
|
||||
Calendar storyCalendar = Calendar.getInstance();
|
||||
storyCalendar.setTime(storyDate);
|
||||
int month = storyCalendar.get(Calendar.DAY_OF_MONTH);
|
||||
|
||||
if (storyDate.getTime() > midnightToday.getTime()) {
|
||||
// Today, January 1st 00:00
|
||||
return "Today, " + todayLongFormat.get().format(storyDate) + getDayOfMonthSuffix(month) + " " + twelveHourFormat.get().format(storyDate);
|
||||
} else if (storyDate.getTime() > midnightYesterday.getTime()) {
|
||||
// Yesterday, January 1st 00:00
|
||||
return "Yesterday, " + todayLongFormat.get().format(storyDate) + getDayOfMonthSuffix(month) + " " + twelveHourFormat.get().format(storyDate);
|
||||
} else if (storyDate.getTime() > beginningOfMonth.getTime()) {
|
||||
// Monday, January 1st 00:00
|
||||
return monthLongFormat.get().format(storyDate) + getDayOfMonthSuffix(month) + " " + twelveHourFormat.get().format(storyDate);
|
||||
} else {
|
||||
// Monday, January 1st 2014 00:00
|
||||
return monthLongFormat.get().format(storyDate) + getDayOfMonthSuffix(month) + " " + yearLongFormat.get().format(storyDate) + " " + twelveHourFormat.get().format(storyDate);
|
||||
}
|
||||
}
|
||||
|
||||
private static Date midnightToday() {
|
||||
Calendar midnight = Calendar.getInstance();
|
||||
midnight.set(Calendar.HOUR_OF_DAY, 0);
|
||||
midnight.set(Calendar.MINUTE, 0);
|
||||
midnight.set(Calendar.SECOND, 0);
|
||||
return midnight.getTime();
|
||||
}
|
||||
|
||||
private static Date midnightYesterday() {
|
||||
return new Date(midnightToday().getTime() - (24 * 60 * 60* 1000));
|
||||
}
|
||||
|
||||
private static Date beginningOfMonth() {
|
||||
Calendar month = Calendar.getInstance();
|
||||
month.set(Calendar.HOUR_OF_DAY, 0);
|
||||
month.set(Calendar.MINUTE, 0);
|
||||
month.set(Calendar.SECOND, 0);
|
||||
month.set(Calendar.DAY_OF_MONTH, 1);
|
||||
return month.getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* From http://stackoverflow.com/questions/4011075/how-do-you-format-the-day-of-the-month-to-say-11th-21st-or-23rd-in-java
|
||||
*/
|
||||
private static String getDayOfMonthSuffix(final int n) {
|
||||
if (n >= 11 && n <= 13) {
|
||||
return "th";
|
||||
}
|
||||
switch (n % 10) {
|
||||
case 1: return "st";
|
||||
case 2: return "nd";
|
||||
case 3: return "rd";
|
||||
default: return "th";
|
||||
}
|
||||
}
|
||||
|
||||
public static String formatShortDate(Date storyDate) {
|
||||
|
||||
Date midnightToday = midnightToday();
|
||||
Date midnightYesterday = midnightYesterday();
|
||||
|
||||
if (storyDate.getTime() > midnightToday.getTime()) {
|
||||
// 00:00
|
||||
return twelveHourFormat.get().format(storyDate);
|
||||
} else if (storyDate.getTime() > midnightYesterday.getTime()) {
|
||||
// Yesterday, 00:00
|
||||
return "Yesterday, " + twelveHourFormat.get().format(storyDate);
|
||||
} else {
|
||||
// 1 Jan 2014, 00:00
|
||||
return shortDateFormat.get().format(storyDate) +", " + twelveHourFormat.get().format(storyDate);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -12,6 +12,9 @@ import android.widget.TextView;
|
|||
import com.newsblur.R;
|
||||
import com.newsblur.database.DatabaseConstants;
|
||||
import com.newsblur.domain.Story;
|
||||
import com.newsblur.util.StoryUtils;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class FeedItemViewBinder implements ViewBinder {
|
||||
|
||||
|
@ -68,7 +71,10 @@ public class FeedItemViewBinder implements ViewBinder {
|
|||
} else if (TextUtils.equals(columnName, DatabaseConstants.STORY_TITLE)) {
|
||||
((TextView) view).setText(Html.fromHtml(cursor.getString(columnIndex)));
|
||||
return true;
|
||||
}
|
||||
} else if (TextUtils.equals(columnName, DatabaseConstants.STORY_DATE)) {
|
||||
((TextView) view).setText(StoryUtils.formatShortDate(new Date(cursor.getLong(columnIndex))));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -15,6 +15,9 @@ import com.newsblur.activity.NewsBlurApplication;
|
|||
import com.newsblur.database.DatabaseConstants;
|
||||
import com.newsblur.domain.Story;
|
||||
import com.newsblur.util.ImageLoader;
|
||||
import com.newsblur.util.StoryUtils;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class SocialItemViewBinder implements ViewBinder {
|
||||
|
||||
|
@ -72,7 +75,10 @@ public class SocialItemViewBinder implements ViewBinder {
|
|||
} else if (TextUtils.equals(columnName, DatabaseConstants.STORY_TITLE)) {
|
||||
((TextView) view).setText(Html.fromHtml(cursor.getString(columnIndex)));
|
||||
return true;
|
||||
}
|
||||
} else if (TextUtils.equals(columnName, DatabaseConstants.STORY_DATE)) {
|
||||
((TextView) view).setText(StoryUtils.formatShortDate(new Date(cursor.getLong(columnIndex))));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue