Project is done, need to refactor some

This commit is contained in:
minhtrannhat 2023-09-16 20:30:45 -04:00
parent 7b4d0433ba
commit bafdbbe5a7
Signed by: minhtrannhat
GPG Key ID: E13CFA85C53F8062
8 changed files with 179 additions and 68 deletions

View File

@ -15,10 +15,12 @@
<activity <activity
android:name=".dataActivity" android:name=".dataActivity"
android:parentActivityName=".MainActivity" android:parentActivityName=".MainActivity"
android:label="Data Activity"
android:exported="false" /> android:exported="false" />
<activity <activity
android:name=".SettingsActivity" android:name=".SettingsActivity"
android:parentActivityName=".MainActivity" android:parentActivityName=".MainActivity"
android:label="Settings Activity"
android:exported="false" /> android:exported="false" />
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"

View File

@ -5,7 +5,7 @@ import android.content.SharedPreferences;
public class CountSharedPreferenceHelper { public class CountSharedPreferenceHelper {
private SharedPreferences sharedPreferences; private final SharedPreferences sharedPreferences;
public CountSharedPreferenceHelper(Context context) { public CountSharedPreferenceHelper(Context context) {
sharedPreferences = context.getSharedPreferences("Count", Context.MODE_PRIVATE); sharedPreferences = context.getSharedPreferences("Count", Context.MODE_PRIVATE);
@ -21,13 +21,31 @@ public class CountSharedPreferenceHelper {
return sharedPreferences.getInt("MaximumCount", 5); return sharedPreferences.getInt("MaximumCount", 5);
} }
public void saveCurrentCount(int current_count) { public int getCurrentCount() {
return sharedPreferences.getString("counterButtonEventsList", "").length();
}
public void setEventsList(String buttonEventsList) {
SharedPreferences.Editor editor = sharedPreferences.edit(); SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("CurrentCount", current_count); editor.putString("counterButtonEventsList", buttonEventsList);
editor.apply(); editor.apply();
} }
public int getCurrentCount() { public String getEventsList() {
return sharedPreferences.getInt("CurrentCount", 0); return sharedPreferences.getString("counterButtonEventsList", "");
}
public String getNumberOfEventInEventsList(char eventNum) {
String eventsList = sharedPreferences.getString("counterButtonEventsList", "");
int count = 0; // Initialize a counter to 0
for (int i = 0; i < eventsList.length(); i++) {
char currentChar = eventsList.charAt(i);
if (currentChar == eventNum) {
count++; // Increment the counter if the current character matches the target character
}
}
return Integer.toString(count);
} }
} }

View File

@ -7,7 +7,7 @@ import android.content.SharedPreferences;
public class CounterButtonSharedPreferenceHelper { public class CounterButtonSharedPreferenceHelper {
private SharedPreferences sharedPreferences; private final SharedPreferences sharedPreferences;
public CounterButtonSharedPreferenceHelper(Context context) { public CounterButtonSharedPreferenceHelper(Context context) {
sharedPreferences = context.getSharedPreferences("counter_button", Context.MODE_PRIVATE); sharedPreferences = context.getSharedPreferences("counter_button", Context.MODE_PRIVATE);
@ -20,17 +20,6 @@ public class CounterButtonSharedPreferenceHelper {
} }
public String getCounterButtonName(CounterButton counterButton) { public String getCounterButtonName(CounterButton counterButton) {
return sharedPreferences.getString(counterButton.getCounterButton(), null); return sharedPreferences.getString(counterButton.getCounterButton(), "");
} }
public void saveCounterButtonCount(CounterButton counterButton, String eventButtonName){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(counterButton.getCounterButton() + "_count", eventButtonName);
editor.apply();
}
public int getCounterButtonCount(CounterButton counterButton) {
return sharedPreferences.getInt(counterButton.getCounterButton() + "_count", -1);
}
} }

View File

@ -2,6 +2,7 @@ package com.example.assignment1;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
@ -62,35 +63,33 @@ public class MainActivity extends AppCompatActivity {
firstCounterButton.setOnClickListener(new View.OnClickListener() { firstCounterButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
incrementCounter(); incrementCounter("1");
} }
}); });
secondCounterButton.setOnClickListener(new View.OnClickListener() { secondCounterButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
incrementCounter(); incrementCounter("2");
} }
}); });
thirdCounterButton.setOnClickListener(new View.OnClickListener() { thirdCounterButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
incrementCounter(); incrementCounter("3");
} }
}); });
} }
@Override @Override
protected void onStart() protected void onStart() {
{
super.onStart(); super.onStart();
if( if (counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.FIRST).trim().isEmpty() ||
counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.FIRST) == null || counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.SECOND).trim().isEmpty() ||
counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.SECOND) == null || counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.THIRD).trim().isEmpty()) {
counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.THIRD) == null) { Toast.makeText(getApplicationContext(), "Event Button(s) can not be empty", Toast.LENGTH_SHORT).show();
goToSettingsActivity(); goToSettingsActivity();
} else { } else {
@ -112,17 +111,18 @@ public class MainActivity extends AppCompatActivity {
startActivity(intent); startActivity(intent);
} }
private void incrementCounter(){ private void incrementCounter(String counterNumber) {
try { try {
if (Integer.parseInt(counter.getText().toString()) < countSharedPreferenceHelper.getMaxCount()) { if (Integer.parseInt(counter.getText().toString()) < countSharedPreferenceHelper.getMaxCount()) {
countSharedPreferenceHelper.setEventsList(countSharedPreferenceHelper.getEventsList() + counterNumber);
Log.d("MainActivity", countSharedPreferenceHelper.getEventsList());
String currentValue = counter.getText().toString(); String currentValue = counter.getText().toString();
int intValue = Integer.parseInt(currentValue); int intValue = Integer.parseInt(currentValue);
int newValue = intValue + 1; int newValue = intValue + 1;
counter.setText(String.valueOf(newValue)); counter.setText(String.valueOf(newValue));
} else {
countSharedPreferenceHelper.saveCurrentCount(newValue);
}
else {
Toast.makeText(getApplicationContext(), "Maximum count has been reached", Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), "Maximum count has been reached", Toast.LENGTH_SHORT).show();
} }
} catch (NumberFormatException e) { } catch (NumberFormatException e) {

View File

@ -59,15 +59,10 @@ public class SettingsActivity extends AppCompatActivity {
} }
@Override @Override
protected void onStart() protected void onStart() {
{
super.onStart(); super.onStart();
if( if (counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.FIRST).equals("") || counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.SECOND).equals("") || counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.THIRD).equals("")) {
counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.FIRST) == null ||
counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.SECOND) == null ||
counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.THIRD) == null ||
countSharedPrefHelper.getMaxCount() == -1) {
goToSettingsActivityEditMode(); goToSettingsActivityEditMode();
@ -80,12 +75,12 @@ public class SettingsActivity extends AppCompatActivity {
} }
// handles edit option
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId(); int id = item.getItemId();
if (id == R.id.edit_settings) { if (id == R.id.edit_settings) {
// Handle the settings item click (e.g., open a settings activity).
goToSettingsActivityEditMode(); goToSettingsActivityEditMode();
return true; return true;
} }
@ -125,6 +120,11 @@ public class SettingsActivity extends AppCompatActivity {
counter_3_name_edit_text.setText(counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.THIRD)); counter_3_name_edit_text.setText(counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.THIRD));
maximum_counts_edit_text.setText(String.format(Integer.toString(countSharedPrefHelper.getMaxCount()))); maximum_counts_edit_text.setText(String.format(Integer.toString(countSharedPrefHelper.getMaxCount())));
String curr_text1 = counter_1_name_edit_text.getText().toString();
String curr_text2 = counter_2_name_edit_text.getText().toString();
String curr_text3 = counter_3_name_edit_text.getText().toString();
int curr_maximum_count = Integer.parseInt(maximum_counts_edit_text.getText().toString());
saveButton.setOnClickListener(new View.OnClickListener() { saveButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
@ -141,6 +141,16 @@ public class SettingsActivity extends AppCompatActivity {
counterButtonSharedPrefHelper.saveCounterButtonName(CounterButton.THIRD, text3); counterButtonSharedPrefHelper.saveCounterButtonName(CounterButton.THIRD, text3);
countSharedPrefHelper.saveMaxCount(maximum_count); countSharedPrefHelper.saveMaxCount(maximum_count);
// if the fields been changed, we reset the count and the events list
if (
!(curr_text1.equals(text1)) ||
!(curr_text2.equals(text2)) ||
!(curr_text3.equals(text3)) ||
!(maximum_count == curr_maximum_count)
){
countSharedPrefHelper.setEventsList("");
}
goToSettingsActivityDisplayMode(); goToSettingsActivityDisplayMode();
} }
} }

