Skip to content

Commit

Permalink
cleaned up copy paste errors, utility for complexity enum, fixed bug …
Browse files Browse the repository at this point in the history
…with restricting types
  • Loading branch information
peer committed Apr 7, 2015
1 parent 319db1a commit 409a4b7
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 100 deletions.
2 changes: 1 addition & 1 deletion sudoq-app/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<!-- This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.sudoq"
android:versionCode="12"
android:versionCode="13"
android:versionName="1.0.8"
android:installLocation="auto" >

Expand Down
28 changes: 2 additions & 26 deletions sudoq-app/res/layout-land/preferences_newsudoku.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,8 @@
android:text="@string/profile_preference_button_advanced_preferences"/>

</LinearLayout>

<LinearLayout
android:id="@+id/playerpreferences_layout_profilename"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/textview_profilename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/profile_preference_title_name"
android:layout_margin="5dp"
android:singleLine="true"
android:inputType="textCapCharacters"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/black" />

<EditText
android:id="@+id/edittext_profilename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="0.12" />

</LinearLayout>
<!-- makes 2 collumns -->

<!-- makes 2 collumns -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
Expand Down
14 changes: 14 additions & 0 deletions sudoq-app/res/menu/action_bar_restrict_types.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<!-- restore all disabled -->
<item android:id="@+id/action_restore_all"
android:title="@string/action_restore_all"
android:showAsAction="never" />

<!-- Tutorial -->
<item android:id="@+id/action_show_tutorial"
android:title="@string/action_show_tutorial"
android:showAsAction="never" />

</menu>
5 changes: 1 addition & 4 deletions sudoq-app/res/menu/action_bar_sudoku_loading.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,4 @@
android:title="@string/action_show_tutorial"
android:showAsAction="never" />

</menu>

<!-- android:icon="@drawable/ic_action_refresh"
android:title="@string/action_tutorial"-->
</menu>
1 change: 1 addition & 0 deletions sudoq-app/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<string name="action_switch_profile">Profil wechseln</string>
<string name="action_delete_all_gestures">Alle Gesten löschen</string>
<string name="action_delete_single_gesture">Spezifische Geste löschen</string>
<string name="action_restore_all">Alle wiederherstellen</string>


<!-- Dialogs -->
Expand Down
2 changes: 1 addition & 1 deletion sudoq-app/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<string name="action_switch_profile">Changer de profil</string>
<string name="action_delete_all_gestures">Supprimer tous les gestes</string>
<string name="action_delete_single_gesture">Supprimer geste spécifique</string>

<string name="action_restore_all">Restaurer Tous</string>

<!-- Dialogs -->
<string name="dialog_yes">Oui</string>
Expand Down
1 change: 1 addition & 0 deletions sudoq-app/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<string name="action_switch_profile">Switch Profile</string>
<string name="action_delete_all_gestures">Delete all gestures</string>
<string name="action_delete_single_gesture">Delete specific gesture</string>
<string name="action_restore_all">Restore all</string>


<!-- Error Messages -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
sudokuType=null;//set to null to prevent: going to advanced -> disabling all -> coming back -> now disabled type still set
//pass (user disabled all types, so a hint is shown. This hint may not be saved as a sudoku type!)
}else{
setSudokuType(Utility.string2enum(getApplicationContext(), item));
Log.d(LOG_TAG, "OnItemSel_a "+sudokuType);
setSudokuType(Utility.string2type(getApplicationContext(), item));
Log.d(LOG_TAG, "OnItemSel_z "+sudokuType);
}
}

Expand All @@ -121,15 +123,7 @@ public void onNothingSelected(AdapterView<?> parent) {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String item = parent.getItemAtPosition(pos).toString();

if (item.equals(getResources().getString(R.string.complexity_easy))) {
setSudokuDifficulty(Complexity.easy);
} else if (item.equals(getResources().getString(R.string.complexity_medium))) {
setSudokuDifficulty(Complexity.medium);
} else if (item.equals(getResources().getString(R.string.complexity_difficult))) {
setSudokuDifficulty(Complexity.difficult);
} else if (item.equals(getResources().getString(R.string.complexity_infernal))) {
setSudokuDifficulty(Complexity.infernal);
}
setSudokuDifficulty(Utility.string2complexity(getApplicationContext(), item));
}

