Implemented shared button helpers for buttons
This commit is contained in:
parent
fb497cad3b
commit
3d9f175ce0
1
.idea/.name
generated
Normal file
1
.idea/.name
generated
Normal file
@ -0,0 +1 @@
|
||||
assignment1
|
@ -1,7 +1,17 @@
|
||||
package com.example.assignment1;
|
||||
|
||||
public enum EventButton{
|
||||
FIRST,
|
||||
SECOND,
|
||||
THIRD
|
||||
}
|
||||
public enum EventButton {
|
||||
FIRST("FirstEventButton"),
|
||||
SECOND("SecondEventButton"),
|
||||
THIRD("ThirdEventButton");
|
||||
|
||||
private String url;
|
||||
|
||||
EventButton(String envUrl) {
|
||||
this.url = envUrl;
|
||||
}
|
||||
|
||||
public String getEventButton() {
|
||||
return url;
|
||||
}
|
||||
}
|
@ -10,16 +10,16 @@ public class EventButtonSharedPreferenceHelper {
|
||||
private SharedPreferences sharedPreferences;
|
||||
|
||||
public EventButtonSharedPreferenceHelper(Context context, EventButton eventButton) {
|
||||
sharedPreferences = context.getSharedPreferences("FirstEventButtonName", Context.MODE_PRIVATE);
|
||||
sharedPreferences = context.getSharedPreferences(eventButton.getEventButton(), Context.MODE_PRIVATE);
|
||||
}
|
||||
|
||||
public void saveEventButtonName(String eventButtonName) {
|
||||
public void saveEventButtonName(EventButton eventButton, String eventButtonName) {
|
||||
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||
editor.putString("FirstEventButtonName", eventButtonName);
|
||||
editor.putString(eventButton.getEventButton(), eventButtonName);
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
public String getEventButtonName() {
|
||||
return sharedPreferences.getString("FirstEventButtonName", null);
|
||||
public String getEventButtonName(EventButton eventButton) {
|
||||
return sharedPreferences.getString(eventButton.getEventButton(), null);
|
||||
}
|
||||
}
|
@ -9,12 +9,22 @@ import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
protected EventButtonSharedPreferenceHelper firstEventButton;
|
||||
protected EventButtonSharedPreferenceHelper secondEventButton;
|
||||
protected EventButtonSharedPreferenceHelper thirdEventButton;
|
||||
|
||||
protected Button settingsButton = null;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
Button settingsButton = findViewById(R.id.settings_button);
|
||||
settingsButton = findViewById(R.id.settings_button);
|
||||
|
||||
firstEventButton = new EventButtonSharedPreferenceHelper(MainActivity.this, EventButton.FIRST);
|
||||
secondEventButton = new EventButtonSharedPreferenceHelper(MainActivity.this, EventButton.SECOND);
|
||||
thirdEventButton = new EventButtonSharedPreferenceHelper(MainActivity.this, EventButton.THIRD);
|
||||
|
||||
settingsButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
@ -25,6 +35,19 @@ public class MainActivity extends AppCompatActivity {
|
||||
});
|
||||
}
|
||||
|
||||
protected void onStart()
|
||||
{
|
||||
super.onStart();
|
||||
|
||||
if(
|
||||
firstEventButton.getEventButtonName(EventButton.FIRST) == null ||
|
||||
secondEventButton.getEventButtonName(EventButton.SECOND) == null ||
|
||||
thirdEventButton.getEventButtonName(EventButton.THIRD) == null) {
|
||||
|
||||
goToSettingsActivity();
|
||||
}
|
||||
}
|
||||
|
||||
private void goToSettingsActivity() {
|
||||
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
|
||||
startActivity(intent);
|
||||
|
Loading…
x
Reference in New Issue
Block a user