View File

@ -1,16 +1,28 @@
package com.example.assignment1; package com.example.assignment1;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import android.os.Bundle; import java.util.Arrays;
import android.view.Menu; import java.util.HashMap;
import android.widget.TextView; import java.util.Map;
public class dataActivity extends AppCompatActivity { public class dataActivity extends AppCompatActivity {
protected Toolbar myToolbar; protected Toolbar myToolbar;
protected TextView firstCounterButtonName;
protected TextView secondCounterButtonName;
protected TextView thirdCounterButtonName;
protected TextView firstCounter; protected TextView firstCounter;
protected TextView secondCounter; protected TextView secondCounter;
protected TextView thirdCounter; protected TextView thirdCounter;
@ -20,17 +32,23 @@ public class dataActivity extends AppCompatActivity {
protected CountSharedPreferenceHelper countSharedPrefHelper; protected CountSharedPreferenceHelper countSharedPrefHelper;
protected boolean showingNames;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data); setContentView(R.layout.activity_data);
myToolbar = (Toolbar) findViewById(R.id.data_toolbar); myToolbar = (Toolbar) findViewById(R.id.data_toolbar);
firstCounter = findViewById(R.id.counter_1_data_name); firstCounterButtonName = findViewById(R.id.counter_1_data_name);
secondCounter = findViewById(R.id.counter_2_data_name); secondCounterButtonName = findViewById(R.id.counter_2_data_name);
thirdCounter = findViewById(R.id.counter_3_data_name); thirdCounterButtonName = findViewById(R.id.counter_3_data_name);
currentCounter = findViewById(R.id.allCounter); currentCounter = findViewById(R.id.allCounter);
firstCounter = findViewById(R.id.firstEventCounter);
secondCounter = findViewById(R.id.secondEventCounter);
thirdCounter = findViewById(R.id.thirdEventCounter);
counterButtonSharedPrefHelper = new CounterButtonSharedPreferenceHelper(dataActivity.this); counterButtonSharedPrefHelper = new CounterButtonSharedPreferenceHelper(dataActivity.this);
countSharedPrefHelper = new CountSharedPreferenceHelper(dataActivity.this); countSharedPrefHelper = new CountSharedPreferenceHelper(dataActivity.this);
@ -44,14 +62,73 @@ public class dataActivity extends AppCompatActivity {
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
firstCounter.setText(counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.FIRST)); firstCounterButtonName.setText(counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.FIRST));
secondCounter.setText(counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.SECOND)); secondCounterButtonName.setText(counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.SECOND));
thirdCounter.setText(counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.THIRD)); thirdCounterButtonName.setText(counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.THIRD));
currentCounter.setText(String.format(Integer.toString(countSharedPrefHelper.getCurrentCount()))); currentCounter.setText(String.format(Integer.toString(countSharedPrefHelper.getCurrentCount())));
firstCounter.setText(countSharedPrefHelper.getNumberOfEventInEventsList('1'));
secondCounter.setText(countSharedPrefHelper.getNumberOfEventInEventsList('2'));
thirdCounter.setText(countSharedPrefHelper.getNumberOfEventInEventsList('3'));
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, constructListViewWithNames());
ListView listView = findViewById(R.id.eventsListView);
listView.setAdapter(adapter);
// to toggle the events name
showingNames = false;
} }
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.data_activity_menu, menu); getMenuInflater().inflate(R.menu.data_activity_menu, menu);
return true; return true;
} }
// handles when no event names are toggled
public boolean onOptionsItemSelected(MenuItem item) {
Log.d("DATA_ACTIVITY", "CLICKED THE TOGGLE");
int id = item.getItemId();
if (id == R.id.toggle_event_names) {
ArrayAdapter<String> adapter;
ListView listView;
if (showingNames) {
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, constructListViewWithNames());
listView = findViewById(R.id.eventsListView);
} else {
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, constructListViewWithoutNames());
listView = findViewById(R.id.eventsListView);
}
listView.setAdapter(adapter);
showingNames = !showingNames;
return true;
}
return super.onOptionsItemSelected(item);
}
private String[] constructListViewWithNames() {
Map<String, String> replacements = new HashMap<>();
replacements.put("1", counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.FIRST));
replacements.put("2", counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.SECOND));
replacements.put("3", counterButtonSharedPrefHelper.getCounterButtonName(CounterButton.THIRD));
return Arrays.stream(constructListViewWithoutNames()).map(value -> replacements.getOrDefault(value, value)).toArray(String[]::new);
}
private String[] constructListViewWithoutNames() {
return countSharedPrefHelper.getEventsList().chars().mapToObj(c -> String.valueOf((char) c)).toArray(String[]::new);
}
} }

