Skip to content

Commit

Permalink
Add "Settings" activity for clearing progress
Browse files Browse the repository at this point in the history
  • Loading branch information
meyfa committed Jun 29, 2018
1 parent e3cd794 commit c45c5ae
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 12 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "net.meyfa.statuscodestrainer"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
21 changes: 17 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".activities.main.MainActivity"
<activity
android:name=".activities.main.MainActivity"
android:label="@string/main_title">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand All @@ -26,27 +27,39 @@
android:value=".activities.main.MainActivity" />
</activity>

<activity android:name=".activities.training.TrainingActivity"
<activity
android:name=".activities.training.TrainingActivity"
android:label="@string/training_title">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.main.MainActivity" />
</activity>

<activity android:name=".activities.test.TestActivity"
<activity
android:name=".activities.test.TestActivity"
android:label="@string/test_title">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.main.MainActivity" />
</activity>

<activity android:name=".activities.testresults.TestResultsActivity"
<activity
android:name=".activities.testresults.TestResultsActivity"
android:label="@string/test_results_title">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.main.MainActivity" />
</activity>

<activity
android:name=".activities.settings.SettingsActivity"
android:label="@string/settings_title"
android:theme="@style/AppTheme.ActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.main.MainActivity" />
</activity>

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import net.meyfa.statuscodestrainer.R;
import net.meyfa.statuscodestrainer.activities.reference.ReferenceActivity;
import net.meyfa.statuscodestrainer.activities.settings.SettingsActivity;
import net.meyfa.statuscodestrainer.activities.test.TestActivity;
import net.meyfa.statuscodestrainer.activities.training.TrainingActivity;
import net.meyfa.statuscodestrainer.logic.progress.ProgressTracker;
Expand Down Expand Up @@ -51,6 +55,28 @@ protected void onResume()
fetchProgress();
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main_menu, menu);
super.onCreateOptionsMenu(menu);

return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
case R.id.menu_item_settings:
Intent launch = new Intent(this, SettingsActivity.class);
startActivity(launch);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

/**
* Click handler for the "Reference" button.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.meyfa.statuscodestrainer.activities.settings;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class SettingsActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

// load the fragment as the main content
getFragmentManager().beginTransaction()//
.replace(android.R.id.content, new SettingsFragment())//
.commit();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package net.meyfa.statuscodestrainer.activities.settings;

import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;

import net.meyfa.statuscodestrainer.R;
import net.meyfa.statuscodestrainer.logic.progress.ProgressTracker;
import net.meyfa.statuscodestrainer.util.async.ActivityActionRunner;
import net.meyfa.statuscodestrainer.util.async.AsyncAction;
import net.meyfa.statuscodestrainer.util.async.Callback;

public class SettingsFragment extends PreferenceFragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);

// clear progress
Preference clearProgress = findPreference(getString(R.string.preference_clear_progress));
clearProgress.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener()
{
@Override
public boolean onPreferenceClick(Preference preference)
{
showClearConfirmation();
return true;
}
});
}

private void showClearConfirmation()
{
new AlertDialog.Builder(getActivity())//
.setTitle(R.string.clear_progress_title)//
.setMessage(R.string.clear_progress_message)//
.setIcon(android.R.drawable.ic_dialog_alert)//
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
clearProgress();
}
})//
.setNegativeButton(android.R.string.no, null)//
.show();
}

private void clearProgress()
{
new ActivityActionRunner<>(getActivity(), new AsyncAction<Void>()
{
@Override
protected Void execute()
{
ProgressTracker tracker = ProgressTracker.getInstance(getActivity().getApplicationContext());
tracker.clearItems();

return null;
}
}).run(new Callback<Void>()
{
@Override
public void done(Void aVoid)
{
Toast.makeText(getActivity(), R.string.clear_progress_toast, Toast.LENGTH_SHORT).show();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ public class ProgressTracker
{
private static volatile ProgressTracker instance;

private final Context appContext;
private final ProgressDatabase db;

private ProgressTracker(@NonNull Context appContext)
{
this.appContext = appContext;
this.db = Room.databaseBuilder(appContext, ProgressDatabase.class, "progressdb").build();
}

Expand Down Expand Up @@ -73,8 +71,16 @@ public ProgressItem createProgressItem(DateTime dateTime, int questionCount, int
item.setQuestionCount(questionCount);
item.setCorrectCount(correctCount);

db.progressItemDao().insertAll(item);
db.progressItemDao().insert(item);

return item;
}

/**
* Deletes all progress items from the database.
*/
public void clearItems()
{
db.progressItemDao().clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public interface ProgressItemDao
List<ProgressItem> getAll();

@Insert
void insertAll(ProgressItem... items);
void insert(ProgressItem item);

@Delete
void deleteAll(ProgressItem... items);
@Query("DELETE FROM progressitem")
void clear();
}
10 changes: 10 additions & 0 deletions app/src/main/res/menu/main_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/menu_item_settings"
android:title="@string/main_menu_item_settings"
app:showAsAction="never" />

</menu>
7 changes: 7 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@
<string name="test_results_title">HTTP Status Test Results</string>
<string name="test_results_total">%d / %d</string>
<string name="test_results_incorrect">Your answer: %s</string>
<string name="main_menu_item_settings">Settings</string>
<string name="settings_title">Settings</string>
<string name="preference_clear_progress">clear_progress</string>
<string name="preference_clear_progress_label">Clear progress</string>
<string name="clear_progress_title">Clear progress?</string>
<string name="clear_progress_message">This will empty the progress database. Are you sure?</string>
<string name="clear_progress_toast">Progress cleared</string>
</resources>
7 changes: 7 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@

</style>

<style name="AppTheme.ActionBar" parent="AppTheme">

<item name="windowActionBar">true</item>
<item name="windowNoTitle">false</item>

</style>

</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:key="@string/preference_clear_progress"
android:title="@string/preference_clear_progress_label" />
</PreferenceScreen>

0 comments on commit c45c5ae

Please sign in to comment.