use of se.oort.diplicity.apigen.UserStats in project android-diplicity by zond.
the class UserView method getAvatarClickListener.
public static OnClickListener getAvatarClickListener(final RetrofitActivity retrofitActivity, final Game game, final Member member, final User user) {
if (game == null) {
return getAvatarClickListener(retrofitActivity, user);
}
Member me = retrofitActivity.getLoggedInMember(game);
if (me == null || me.Nation.equals(member.Nation)) {
return getAvatarClickListener(retrofitActivity, user);
}
final Member finalMe = me;
return new OnClickListener() {
@Override
public void onClick(View v) {
retrofitActivity.handleReq(JoinObservable.when(JoinObservable.from(retrofitActivity.userStatsService.UserStatsLoad(user.Id)).and(retrofitActivity.gameStateService.GameStateLoad(game.ID, finalMe.Nation)).and(retrofitActivity.banService.BanLoad(retrofitActivity.getLoggedInUser().Id, user.Id).onErrorReturn(new Func1<Throwable, SingleContainer<Ban>>() {
@Override
public SingleContainer<Ban> call(Throwable throwable) {
if (throwable instanceof HttpException) {
HttpException he = (HttpException) throwable;
if (he.code() == 404) {
return null;
}
}
throw new RuntimeException(throwable);
}
})).then(new Func3<SingleContainer<UserStats>, SingleContainer<GameState>, SingleContainer<Ban>, Object>() {
@Override
public Object call(final SingleContainer<UserStats> userStatsSingleContainer, final SingleContainer<GameState> gameStateSingleContainer, final SingleContainer<Ban> banSingleContainer) {
AlertDialog dialog = new AlertDialog.Builder(retrofitActivity).setView(R.layout.user_dialog).show();
setupUserDialog(retrofitActivity, dialog, userStatsSingleContainer, banSingleContainer);
final CheckBox mutedCheckBox = (CheckBox) dialog.findViewById(R.id.muted);
mutedCheckBox.setVisibility(VISIBLE);
mutedCheckBox.setChecked(gameStateSingleContainer.Properties.Muted != null && gameStateSingleContainer.Properties.Muted.contains(member.Nation));
mutedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked && (gameStateSingleContainer.Properties.Muted == null || !gameStateSingleContainer.Properties.Muted.contains(member.Nation))) {
if (gameStateSingleContainer.Properties.Muted == null) {
gameStateSingleContainer.Properties.Muted = new ArrayList<String>();
}
gameStateSingleContainer.Properties.Muted.add(member.Nation);
} else if (!isChecked && gameStateSingleContainer.Properties.Muted != null && gameStateSingleContainer.Properties.Muted.contains(member.Nation)) {
gameStateSingleContainer.Properties.Muted.remove(member.Nation);
}
retrofitActivity.handleReq(retrofitActivity.gameStateService.GameStateUpdate(gameStateSingleContainer.Properties, game.ID, finalMe.Nation), new Sendable<SingleContainer<GameState>>() {
@Override
public void send(SingleContainer<GameState> gameStateSingleContainer) {
}
}, retrofitActivity.getResources().getString(R.string.updating));
}
});
return null;
}
})).toObservable(), new Sendable<Object>() {
@Override
public void send(Object o) {
}
}, retrofitActivity.getResources().getString(R.string.loading_user_stats));
}
};
}
use of se.oort.diplicity.apigen.UserStats in project android-diplicity by zond.
the class MainActivity method onPostCreate.
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
addGameButton = (FloatingActionButton) findViewById(R.id.add_game_button);
addGameButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
handleReq(userStatsService.UserStatsLoad(getLoggedInUser().Id), new Sendable<SingleContainer<UserStats>>() {
@Override
public void send(SingleContainer<UserStats> userStatsSingleContainer) {
final List<UserStats> statsContainer = new ArrayList<>();
statsContainer.add(userStatsSingleContainer.Properties);
final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setView(R.layout.create_game_dialog).show();
final Returner<Game, Boolean> validateGame = new Returner<Game, Boolean>() {
@Override
public Boolean Return(Game g) {
if (g.PhaseLengthMinutes == null)
g.PhaseLengthMinutes = DAY_IN_MINUTES;
if (g.PhaseLengthMinutes > 30 * DAY_IN_MINUTES) {
Toast.makeText(MainActivity.this, R.string.phase_length_must_be_less_than_30_days, Toast.LENGTH_LONG).show();
return false;
}
UserStats us = statsContainer.get(0);
if (g.MinRating == null)
g.MinRating = 0.0;
if (g.MinRating != 0.0 && g.MinRating > us.Glicko.PracticalRating) {
Toast.makeText(MainActivity.this, getResources().getString(R.string.minimum_rating_must_be_below_your_rating_x, us.Glicko.PracticalRating), Toast.LENGTH_LONG).show();
return false;
}
if (g.MaxRating == null)
g.MaxRating = 0.0;
if (g.MaxRating != 0.0 && g.MaxRating < us.Glicko.PracticalRating) {
Toast.makeText(MainActivity.this, getResources().getString(R.string.maximum_rating_must_be_above_your_rating_x, us.Glicko.PracticalRating), Toast.LENGTH_LONG).show();
return false;
}
if (g.MinReliability == null)
g.MinReliability = 0.0;
if (g.MinReliability != 0.0 && g.MinReliability > us.Reliability) {
Toast.makeText(MainActivity.this, getResources().getString(R.string.minimum_reliability_must_be_below_your_reliability_x, us.Reliability), Toast.LENGTH_LONG).show();
return false;
}
if (g.MinQuickness == null)
g.MinQuickness = 0.0;
if (g.MinQuickness != 0.0 && g.MinQuickness > us.Quickness) {
Toast.makeText(MainActivity.this, getResources().getString(R.string.minimum_quickness_must_be_below_your_quickness_x, us.Quickness), Toast.LENGTH_LONG).show();
return false;
}
if (g.MaxHated == null)
g.MaxHated = 0.0;
if (g.MaxHated != 0.0 && g.MaxHated < us.Hated) {
Toast.makeText(MainActivity.this, getResources().getString(R.string.maximum_hated_must_be_above_your_hated_x, us.Hated), Toast.LENGTH_LONG).show();
return false;
}
if (g.MaxHater == null)
g.MaxHater = 0.0;
if (g.MaxHater != 0.0 && g.MaxHater < us.Hater) {
Toast.makeText(MainActivity.this, getResources().getString(R.string.maximum_hater_must_be_above_your_hater_x, us.Hater), Toast.LENGTH_LONG).show();
return false;
}
return true;
}
};
final Spinner variants = ((Spinner) dialog.findViewById(R.id.variants));
final List<SpinnerVariantElement> variantNames = new ArrayList<>();
for (SingleContainer<VariantService.Variant> variantContainer : getVariants().Properties) {
SpinnerVariantElement el = new SpinnerVariantElement();
el.name = variantContainer.Properties.Name;
if (variantContainer.Properties.Nations != null) {
el.players = variantContainer.Properties.Nations.size();
}
variantNames.add(el);
}
int classical = 0;
Collections.sort(variantNames);
for (int i = 0; i < variantNames.size(); i++) {
if (variantNames.get(i).name.equals("Classical")) {
classical = i;
}
}
ArrayAdapter<SpinnerVariantElement> variantAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_item, variantNames);
variants.setAdapter(variantAdapter);
variantAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
variants.setSelection(classical);
final EditText gameNameView = (EditText) dialog.findViewById(R.id.desc);
final EditText phaseLengthView = (EditText) dialog.findViewById(R.id.phase_length);
final Spinner phaseLengthUnitsSpinner = (Spinner) dialog.findViewById(R.id.phase_length_units);
final EditText minRatingView = (EditText) dialog.findViewById(R.id.min_rating);
final EditText maxRatingView = (EditText) dialog.findViewById(R.id.max_rating);
final EditText minReliabilityView = (EditText) dialog.findViewById(R.id.min_reliability);
final EditText minQuicknessView = (EditText) dialog.findViewById(R.id.min_quickness);
final EditText maxHatedView = (EditText) dialog.findViewById(R.id.max_hated);
final EditText maxHaterView = (EditText) dialog.findViewById(R.id.max_hater);
final View.OnFocusChangeListener gameNameListener = new View.OnFocusChangeListener() {
private final int key = random.nextInt(Integer.MAX_VALUE);
private boolean generatedName = true;
@Override
public void onFocusChange(View view, boolean b) {
if (view.equals(gameNameView)) {
if (gameNameView.getText().toString().isEmpty()) {
generatedName = true;
}
}
if (generatedName) {
long phaseLength = getPhaseLengthMinutes(phaseLengthView, phaseLengthUnitsSpinner);
String battle;
if (phaseLength < DAY_IN_MINUTES) {
battle = getKeyedString(R.array.blitz);
} else if (phaseLength <= 2 * DAY_IN_MINUTES) {
battle = getKeyedString(R.array.battle);
} else {
battle = getKeyedString(R.array.war);
}
String adjective;
if (getDoubleValue(minRatingView, 0) > 1400) {
adjective = getKeyedString(R.array.quality);
} else if (getDoubleValue(maxRatingView, 2000) < 1000) {
adjective = getKeyedString(R.array.fun);
} else if (getDoubleValue(minReliabilityView, 0) > 10) {
adjective = getKeyedString(R.array.reliable);
} else if (getDoubleValue(minQuicknessView, 0) > 10) {
adjective = getKeyedString(R.array.fast);
} else if (getDoubleValue(maxHatedView, 100) < 10) {
adjective = getKeyedString(R.array.pleasant);
} else if (getDoubleValue(maxHaterView, 100) < 10) {
adjective = getKeyedString(R.array.patient);
} else {
adjective = getKeyedString(R.array.other);
}
String prize = getKeyedString(R.array.prize);
String calculatedName = getResources().getString(R.string.game_name_template, battle, adjective, prize);
if (view.equals(gameNameView)) {
String enteredName = gameNameView.getText().toString();
generatedName = (enteredName.isEmpty() || enteredName.equals(calculatedName));
} else {
gameNameView.setText(calculatedName);
}
}
}
private double getDoubleValue(EditText editText, int def) {
try {
return Double.parseDouble(editText.getText().toString());
} catch (NumberFormatException e) {
return def;
}
}
private String getKeyedString(int arrayId) {
return getResources().getStringArray(arrayId)[key % getResources().getStringArray(arrayId).length];
}
};
AdapterView.OnItemSelectedListener phaseLengthUnitsListener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
gameNameListener.onFocusChange(view, false);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
parent.setSelection(0);
}
};
setDefaultPhaseLength(phaseLengthView, phaseLengthUnitsSpinner);
setDefaultMinReliability(minReliabilityView, statsContainer.get(0));
setDefaultMinQuickness(minQuicknessView, statsContainer.get(0));
gameNameView.setOnFocusChangeListener(gameNameListener);
phaseLengthView.setOnFocusChangeListener(gameNameListener);
minRatingView.setOnFocusChangeListener(gameNameListener);
maxRatingView.setOnFocusChangeListener(gameNameListener);
minReliabilityView.setOnFocusChangeListener(gameNameListener);
minQuicknessView.setOnFocusChangeListener(gameNameListener);
maxHatedView.setOnFocusChangeListener(gameNameListener);
maxHaterView.setOnFocusChangeListener(gameNameListener);
phaseLengthUnitsSpinner.setOnItemSelectedListener(phaseLengthUnitsListener);
((FloatingActionButton) dialog.findViewById(R.id.create_game_button)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final Game game = new Game();
game.Desc = gameNameView.getText().toString();
game.Variant = variantNames.get(variants.getSelectedItemPosition()).name;
game.PhaseLengthMinutes = getPhaseLengthMinutes(phaseLengthView, phaseLengthUnitsSpinner);
try {
game.MinRating = Double.parseDouble(minRatingView.getText().toString());
} catch (NumberFormatException e) {
}
try {
game.MaxRating = Double.parseDouble(maxRatingView.getText().toString());
} catch (NumberFormatException e) {
}
try {
game.MinReliability = Double.parseDouble(minReliabilityView.getText().toString());
} catch (NumberFormatException e) {
}
try {
game.MinQuickness = Double.parseDouble(minQuicknessView.getText().toString());
} catch (NumberFormatException e) {
}
try {
game.MaxHated = Double.parseDouble(maxHatedView.getText().toString());
} catch (NumberFormatException e) {
}
try {
game.MaxHater = Double.parseDouble(maxHaterView.getText().toString());
} catch (NumberFormatException e) {
}
if (validateGame.Return(game)) {
handleReq(gameService.GameCreate(game), new Sendable<SingleContainer<Game>>() {
@Override
public void send(SingleContainer<Game> gameSingleContainer) {
if (nextCursorContainer.get(0).length() == 0) {
findViewById(R.id.empty_view).setVisibility(View.GONE);
contentList.setVisibility(View.VISIBLE);
gamesAdapter.items.add(gameSingleContainer);
gamesAdapter.notifyDataSetChanged();
}
dialog.dismiss();
}
}, new ErrorHandler(412, new Sendable<HttpException>() {
@Override
public void send(HttpException e) {
handleReq(userStatsService.UserStatsLoad(getLoggedInUser().Id), new Sendable<SingleContainer<UserStats>>() {
@Override
public void send(SingleContainer<UserStats> userStatsSingleContainer) {
statsContainer.set(0, userStatsSingleContainer.Properties);
validateGame.Return(game);
}
}, getResources().getString(R.string.creating_game));
}
}), getResources().getString(R.string.creating_game));
}
}
});
}
}, getResources().getString(R.string.loading_user_stats));
}
/**
* Determine the current user entered phase length.
*
* @param phaseLengthView The view containing the selected quantity.
* @param phaseLengthUnitsView The spiner containing the selected units.
* @return The selected phase length in minutes.
*/
private long getPhaseLengthMinutes(EditText phaseLengthView, Spinner phaseLengthUnitsView) {
// Get the quantity entered
long quantity;
try {
quantity = Long.valueOf(phaseLengthView.getText().toString());
} catch (NumberFormatException e) {
setDefaultPhaseLength(phaseLengthView, phaseLengthUnitsView);
quantity = Long.valueOf(phaseLengthView.getText().toString());
}
// Use the quantity and the selected unit to find the phase length.
long phaseLength;
int selectedItem = phaseLengthUnitsView.getSelectedItemPosition();
if (selectedItem == getIndexOfUnit(R.string._day_s)) {
phaseLength = quantity * DAY_IN_MINUTES;
} else if (selectedItem == getIndexOfUnit(R.string._hour_s)) {
phaseLength = quantity * HOUR_IN_MINUTES;
} else if (selectedItem == getIndexOfUnit(R.string._minute_s)) {
phaseLength = quantity;
} else {
Log.e("Diplicity", "Programmer error: Unexpected phase length units selected");
throw new IllegalStateException("Programmer error: Unexpected phase length units selected");
}
return phaseLength;
}
/**
* Set the phase length to the default of one day.
*/
private void setDefaultPhaseLength(EditText phaseLengthView, Spinner phaseLengthUnitsView) {
phaseLengthView.setText("1");
phaseLengthUnitsView.setSelection(getIndexOfUnit(R.string._day_s));
}
private void setDefaultMinReliability(EditText minReliabilityView, UserStats userStats) {
if (userStats.Reliability > 10) {
minReliabilityView.setText("10");
} else {
minReliabilityView.setText("" + userStats.Reliability.intValue());
}
}
private void setDefaultMinQuickness(EditText minQuicknessView, UserStats userStats) {
if (userStats.Quickness > 10) {
minQuicknessView.setText("10");
} else {
minQuicknessView.setText("" + userStats.Quickness.intValue());
}
}
/**
* Get the index of the given unit string resource in the array list used by the units spinner.
*/
private int getIndexOfUnit(int unitString) {
List<String> unitLabels = asList(getResources().getStringArray(R.array.phaseLengthUnits));
return unitLabels.indexOf(getResources().getString(unitString));
}
});
setupNavigation();
contentList = (RecyclerView) findViewById(R.id.content_list);
LinearLayoutManager contentLayoutManager = new LinearLayoutManager(this);
contentLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
contentList.setLayoutManager(contentLayoutManager);
scrollListener = new EndlessRecyclerViewScrollListener(contentLayoutManager, nextCursor) {
@Override
public void onLoadMore(String cursor, int totalItemsCount, RecyclerView view) {
if (cursor.length() > 0) {
loadMoreProcContainer.get(0).send(cursor);
}
}
};
contentList.addOnScrollListener(scrollListener);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, contentLayoutManager.getOrientation());
contentList.addItemDecoration(dividerItemDecoration);
loadMoreProcContainer.add(null);
if (!ACTION_VIEW_USER_GAMES.equals(getIntent().getAction()) && !Intent.ACTION_VIEW.equals(getIntent().getAction()))
navigateTo(0, 0);
}
use of se.oort.diplicity.apigen.UserStats in project android-diplicity by zond.
the class UserView method getAvatarClickListener.
public static OnClickListener getAvatarClickListener(final RetrofitActivity retrofitActivity, final User user) {
return new OnClickListener() {
@Override
public void onClick(View v) {
retrofitActivity.handleReq(JoinObservable.when(JoinObservable.from(retrofitActivity.userStatsService.UserStatsLoad(user.Id)).and(retrofitActivity.banService.BanLoad(retrofitActivity.getLoggedInUser().Id, user.Id).onErrorReturn(new Func1<Throwable, SingleContainer<Ban>>() {
@Override
public SingleContainer<Ban> call(Throwable throwable) {
if (throwable instanceof HttpException) {
HttpException he = (HttpException) throwable;
if (he.code() == 404) {
return null;
}
}
throw new RuntimeException(throwable);
}
})).then(new Func2<SingleContainer<UserStats>, SingleContainer<Ban>, Object>() {
@Override
public Object call(SingleContainer<UserStats> userStatsSingleContainer, SingleContainer<Ban> banSingleContainer) {
AlertDialog dialog = new AlertDialog.Builder(retrofitActivity).setView(R.layout.user_dialog).show();
setupUserDialog(retrofitActivity, dialog, userStatsSingleContainer, banSingleContainer);
return null;
}
})).toObservable(), new Sendable<Object>() {
@Override
public void send(Object o) {
}
}, retrofitActivity.getResources().getString(R.string.loading_user_stats));
}
};
}
Aggregations