make ExpiringDelegate responsible for figuring percentage of time left

given start time and having a constant for how long a turn lasts.  Eventually
the constant will be replaced by a second number passed in.
This commit is contained in:
Eric House 2012-10-01 19:07:23 -07:00
parent cd776a34dd
commit 0a60419802
4 changed files with 57 additions and 33 deletions

View file

@ -31,6 +31,9 @@ import android.view.View;
import junit.framework.Assert;
public class ExpiringDelegate {
// private static final long INTERVAL_SECS = 3 * 24 * 60 * 60;
private static final long INTERVAL_SECS = 60 * 60;
private Drawable m_back = null;
private Context m_context;
private View m_view;
@ -40,13 +43,17 @@ public class ExpiringDelegate {
private Rect m_rect;
private Paint m_paint;
private float[] m_points;
private long m_startSecs;
public ExpiringDelegate( Context context, View view, int pct,
boolean haveTurn, boolean haveTurnLocal )
public ExpiringDelegate( Context context, View view, boolean haveTurn,
boolean haveTurnLocal, long startSecs )
{
m_context = context;
m_view = view;
m_pct = pct;
m_startSecs = startSecs;
figurePct();
if ( !haveTurn ) {
// nothing to do
} else if ( haveTurnLocal ) {
@ -127,38 +134,47 @@ public class ExpiringDelegate {
private void setBackground()
{
if ( null == m_back && -1 != m_pct ) {
mkTurnIndicator( m_pct );
mkTurnIndicator();
}
if ( null != m_back ) {
m_view.setBackgroundDrawable( m_back );
}
}
private void mkTurnIndicator( int pctGone )
private void mkTurnIndicator()
{
DbgUtils.logf( "mkTurnIndicator(%d)", pctGone );
Assert.assertTrue( 0 <= pctGone && pctGone <= 100 );
if ( null == m_back || m_pct != pctGone ) {
m_pct = pctGone;
// long now = System.currentTimeMillis() / 1000;
// long allowed = 60*60; // one hour
// allowed *= 24 * 3; // three days
// long used = now - lastTurn;
// if ( used < allowed ) {
// long asLong = (100 * used) / allowed;
// Assert.assertTrue( asLong <= 100 && asLong >= 0 );
// pctGone = (int)asLong;
// }
Bitmap bm = Bitmap.createBitmap( 100, 2, Bitmap.Config.ARGB_8888 );
Assert.assertTrue( 0 <= m_pct && m_pct <= 100 );
if ( null == m_back ) {
Bitmap bm = Bitmap.createBitmap( 100, 1, Bitmap.Config.ARGB_8888 );
Canvas canvas = new Canvas(bm);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor( Color.RED );
canvas.drawRect( 0, 0, pctGone, 2, paint );
canvas.drawRect( 0, 0, m_pct, 1, paint );
paint.setColor( Utils.TURN_COLOR );
canvas.drawRect( pctGone, 0, 100, 2, paint );
canvas.drawRect( m_pct, 0, 100, 1, paint );
m_back = new BitmapDrawable( m_context.getResources(), bm );
}
}
private void figurePct()
{
if ( 0 == m_startSecs ) {
m_pct = 0;
} else {
long now = Utils.getCurSeconds();
long passed = now - m_startSecs;
m_pct = (int)((100 * passed) / INTERVAL_SECS);
if ( m_pct > 100 ) {
m_pct = 100;
} else {
long onePct = INTERVAL_SECS / 100;
long lastStart = m_startSecs + (onePct * m_pct);
Assert.assertTrue( lastStart <= now );
long nextStartIn = lastStart + onePct - now;
DbgUtils.logf( "pct change %d seconds from now", nextStartIn );
}
}
}
}

View file

@ -33,10 +33,10 @@ public class ExpiringLinearLayout extends LinearLayout {
m_context = context;
}
public void setPct( int pct, boolean haveTurn, boolean haveTurnLocal )
public void setPct( boolean haveTurn, boolean haveTurnLocal, long startSecs )
{
m_delegate = new ExpiringDelegate( m_context, this, pct, haveTurn,
haveTurnLocal );
m_delegate = new ExpiringDelegate( m_context, this, haveTurn,
haveTurnLocal, startSecs );
}
@Override

View file

@ -34,10 +34,10 @@ class ExpiringTextView extends TextView {
m_context = context;
}
public void setPct( int pct, boolean haveTurn, boolean haveTurnLocal )
public void setPct( boolean haveTurn, boolean haveTurnLocal, long startSecs )
{
m_delegate = new ExpiringDelegate( m_context, this, pct, haveTurn,
haveTurnLocal );
m_delegate = new ExpiringDelegate( m_context, this, haveTurn,
haveTurnLocal, startSecs );
}
@Override

View file

@ -63,18 +63,22 @@ public class GameListAdapter extends XWListAdapter {
private ExpiringTextView m_name;
private boolean m_expanded, m_haveTurn, m_haveTurnLocal;
private long m_rowid;
private long m_lastMoveTime;
private ImageButton m_expandButton;
public ViewInfo( View view, long rowid )
{
m_view = view;
m_rowid = rowid;
m_lastMoveTime = 0;
}
public ViewInfo( View view, long rowid, boolean expanded,
boolean haveTurn, boolean haveTurnLocal ) {
long lastMoveTime, boolean haveTurn,
boolean haveTurnLocal ) {
this( view, rowid );
m_expanded = expanded;
m_lastMoveTime = lastMoveTime;
m_haveTurn = haveTurn;
m_haveTurnLocal = haveTurnLocal;
m_hideable = (LinearLayout)view.findViewById( R.id.hideable );
@ -92,7 +96,8 @@ public class GameListAdapter extends XWListAdapter {
m_hideable.setVisibility( m_expanded? View.VISIBLE : View.GONE );
m_name.setBackgroundColor( android.R.color.transparent );
m_name.setPct( 75, m_haveTurn && !m_expanded, m_haveTurnLocal );
m_name.setPct( m_haveTurn && !m_expanded, m_haveTurnLocal,
m_lastMoveTime );
}
public void onClick( View view ) {
@ -202,14 +207,16 @@ public class GameListAdapter extends XWListAdapter {
haveTurnLocal = true;
}
}
tmp.setPct( 20 * (ii + 1), haveTurn, haveTurnLocal );
tmp.setPct( haveTurn, haveTurnLocal, summary.lastMoveTime );
list.addView( tmp, ii );
}
view = (TextView)layout.findViewById( R.id.state );
view.setText( state );
view = (TextView)layout.findViewById( R.id.modtime );
view.setText( m_df.format( new Date( summary.modtime ) ) );
long lastMoveTime = summary.lastMoveTime;
lastMoveTime *= 1000;
view.setText( m_df.format( new Date( lastMoveTime ) ) );
int iconID;
ImageView marker =
@ -235,8 +242,9 @@ public class GameListAdapter extends XWListAdapter {
}
boolean expanded = DBUtils.getExpanded( m_context, m_rowid );
ViewInfo vi = new ViewInfo( layout, m_rowid,
expanded, haveTurn, haveTurnLocal );
ViewInfo vi = new ViewInfo( layout, m_rowid, expanded,
summary.lastMoveTime, haveTurn,
haveTurnLocal );
synchronized( m_viewsCache ) {
m_viewsCache.put( m_rowid, vi );