public void onNothingSelected(AdapterView<?> parent) {
Expand All @@ -146,7 +140,14 @@ public void onNothingSelected(AdapterView<?> parent) {
@Override
public void onResume() {
super.onResume();
fillTypeSpinner(Profile.getInstance().getAssistances().getWantedTypesList());
SudokuTypesList wtl = Profile.getInstance().getAssistances().getWantedTypesList();
fillTypeSpinner(wtl);
/* this is a hack: for some reason when returning from settings, the typeSpinner selects the first position
* probably because it gets a new adapter. At the time I'm unable to debug this properly
* (judging from the LOG.d's it happens after this method) but it seems to work */
if(wtl.contains(sudokuType))
((Spinner) findViewById(R.id.spinner_sudokutype)).setSelection(wtl.indexOf(sudokuType));
Log.d(LOG_TAG, "Resume_ende: "+sudokuType);
}

private Spinner fillTypeSpinner(SudokuTypesList stl) {
Expand All @@ -162,12 +163,14 @@ private Spinner fillTypeSpinner(SudokuTypesList stl) {
Collections.sort(stl);//sortieren
/* converse */
for(SudokuTypes st: stl)
wantedSudokuTypes.add(Utility.enum2string(this, st));

wantedSudokuTypes.add(Utility.type2string(this, st));
}

Log.d(LOG_TAG, "Sudokutype_1: " + this.sudokuType);
ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, wantedSudokuTypes);
typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
typeSpinner.setAdapter(typeAdapter);
Log.d(LOG_TAG, "Sudokutype_4: " + this.sudokuType);

return typeSpinner;
}
Expand Down
24 changes: 6 additions & 18 deletions sudoq-app/src/de/sudoq/controller/menus/SudokuLoadingAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
import de.sudoq.R;
import de.sudoq.model.files.FileManager;
import de.sudoq.model.game.GameData;
import de.sudoq.model.sudoku.complexity.Complexity;
import de.sudoq.model.sudoku.sudokuTypes.SudokuTypes;

/**
* Adapter für die Anzeige aller Spiele des Spielers
Expand All @@ -43,8 +41,6 @@ public class SudokuLoadingAdapter extends ArrayAdapter<GameData> {
private static final String LOG_TAG = SudokuLoadingAdapter.class.getSimpleName();
private final Context context;
private final List<GameData> gameDatas;
private String[] typeStrings;
private String[] complexityStrings;

/**
* Erzeugt einen neuen SudokuLoadingAdpater mit den gegebenen Parametern
Expand All @@ -58,7 +54,6 @@ public SudokuLoadingAdapter(Context context, List<GameData> games) {
super(context, R.layout.sudokuloadingitem, games);
this.context = context;
this.gameDatas = games;
initialiseComplexities();
}

/**
Expand Down Expand Up @@ -98,8 +93,8 @@ public View getView(int position, View convertView, ViewGroup parent) {
((SudokuLoadingActivity) context).finish();
}

sudokuType.setText(typeStrings[gameDatas.get(position).getType().ordinal()]);
sudokuComplexity.setText(complexityStrings[gameDatas.get(position).getComplexity().ordinal()]);
sudokuType. setText(Utility. type2string(getContext(), gameDatas.get(position).getType()));
sudokuComplexity.setText(Utility.complexity2string(getContext(), gameDatas.get(position).getComplexity()));

TimeZone tz = TimeZone.getDefault();
SimpleDateFormat sdf = new SimpleDateFormat(context.getString(R.string.time_format));
Expand All @@ -111,22 +106,15 @@ public View getView(int position, View convertView, ViewGroup parent) {

if (gameDatas.get(position).isFinished()) {
sudokuState.setText(context.getString(R.string.check_mark));
sudokuType.setTextColor(Color.GRAY);
sudokuComplexity.setTextColor(Color.GRAY);
sudokuTime.setTextColor(Color.GRAY);
sudokuState.setTextColor(Color.GREEN);
sudokuType. setTextColor(Color.GRAY);
sudokuComplexity.setTextColor(Color.GRAY);
sudokuTime. setTextColor(Color.GRAY);

}else{
sudokuState.setText("");
}

return rowView;
}

private void initialiseComplexities() {
complexityStrings = new String[Complexity.values().length];
complexityStrings[Complexity.easy.ordinal()] = context.getString(R.string.complexity_easy);
complexityStrings[Complexity.medium.ordinal()] = context.getString(R.string.complexity_medium);
complexityStrings[Complexity.difficult.ordinal()] = context.getString(R.string.complexity_difficult);
complexityStrings[Complexity.infernal.ordinal()] = context.getString(R.string.complexity_infernal);
}
}
36 changes: 32 additions & 4 deletions sudoq-app/src/de/sudoq/controller/menus/Utility.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package de.sudoq.controller.menus;

import android.content.Context;
import android.content.res.Resources;
import de.sudoq.R;
import de.sudoq.model.sudoku.complexity.Complexity;
import de.sudoq.model.sudoku.sudokuTypes.SudokuTypes;

public class Utility {


private static String[] getSudokuTypeValues(Context context){
String[] typeStrings = new String[SudokuTypes.values().length];
typeStrings[SudokuTypes.standard4x4. ordinal()] = context.getString(R.string.sudoku_type_standard_4x4);
Expand All @@ -23,20 +24,47 @@ private static String[] getSudokuTypeValues(Context context){
}



public static SudokuTypes string2enum(Context context, String string){
public static SudokuTypes string2type(Context context, String string){
String[] typeStrings = getSudokuTypeValues(context);
for(int i=0; i<typeStrings.length; i++)
if(string.equals(typeStrings[i]))
return SudokuTypes.values()[i];
return null;
}

public static String enum2string(Context context, SudokuTypes st){
public static String type2string(Context context, SudokuTypes st){
String[] typeStrings = getSudokuTypeValues(context);
int index = st.ordinal();
return index >= typeStrings.length ? null : typeStrings[index];
}




private static String[] getComplexityValues(Context context){
String[] typeStrings = new String[Complexity.values().length];
typeStrings[Complexity.easy. ordinal()] = context.getString(R.string.complexity_easy);
typeStrings[Complexity.medium. ordinal()] = context.getString(R.string.complexity_medium);
typeStrings[Complexity.difficult.ordinal()] = context.getString(R.string.complexity_difficult);
typeStrings[Complexity.infernal. ordinal()] = context.getString(R.string.complexity_infernal);
typeStrings[Complexity.arbitrary.ordinal()] = "error";//only for compatibility. There's no reason to convert 'arbitrary' to a string
return typeStrings;
}

public static Complexity string2complexity(Context context, String string){
String[] complexityStrings = getComplexityValues(context);
for(int i=0; i<complexityStrings.length; i++)
if(string.equals(complexityStrings[i]))
return Complexity.values()[i];
return null;
}

public static String complexity2string(Context context, Complexity st){
String[] complexityStrings = getComplexityValues(context);
int index = st.ordinal();
return index >= complexityStrings.length ? null : complexityStrings[index];
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import de.sudoq.R;
import de.sudoq.controller.menus.SudokuLoadingActivity;
import de.sudoq.model.game.GameSettings;
import de.sudoq.model.profile.Profile;

Expand Down Expand Up @@ -83,7 +81,7 @@ public void selectTypesToRestrict(View view) {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.action_bar_player_preferences, menu);
inflater.inflate(R.menu.action_bar_standard, menu);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,16 @@
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.TextView;
import android.widget.Toast;
import de.sudoq.R;
import de.sudoq.controller.SudoqListActivity;
import de.sudoq.controller.sudoku.SudokuActivity;
import de.sudoq.model.game.GameData;
import de.sudoq.model.game.GameManager;
import de.sudoq.model.profile.Profile;
import de.sudoq.model.sudoku.sudokuTypes.SudokuTypes;
import de.sudoq.model.xml.SudokuTypesList;
Expand All @@ -52,12 +44,6 @@ public class RestrictTypesActivity extends SudoqListActivity implements OnItemCl
private RestrictTypesAdapter adapter;

private SudokuTypesList types;

protected static MenuItem menuDeleteFinished;
private static final int MENU_DELETE_FINISHED = 0;

protected static MenuItem menuDeleteSpecific;
private static final int MENU_DELETE_SPECIFIC = 1;

/** Constructors */

Expand Down Expand Up @@ -86,7 +72,7 @@ public void onCreate(Bundle savedInstanceState) {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.action_bar_sudoku_loading, menu);
inflater.inflate(R.menu.action_bar_restrict_types, menu);
return true;
}

