Redesigned the Continue Game Screen. Also the list is now sorted and only the top 10 recent played games are saved. All the others are deleted.

This commit is contained in:
Christopher Beckmann 2015-11-24 11:06:39 +01:00
parent 7d2ec4abae
commit e2ecfe9292
6 changed files with 161 additions and 21 deletions

View file

@ -141,7 +141,7 @@ public class GameController implements IModelChangedListener {
boolean[][] setNotes = gic.getSetNotes();
this.gameID = gic.getID();
this.difficulty = gic.getDifficulty();
this.time = gic.getTime();
this.time = gic.getTimePlayed();
setGameType(gic.getGameType());
this.gameBoard = new GameBoard(gic.getGameType());

View file

@ -1,5 +1,6 @@
package tu_darmstadt.sudoku.controller;
import android.os.*;
import android.util.Log;
import java.util.LinkedList;

View file

@ -24,6 +24,7 @@ public class SaveLoadGameStateController {
private static String FILE_EXTENSION = ".txt";
private static String SAVE_PREFIX = "save_";
private static String SAVES_DIR = "saves";
private static final int MAX_NUM_OF_SAVED_GAMES = 10;
private static List<GameInfoContainer> list = new LinkedList<>();
@ -42,7 +43,7 @@ public class SaveLoadGameStateController {
}
File dir = context.getDir(SAVES_DIR, 0);
List<GameInfoContainer> result = new LinkedList<>();
LinkedList<GameInfoContainer> result = new LinkedList<>();
// go through every file
for(File file : dir.listFiles()) {
@ -98,7 +99,75 @@ public class SaveLoadGameStateController {
editor.putBoolean("savesChanged", false);
editor.commit();
list = result;
list = sortListByLastPlayedDate(result);
LinkedList<GameInfoContainer> removeList = new LinkedList<>();
for(int i = 0; i < list.size(); i++) {
if(i >= MAX_NUM_OF_SAVED_GAMES) {
deleteGameStateFile(list.get(i));
removeList.add(list.get(i));
}
}
for(GameInfoContainer gic : removeList) {
list.remove(gic);
}
return list;
}
private void deleteGameStateFile(GameInfoContainer gic) {
File dir = context.getDir(SAVES_DIR, 0);
File file = new File(dir, SAVE_PREFIX+gic.getID()+FILE_EXTENSION);
file.delete();
}
public LinkedList<GameInfoContainer> sortListByLastPlayedDate(LinkedList<GameInfoContainer> list) {
if(list.size() < 2) {
return list;
}
LinkedList<GameInfoContainer> listL = new LinkedList<>();
LinkedList<GameInfoContainer> listR = new LinkedList<>();
int middle = list.size()/2;
for(int i = 0; i < list.size(); i++) {
if(i < middle) {
listL.add(list.get(i));
} else {
listR.add(list.get(i));
}
}
listL = sortListByLastPlayedDate(listL);
listR = sortListByLastPlayedDate(listR);
return sortListByLastPlayedDateMerge(listL, listR);
}
public LinkedList<GameInfoContainer> sortListByLastPlayedDateMerge(LinkedList<GameInfoContainer> list1, LinkedList<GameInfoContainer> list2) {
LinkedList<GameInfoContainer> result = new LinkedList<>();
while(!(list1.isEmpty() && list2.isEmpty())) {
GameInfoContainer gic1 = list1.peek();
GameInfoContainer gic2 = list2.peek();
if(gic1 == null) {
result.add(list2.pop());
} else if(gic2 == null) {
result.add(list1.pop());
} else if(gic1.getLastTimePlayed().after(gic2.getLastTimePlayed())) {
result.add(list1.pop());
} else {
result.add(list2.pop());
}
}
return result;
}

View file

@ -51,10 +51,14 @@ public class GameInfoContainer {
}
}
public int getTime() {
public int getTimePlayed() {
return timePlayed;
}
public Date getLastTimePlayed() {
return lastTimePlayed;
}
public void parseTime(String s) {
try {
this.timePlayed = Integer.valueOf(s);

View file

@ -13,12 +13,19 @@ import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import tu_darmstadt.sudoku.controller.SaveLoadGameStateController;
import tu_darmstadt.sudoku.controller.helper.GameInfoContainer;
import tu_darmstadt.sudoku.game.GameDifficulty;
import tu_darmstadt.sudoku.ui.view.R;
public class LoadGameActivity extends AppCompatActivity {
@ -98,8 +105,11 @@ public class LoadGameActivity extends AppCompatActivity {
GameInfoContainer gic = loadableGameList.get(position);
TextView name = (TextView)convertView.findViewById(R.id.loadgame_listentry_gametype);
TextView summary=(TextView)convertView.findViewById(R.id.loadgame_listentry_id);
TextView gameType = (TextView)convertView.findViewById(R.id.loadgame_listentry_gametype);
TextView difficulty =(TextView)convertView.findViewById(R.id.loadgame_listentry_difficultytext);
RatingBar difficultyBar =(RatingBar)convertView.findViewById(R.id.loadgame_listentry_difficultybar);
TextView playedTime = (TextView)convertView.findViewById(R.id.loadgame_listentry_timeplayed);
TextView lastTimePlayed = (TextView)convertView.findViewById(R.id.loadgame_listentry_lasttimeplayed);
ImageView image = (ImageView)convertView.findViewById(R.id.loadgame_listentry_gametypeimage);
switch(gic.getGameType()) {
@ -115,8 +125,26 @@ public class LoadGameActivity extends AppCompatActivity {
default:
image.setImageResource(R.drawable.icon_default_9x9);
}
name.setText(gic.getGameType().name());
summary.setText(String.valueOf(gic.getID()));
gameType.setText(gic.getGameType().getStringResID());
difficulty.setText(gic.getDifficulty().getStringResID());
difficultyBar.setRating(GameDifficulty.getValidDifficultyList().indexOf(gic.getDifficulty())+1);
int time = gic.getTimePlayed();
int seconds = time % 60;
int minutes = ((time -seconds)/60)%60 ;
int hours = (time - minutes - seconds)/(3600);
String h,m,s;
s = (seconds< 10)? "0"+String.valueOf(seconds):String.valueOf(seconds);
m = (minutes< 10)? "0"+String.valueOf(minutes):String.valueOf(minutes);
h = (hours< 10)? "0"+String.valueOf(hours):String.valueOf(hours);
playedTime.setText(h + ":" + m + ":" + s);
Date lastTimePlayedDate = gic.getLastTimePlayed();
DateFormat format = DateFormat.getDateTimeInstance();
format.setTimeZone(TimeZone.getDefault());
lastTimePlayed.setText(format.format(lastTimePlayedDate));
return convertView;
}

View file

@ -15,23 +15,61 @@
android:src="@drawable/icon_default_9x9"/>
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="4"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:text="ID"
android:id="@+id/loadgame_listentry_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="gametype"
android:layout_below="@+id/loadgame_listentry_id"
android:text="GameType"
android:id="@+id/loadgame_listentry_gametype"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/loadgame_listentry_gametype">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/loadgame_listentry_difficultybar"
android:numStars="3"
android:rating="3"
android:clickable="false"
style="?android:attr/ratingBarStyleSmall"/>
<TextView
android:text="Difficulty"
android:id="@+id/loadgame_listentry_difficultytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:gravity="right">
<TextView
android:text="00:00"
android:textStyle="bold"
android:id="@+id/loadgame_listentry_timeplayed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"/>
<TextView
android:text="1 second ago"
android:gravity="end"
android:layout_below="@+id/loadgame_listentry_timeplayed"
android:id="@+id/loadgame_listentry_lasttimeplayed"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>