View File

@ -34,21 +34,25 @@ tools:context=".SettingsActivity">
android:id="@+id/counter_1_data_name" android:id="@+id/counter_1_data_name"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:textSize="24sp"
/> />
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:textSize="24sp"
android:text="@string/colon"/> android:text="@string/colon"/>
<TextView <TextView
android:id="@+id/firstEventCounter" android:id="@+id/firstEventCounter"
android:textSize="24sp"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent"/> android:layout_height="match_parent"/>
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:textSize="24sp"
android:text="@string/events"/> android:text="@string/events"/>
</LinearLayout> </LinearLayout>
@ -62,22 +66,26 @@ tools:context=".SettingsActivity">
<TextView <TextView
android:id="@+id/counter_2_data_name" android:id="@+id/counter_2_data_name"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:textSize="24sp"
android:layout_height="match_parent" android:layout_height="match_parent"
/> />
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:textSize="24sp"
android:text="@string/colon"/> android:text="@string/colon"/>
<TextView <TextView
android:id="@+id/secondEventCounter" android:id="@+id/secondEventCounter"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:textSize="24sp"
android:layout_height="match_parent"/> android:layout_height="match_parent"/>
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:textSize="24sp"
android:text="@string/events"/> android:text="@string/events"/>
</LinearLayout> </LinearLayout>
@ -92,21 +100,25 @@ tools:context=".SettingsActivity">
android:id="@+id/counter_3_data_name" android:id="@+id/counter_3_data_name"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:textSize="24sp"
/> />
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:textSize="24sp"
android:text="@string/colon"/> android:text="@string/colon"/>
<TextView <TextView
android:id="@+id/thirdEventCounter" android:id="@+id/thirdEventCounter"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:textSize="24sp"
android:layout_height="match_parent"/> android:layout_height="match_parent"/>
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:textSize="24sp"
android:text="@string/events"/> android:text="@string/events"/>
</LinearLayout> </LinearLayout>
@ -121,21 +133,24 @@ tools:context=".SettingsActivity">
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:textSize="24sp"
android:text="@string/total_events" android:text="@string/total_events"
/> />
<TextView <TextView
android:id="@+id/allCounter" android:id="@+id/allCounter"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:textSize="24sp"
android:layout_height="match_parent"/> android:layout_height="match_parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/events"/>
</LinearLayout> </LinearLayout>
<!-- Events List View--> <!-- Events List View-->
<ListView
android:id="@+id/eventsListView"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout> </LinearLayout>
</RelativeLayout> </RelativeLayout>

View File

@ -15,7 +15,7 @@
<string name="maximum_counts_text">Maximum Counts</string> <string name="maximum_counts_text">Maximum Counts</string>
<string name="maximum_counts"/> <string name="maximum_counts"/>
<string name="toggle_event_names">Toggle Event Names</string> <string name="toggle_event_names">Toggle Event Names</string>
<string name="colon">:</string> <string name="colon">": "</string>
<string name="events">" events"</string> <string name="events">" events"</string>
<string name="total_events">"Total events: "</string> <string name="total_events">"Total events: "</string>
</resources> </resources>