diff --git a/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/GameController.java b/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/GameController.java index f2bb7f0..e58ddd3 100644 --- a/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/GameController.java +++ b/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/GameController.java @@ -111,6 +111,17 @@ public class GameController implements IModelChangedListener, Parcelable { newLevelManager.checkAndRestock(); } + public void loadNewDailySudokuLevel(GameDifficulty gameDifficulty) { + NewLevelManager newLevelManager = NewLevelManager.getInstance(context, settings); + + int[] level = newLevelManager.loadDailySudoku(); + + loadLevel(new GameInfoContainer(DAILY_SUDOKU_ID, gameDifficulty, GameType.Default_9x9, level, null, null)); + + newLevelManager.checkAndRestock(); + + } + public int getTime() { return time; } diff --git a/app/src/main/java/org/secuso/privacyfriendlysudoku/ui/GameActivity.java b/app/src/main/java/org/secuso/privacyfriendlysudoku/ui/GameActivity.java index 7481a83..441e2ae 100644 --- a/app/src/main/java/org/secuso/privacyfriendlysudoku/ui/GameActivity.java +++ b/app/src/main/java/org/secuso/privacyfriendlysudoku/ui/GameActivity.java @@ -55,7 +55,7 @@ import org.secuso.privacyfriendlysudoku.ui.view.SudokuSpecialButtonLayout; import org.secuso.privacyfriendlysudoku.ui.view.WinDialog; import org.secuso.privacyfriendlysudoku.ui.view.databinding.DialogFragmentShareBoardBinding; -import java.util.Date; + import java.util.LinkedList; import java.util.List; @@ -158,23 +158,29 @@ public class GameActivity extends BaseActivity implements NavigationView.OnNavig gameController.loadLevel(container); } else { + boolean isDailySudoku = false; if (extras != null) { gameType = GameType.valueOf(extras.getString("gameType", GameType.Default_9x9.name())); gameDifficulty = GameDifficulty.valueOf(extras.getString("gameDifficulty", GameDifficulty.Moderate.name())); + isDailySudoku = extras.getBoolean("isDailySudoku", false); loadLevel = extras.getBoolean("loadLevel", false); if (loadLevel) { loadLevelID = extras.getInt("loadLevelID"); } } + if (isDailySudoku) { + gameController.loadNewDailySudokuLevel(gameDifficulty); + } else { - List loadableGames = GameStateManager.getLoadableGameList(); + List loadableGames = GameStateManager.getLoadableGameList(); - if (loadLevel && loadableGames.size() > loadLevelID) { - // load level from GameStateManager - gameController.loadLevel(loadableGames.get(loadLevelID)); - } else { - // load a new level - gameController.loadNewLevel(gameType, gameDifficulty); + if (loadLevel && loadableGames.size() > loadLevelID) { + // load level from GameStateManager + gameController.loadLevel(loadableGames.get(loadLevelID)); + } else { + // load a new level + gameController.loadNewLevel(gameType, gameDifficulty); + } } } } else { diff --git a/app/src/main/java/org/secuso/privacyfriendlysudoku/ui/view/DailySudokuActivity.java b/app/src/main/java/org/secuso/privacyfriendlysudoku/ui/view/DailySudokuActivity.java index 96aeba9..3cc73d9 100644 --- a/app/src/main/java/org/secuso/privacyfriendlysudoku/ui/view/DailySudokuActivity.java +++ b/app/src/main/java/org/secuso/privacyfriendlysudoku/ui/view/DailySudokuActivity.java @@ -1,16 +1,44 @@ package org.secuso.privacyfriendlysudoku.ui.view; + +import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; +import android.os.Handler; +import android.preference.PreferenceManager; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuItem; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.ImageView; +import android.widget.ListView; +import android.widget.TextView; +import android.view.View; +import android.widget.RatingBar; +import org.secuso.privacyfriendlysudoku.controller.database.DatabaseHelper; +import org.secuso.privacyfriendlysudoku.controller.database.model.DailySudoku; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; -import org.secuso.privacyfriendlysudoku.controller.helper.GameInfoContainer; +import org.secuso.privacyfriendlysudoku.controller.SaveLoadStatistics; +import org.secuso.privacyfriendlysudoku.game.GameDifficulty; +import org.secuso.privacyfriendlysudoku.ui.GameActivity; +import org.secuso.privacyfriendlysudoku.ui.StatsActivity; import java.util.List; -public class DailySudokuActivity extends AppCompatActivity { - List loadableGameList; +public class DailySudokuActivity extends AppCompatActivity { + + List sudokuList; SharedPreferences settings; + private final DatabaseHelper dbHelper = new DatabaseHelper(this); + RatingBar difficultyBar; + static final int MAIN_CONTENT_FADEOUT_DURATION = 150; + static final int MAIN_CONTENT_FADEIN_DURATION = 250; + private Handler mHandler; + private StatsActivity.SectionsPagerAdapter mSectionsPagerAdapter; + private SudokuListAdapter sudokuListAdapter; @Override @@ -22,11 +50,118 @@ public class DailySudokuActivity extends AppCompatActivity { setSupportActionBar(toolbar); + List sudokus = dbHelper.getDailySudokus(); + TextView tw = findViewById(R.id.first_diff_text); + tw.setText(String.valueOf(sudokus.size())); + androidx.appcompat.app.ActionBar actionBar = getSupportActionBar(); actionBar.setTitle("Daily Sudoku"); actionBar.setDisplayHomeAsUpEnabled(true); + difficultyBar = findViewById(R.id.first_diff_bar); + settings = PreferenceManager.getDefaultSharedPreferences(this); + mHandler = new Handler(); + sudokuList = dbHelper.getDailySudokus(); + + ListView listView = (ListView)findViewById(R.id.sudoku_list); + sudokuListAdapter = new DailySudokuActivity.SudokuListAdapter(this, sudokuList); + listView.setAdapter(sudokuListAdapter); } + + public void onClick(View view) { + + int index = difficultyBar.getProgress()-1; + GameDifficulty gameDifficulty = GameDifficulty.getValidDifficultyList().get(index < 0 ? 0 : index); + //send everything to game activity + final Intent intent = new Intent(this,GameActivity.class); + intent.putExtra("gameDifficulty", gameDifficulty.name()); + intent.putExtra("isDailySudoku", true); + + startActivity(intent); + + } + + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.menu_stats, menu); + //getMenuInflater().inflate(R.menu.menu_stats, menu); + return true; + //return false; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + + //noinspection SimplifiableIfStatement + switch(item.getItemId()) { + case R.id.action_reset: + SaveLoadStatistics.resetStats(this); + mSectionsPagerAdapter.refresh(this); + return true; + case android.R.id.home: + finish(); + return true; + } + + return super.onOptionsItemSelected(item); + } + + private class SudokuListAdapter extends BaseAdapter { + + private Context context; + private List sudokuList; + + public SudokuListAdapter(Context context, List sudokuList) { + this.context = context; + this.sudokuList = sudokuList; + } + + @Override + public int getCount() { + return sudokuList.size(); + } + + @Override + public Object getItem(int position) { + return sudokuList.get(position); + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + DailySudoku sudoku = sudokuList.get(position); + + if (convertView == null) { + LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + convertView = (View) inflater.inflate(R.layout.list_entry_layout, null); + } + + 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); + + + image.setImageResource(R.drawable.icon_default_9x9); + + gameType.setText(sudoku.getGameType().getStringResID()); + difficulty.setText(sudoku.getDifficulty().getStringResID()); + difficultyBar.setNumStars(GameDifficulty.getValidDifficultyList().size()); + difficultyBar.setMax(GameDifficulty.getValidDifficultyList().size()); + difficultyBar.setRating(GameDifficulty.getValidDifficultyList().indexOf(sudoku.getDifficulty())+1); + + return convertView; + } + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_daily_sudoku.xml b/app/src/main/res/layout/activity_daily_sudoku.xml index 18bfd7d..308b70d 100644 --- a/app/src/main/res/layout/activity_daily_sudoku.xml +++ b/app/src/main/res/layout/activity_daily_sudoku.xml @@ -52,7 +52,7 @@ + android:textSize="32dp" /> @@ -192,7 +200,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -505,6 +235,7 @@ android:layout_marginRight="30dp" android:layout_marginBottom="290dp" android:clickable="true" + android:onClick="onClick" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:srcCompat="@android:drawable/ic_media_play" />