Expand All @@ -101,12 +87,11 @@ public boolean onCreateOptionsMenu(Menu menu) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_sudokuloading_delete_finished:
GameManager.getInstance().deleteFinishedGames();
break;
case R.id.action_sudokuloading_delete_all:
for (GameData gd : GameManager.getInstance().getGameList())
GameManager.getInstance().deleteGame(gd.getId());
case R.id.action_restore_all:
/* add (only!) types that are not currently selected */
for(SudokuTypes s: types.getAllTypes())
if(!types.contains(s))
types.add(s);
break;
default:
super.onOptionsItemSelected(item);
Expand All @@ -118,11 +103,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
List<GameData> gamesList = GameManager.getInstance().getGameList();
boolean noGames = gamesList.isEmpty();

menu.findItem(R.id.action_sudokuloading_delete_finished).setVisible(!noGames);
menu.findItem(R.id.action_sudokuloading_delete_all ).setVisible(!noGames);
//Toast.makeText(getApplicationContext(), "prepOpt called. s_1: "+types.size()+" s_2: "+types.getAllTypes().size(), Toast.LENGTH_LONG).show();
menu.findItem(R.id.action_restore_all).setVisible(types.size() < types.getAllTypes().size());//offer option to restore all only when some are disabled...

return true;
}
Expand Down Expand Up @@ -183,8 +166,7 @@ public boolean onItemLongClick(AdapterView<?> parent, View view, final int posit

private void initialiseTypes() {
types = Profile.getInstance().getAssistances().getWantedTypesList();
Log.d(LOG_TAG, "typesSize: "+types.size());
// initialize ArrayAdapter for the profile names and set it
// initialize ArrayAdapter for the type names and set it
adapter = new RestrictTypesAdapter(this, types);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
Expand Down
Loading

0 comments on commit 409a4b7

Please sign in to comment.