Skip to content

Commit

Permalink
Showing time left when listing games.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Bruse committed Dec 19, 2016
1 parent 6aa49f6 commit ed53d98
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 31 deletions.
29 changes: 29 additions & 0 deletions app/src/main/java/se/oort/diplicity/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.app.Application;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import se.oort.diplicity.apigen.MultiContainer;
import se.oort.diplicity.apigen.SingleContainer;
Expand All @@ -15,5 +17,32 @@ public class App extends Application {
public static User loggedInUser;
public static MultiContainer<VariantService.Variant> variants;

public static String nanosToDuration(long nanos) {
return minutesToDuration((int) ((nanos / (long) 1000000000) / (long) 60));
}

public static String minutesToDuration(int mins) {
long days = mins / (60 * 24);
long hours = (mins - (60 * 24 * days)) / 60;
long minutes = mins - (60 * 24 * days) - (60 * hours);
List<String> timeLabelList = new ArrayList<String>();
if (days > 0) {
timeLabelList.add("" + days + "d");
}
if (hours > 0) {
timeLabelList.add("" + hours + "h");
}
if (minutes > 0) {
timeLabelList.add("" + minutes + "m");
}
StringBuilder timeLabel = new StringBuilder();
for (int i = 0; i < timeLabelList.size(); i++) {
timeLabel.append(timeLabelList.get(i));
if (i < timeLabelList.size() - 1) {
timeLabel.append(", ");
}
}
return timeLabel.toString();
}

}
50 changes: 20 additions & 30 deletions app/src/main/java/se/oort/diplicity/GamesAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -31,7 +27,7 @@ public class ViewHolder extends RecycleAdapter<SingleContainer<Game>, GamesAdapt
TextView desc, variant, deadline, state, rating,
minReliability, minQuickness, maxHated, maxHater,
ratingLabel, minReliabilityLabel, minQuicknessLabel,
maxHatedLabel, maxHaterLabel;
maxHatedLabel, maxHaterLabel, nextDeadline;
MemberTable members;
RelativeLayout expanded;
View.OnClickListener delegateClickListener;
Expand All @@ -42,6 +38,7 @@ public ViewHolder(View view) {
desc = (TextView) view.findViewById(R.id.desc);
variant = (TextView) view.findViewById(R.id.variant);
deadline = (TextView) view.findViewById(R.id.deadline);
nextDeadline = (TextView) view.findViewById(R.id.next_deadline);
state = (TextView) view.findViewById(R.id.state);
rating = (TextView) view.findViewById(R.id.rating);
minReliability = (TextView) view.findViewById(R.id.min_reliability);
Expand All @@ -65,39 +62,42 @@ public void bind(final SingleContainer<Game> game, final int pos) {
desc.setText(game.Properties.Desc);
}
if (game.Properties.MinRating != 0 || game.Properties.MaxRating != 0) {
rating.setText(ctx.getResources().getString(R.string.x_to_y, game.Properties.MinRating, game.Properties.MaxRating));
rating.setText(ctx.getResources().getString(
R.string.x_to_y,
retrofitActivity.toString(game.Properties.MinRating),
retrofitActivity.toString(game.Properties.MaxRating)));
rating.setVisibility(View.VISIBLE);
ratingLabel.setVisibility(View.VISIBLE);
} else {
rating.setVisibility(View.GONE);
ratingLabel.setVisibility(View.GONE);
}
if (game.Properties.MinRating != 0) {
minReliability.setText("" + game.Properties.MinReliability);
minReliability.setText(retrofitActivity.toString(game.Properties.MinReliability));
minReliability.setVisibility(View.VISIBLE);
minReliabilityLabel.setVisibility(View.VISIBLE);
} else {
minReliability.setVisibility(View.GONE);
minReliabilityLabel.setVisibility(View.GONE);
}
if (game.Properties.MinQuickness != 0) {
minQuickness.setText("" + game.Properties.MinQuickness);
minQuickness.setText(retrofitActivity.toString(game.Properties.MinQuickness));
minQuickness.setVisibility(View.VISIBLE);
minQuicknessLabel.setVisibility(View.VISIBLE);
} else {
minQuickness.setVisibility(View.GONE);
minQuicknessLabel.setVisibility(View.GONE);
}
if (game.Properties.MaxHated != 0) {
maxHated.setText("" + game.Properties.MaxHated);
maxHated.setText(retrofitActivity.toString(game.Properties.MaxHated));
maxHated.setVisibility(View.VISIBLE);
maxHatedLabel.setVisibility(View.VISIBLE);
} else {
maxHated.setVisibility(View.GONE);
maxHatedLabel.setVisibility(View.GONE);
}
if (game.Properties.MaxHater != 0) {
maxHater.setText("" + game.Properties.MaxHater);
maxHater.setText(retrofitActivity.toString(game.Properties.MaxHater));
maxHater.setVisibility(View.VISIBLE);
maxHaterLabel.setVisibility(View.VISIBLE);
} else {
Expand All @@ -107,27 +107,17 @@ public void bind(final SingleContainer<Game> game, final int pos) {

variant.setText(game.Properties.Variant);

long days = game.Properties.PhaseLengthMinutes / (60 * 24);
long hours = (game.Properties.PhaseLengthMinutes - (60 * 24 * days)) / 60;
long minutes = game.Properties.PhaseLengthMinutes - (60 * 24 * days) - (60 * hours);
List<String> timeLabelList = new ArrayList<String>();
if (days > 0) {
timeLabelList.add("" + days + "d");
}
if (hours > 0) {
timeLabelList.add("" + hours + "h");
}
if (minutes > 0) {
timeLabelList.add("" + minutes + "m");
}
StringBuffer timeLabel = new StringBuffer();
for (int i = 0; i < timeLabelList.size(); i++) {
timeLabel.append(timeLabelList.get(i));
if (i < timeLabelList.size() - 1) {
timeLabel.append(", ");
}
deadline.setText(App.minutesToDuration(game.Properties.PhaseLengthMinutes.intValue()));

if (
game.Properties.Started &&
game.Properties.NewestPhaseMeta.size() > 0 &&
game.Properties.NewestPhaseMeta.get(0).NextDeadlineIn > 0) {
nextDeadline.setText(App.nanosToDuration(game.Properties.NewestPhaseMeta.get(0).NextDeadlineIn));
nextDeadline.setVisibility(View.VISIBLE);
} else {
nextDeadline.setVisibility(View.GONE);
}
deadline.setText(timeLabel.toString());

if (!game.Properties.Started) {
state.setText(ctx.getResources().getQuantityString(R.plurals.player, game.Properties.NMembers.intValue(), game.Properties.NMembers));
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/se/oort/diplicity/MemberTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void setMembers(final RetrofitActivity retrofitActivity, final List<Membe
tableRow.addView(scs);
TextView points = new TextView(retrofitActivity);
points.setLayoutParams(wrapContentParams);
points.setText(getResources().getString(R.string._points, foundScore.Score.intValue()));
points.setText(getResources().getString(R.string._points, retrofitActivity.toString(foundScore.Score)));
tableRow.addView(points);
}
}
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/se/oort/diplicity/RetrofitActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import okhttp3.HttpUrl;
Expand Down Expand Up @@ -258,6 +259,13 @@ public <T> void handleReq(Observable<T> req, final Sendable<T> handler, final Er
observe(req, handler, newProgressAndToastHandler(onError, progressMessage));
}

public String toString(Double d) {
return String.format(
Locale.getDefault(),
getResources().getString(R.string.float_format),
d);
}

protected void setBaseURL(String baseURL) {
App.baseURL = baseURL;
adapterFactory = new AuthenticatingCallAdapterFactory();
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/se/oort/diplicity/apigen/Phase.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Phase implements java.io.Serializable {
public String Type;
public Boolean Resolved;
public java.util.Date DeadlineAt;
public Long NextDeadlineIn;
public String UnitsJSON;
public String SCsJSON;
public String GameID;
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/se/oort/diplicity/apigen/PhaseMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class PhaseMeta implements java.io.Serializable {
public String Type;
public Boolean Resolved;
public java.util.Date DeadlineAt;
public Long NextDeadlineIn;
public String UnitsJSON;
public String SCsJSON;
}
10 changes: 10 additions & 0 deletions app/src/main/res/layout/game_list_row.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@
android:layout_height="wrap_content"
android:textColor="?android:attr/textColorPrimary" />

<TextView
android:id="@+id/next_deadline"
android:layout_margin="2dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBaseline="@id/variant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?android:attr/textColorPrimary" />

<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/expanded"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,5 @@
<string name="rdy">RDY</string>
<string name="_messages">%1s msgs</string>
<string name="_unread">%1s unread</string>
<string name="float_format">%1$.2f</string>
</resources>

0 comments on commit ed53d98

Please sign in to comment.