Skip to content

Commit

Permalink
Added: Duplicate alarm.
Browse files Browse the repository at this point in the history
  • Loading branch information
shauleiz committed Mar 9, 2024
1 parent b9a0b05 commit 6c0987f
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 11 deletions.
12 changes: 10 additions & 2 deletions app/src/main/java/com/example/thetimemachine/AlarmViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public void GetValuesFromList(int position){
oneOff.setValue((AlarmList.get(position).isOneOff()));
}

public void GetValuesFromList(AlarmItem item){
public void GetValuesFromList(AlarmItem item, boolean edit){
// Sanity check
if (item == null)
return;
Expand All @@ -210,12 +210,20 @@ public void GetValuesFromList(AlarmItem item){
active.setValue(item.isActive());

AlarmItem ai = item;
long c = ai.getCreateTime();
long c;
if (edit)
c = ai.getCreateTime();
else
c=0;
createTime.setValue(c);

weekDays.setValue(item.getWeekDays());
oneOff.setValue((item.isOneOff()));
}
public void GetValuesFromList(AlarmItem item){
GetValuesFromList( item, true);
}


/* Getter Methods */
public MutableLiveData<Integer> getHour() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void onItemClick(View view, int position) {
String label = alarmList.get(position).getLabel();
String alarmTime = String.format( Locale.US,"%d:%02d", alarmList.get(position).getHour(), alarmList.get(position).getMinute());
Toast.makeText(getContext(), "Alarm Clicked: " + label + ": Time: " + alarmTime, Toast.LENGTH_SHORT).show();
AlarmItemEdit(alarmList.get(position));
AlarmItemEdit(alarmList.get(position),true);
}
}
});
Expand Down Expand Up @@ -302,26 +302,37 @@ void ActiveCheckboxChanged(View view, int position) {


}

/*
* Called when user clicks on Alarm item in recycler or toolbar Edit action
* Copy values from the selected item to be used by the setup fragment
* Create a bundle with data to be passed to the setup fragment
* Replace this fragment by setup fragment
*
* If parameter edit if false - this means that this item should be duplicated
* rather than edited
*/
void AlarmItemEdit(AlarmItem item){
void AlarmItemEdit(AlarmItem item, boolean edit){

if (item == null) return;

// Copy values from the selected item to be used by the setup fragment
parent.alarmViewModel.setUpAlarmValues.GetValuesFromList(item);
parent.alarmViewModel.setUpAlarmValues.GetValuesFromList(item, edit);

// Passing parameters to setup fragment
Bundle b = new Bundle();
b.putBoolean("INIT_NEWALARM",false);

b.putInt("INIT_POSITION", 0);
List<AlarmItem> alarmItems = parent.alarmViewModel.getAlarmList().getValue();
if (alarmItems==null)return;
b.putLong("INIT_CREATE_TIME", item.getCreateTime());
if (edit) {
b.putLong("INIT_CREATE_TIME", item.getCreateTime());
b.putBoolean("INIT_NEWALARM",false);
}
else {
b.putLong("INIT_CREATE_TIME", 0);
b.putBoolean("INIT_NEWALARM",true);
}

// Replace current fragment with the Setup Alarm fragment
parent = (MainActivity) getActivity();
Expand All @@ -333,6 +344,8 @@ void AlarmItemEdit(AlarmItem item){
commit();
}



/* When an item is Long-Clicked:
- The item is selected/deselected (Visually)
- The App bar may take one of the following modes:
Expand Down Expand Up @@ -400,7 +413,12 @@ public void EditSelectedAlarm(){

// Edit only is exactly one item selected
if (selectedItems.size() !=1 ) return;
AlarmItemEdit( parent.alarmViewModel.getAlarmItemById(selectedItems.get(0)));
AlarmItemEdit( parent.alarmViewModel.getAlarmItemById(selectedItems.get(0)), true);
}

public void DuplicateSelectedAlarm(){
// Duplicate only is exactly one item selected
if (selectedItems.size() !=1 ) return;
AlarmItemEdit( parent.alarmViewModel.getAlarmItemById(selectedItems.get(0)), false);
}
}
15 changes: 15 additions & 0 deletions app/src/main/java/com/example/thetimemachine/UI/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ public class MainActivity extends AppCompatActivity {
static final String action_delete = "ACTION_DELETE";
static final String action_edit = "ACTION_EDIT";
static final String action_settings = "ACTION_SETTINGS";
static final String action_duplicate = "ACTION_DUPLICATE";


// ViewModel object of class MyViewModel
// Holds all UI variables related to this activity
public AlarmViewModel alarmViewModel;
private boolean deleteAction = false;
private boolean editAction = false;
private boolean duplicateAction = false;
private boolean settingsAction = true;

public MainActivity() {
Expand All @@ -41,6 +43,7 @@ public void onSaveInstanceState(@NonNull Bundle outState) {
outState.putBoolean(action_delete, deleteAction);
outState.putBoolean(action_edit, editAction);
outState.putBoolean(action_settings, settingsAction);
outState.putBoolean(action_duplicate, duplicateAction);

//Toast.makeText(getApplicationContext(), "MainActivity::onSaveInstanceState Called", Toast.LENGTH_SHORT).show();
}
Expand Down Expand Up @@ -68,6 +71,7 @@ protected void onCreate(Bundle savedInstanceState) {
deleteAction = savedInstanceState.getBoolean(action_delete, false);
editAction = savedInstanceState.getBoolean(action_edit, false);
settingsAction = savedInstanceState.getBoolean(action_settings,false);
duplicateAction = savedInstanceState.getBoolean(action_duplicate,false);
//Toast.makeText(getApplicationContext(), "MainActivity::onCreate Called - deleteAction=" + deleteAction, Toast.LENGTH_SHORT).show();
}

Expand Down Expand Up @@ -111,25 +115,30 @@ public void UpdateOptionMenu(){
if (len == 0){
setDeleteAction(false);
setEditAction( false);
setDuplicateAction(false);
} else if (len == 1) {
setDeleteAction(true);
setEditAction( true);
setDuplicateAction(true);
} else {
setDeleteAction(true);
setEditAction( false);
setDuplicateAction(false);
}
invalidateOptionsMenu();
}

public void setDeleteAction(boolean d){deleteAction = d;}

public void setEditAction(boolean editAction) {this.editAction = editAction;}
public void setDuplicateAction(boolean editAction) {this.duplicateAction = editAction;}

public void setSettingsAction(boolean settingsAction){this.settingsAction = settingsAction;}

public boolean isDeleteAction() {return deleteAction;}

public boolean isEditAction() {return editAction;}
public boolean isDuplicateAction() {return duplicateAction;}

public boolean isSettingsAction() {return settingsAction;}

Expand All @@ -139,6 +148,8 @@ public boolean onPrepareOptionsMenu(Menu menu) {
deleteItem.setVisible(deleteAction);
MenuItem editItem = menu.findItem(R.id.edit);
editItem.setVisible(editAction);
MenuItem duplicateItem = menu.findItem(R.id.duplicate);
duplicateItem.setVisible(duplicateAction);
MenuItem settingsItem = menu.findItem(R.id.settings);
settingsItem.setVisible(settingsAction);

Expand Down Expand Up @@ -168,6 +179,9 @@ else if (frag instanceof AlarmListFragment) {
} else if (itemId == R.id.edit) {
((AlarmListFragment) frag).EditSelectedAlarm();
return true;
} else if (itemId == R.id.duplicate) {
((AlarmListFragment) frag).DuplicateSelectedAlarm();
return true;
} else if (itemId == R.id.settings) {
Settings();
return true;
Expand Down Expand Up @@ -197,6 +211,7 @@ private void Settings(){
setDeleteAction(false);
setEditAction( false);
setSettingsAction(false);
setDuplicateAction(false);

getSupportFragmentManager()
.beginTransaction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
parent.setDeleteAction(false);
parent.setSettingsAction(true);
parent.setEditAction(false);
parent.setDuplicateAction(false);
parent.invalidateOptionsMenu();

// Get instance of SetUpAlarmValues sub-class
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/res/drawable/octicon__duplicate_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?attr/colorPrimaryContainer"
android:pathData="M14.513,6a0.75,0.75 0,0 1,0.75 0.75v2h1.987a0.75,0.75 0,0 1,0 1.5h-1.987v2a0.75,0.75 0,1 1,-1.5 0v-2H11.75a0.75,0.75 0,0 1,0 -1.5h2.013v-2a0.75,0.75 0,0 1,0.75 -0.75"
/>
<path
android:pathData="M7.024,3.75c0,-0.966 0.784,-1.75 1.75,-1.75L20.25,2c0.966,0 1.75,0.784 1.75,1.75v11.498a1.75,1.75 0,0 1,-1.75 1.75L8.774,16.998a1.75,1.75 0,0 1,-1.75 -1.75ZM8.774,3.5a0.25,0.25 0,0 0,-0.25 0.25v11.498c0,0.139 0.112,0.25 0.25,0.25L20.25,15.498a0.25,0.25 0,0 0,0.25 -0.25L20.5,3.75a0.25,0.25 0,0 0,-0.25 -0.25Z"
android:fillColor="?attr/colorPrimaryContainer"/>
<path
android:pathData="M1.995,10.749a1.75,1.75 0,0 1,1.75 -1.751H5.25a0.75,0.75 0,1 1,0 1.5H3.745a0.25,0.25 0,0 0,-0.25 0.25L3.5,20.25c0,0.138 0.111,0.25 0.25,0.25h9.5a0.25,0.25 0,0 0,0.25 -0.25v-1.51a0.75,0.75 0,1 1,1.5 0v1.51A1.75,1.75 0,0 1,13.25 22h-9.5A1.75,1.75 0,0 1,2 20.25z"
android:fillColor="?attr/colorPrimaryContainer"/>
</vector>
12 changes: 11 additions & 1 deletion app/src/main/res/menu/action_bar_basic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:android="http://schemas.android.com/apk/res/android">



<!-- Edit -->
<item
android:id="@+id/edit"
Expand All @@ -19,15 +20,24 @@
android:visible="true"
TheTimeMachine:showAsAction="ifRoom"/>

<!-- Duplicate -->
<item
android:id="@+id/duplicate"
android:icon="@drawable/octicon__duplicate_24"
android:iconTint="?attr/colorPrimaryContainer"
android:title="Duplicate"
android:visible="true"
TheTimeMachine:showAsAction="ifRoom" />

<!-- Settings -->
<item
android:id="@+id/settings"
android:icon="@drawable/settings_fill0_wght400_grad0_opsz24"
android:iconTint="?attr/colorPrimaryContainer"
android:title="settings"
android:title="Settings"
android:visible="true"
TheTimeMachine:showAsAction="always" />



</menu>
9 changes: 9 additions & 0 deletions app/src/main/res/menu/action_bar_multi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
android:visible="true"
TheTimeMachine:showAsAction="ifRoom"/>

<!-- Duplicate -->
<item
android:id="@+id/duplicate"
android:icon="@drawable/octicon__duplicate_24"
android:iconTint="?attr/colorPrimaryContainer"
android:title="Duplicate"
android:visible="true"
TheTimeMachine:showAsAction="ifRoom" />

<!-- Settings -->
<item
android:id="@+id/settings"
Expand Down
15 changes: 13 additions & 2 deletions app/src/main/res/menu/action_bar_single.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,32 @@
<menu xmlns:TheTimeMachine="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Duplicate -->
<item
android:id="@+id/duplicate"
android:icon="@drawable/octicon__duplicate_24"
android:iconTint="?attr/colorPrimaryContainer"
android:title="Duplicate"
android:visible="true"
TheTimeMachine:showAsAction="always" />

<!-- Edit -->
<item
android:id="@+id/edit"
android:icon="@drawable/edit_fill0_wght400_grad0_opsz24"
android:title="edit"
android:visible="true"
TheTimeMachine:showAsAction="ifRoom"/>
TheTimeMachine:showAsAction="always"/>

<!-- Delete -->
<item
android:id="@+id/delete"
android:icon="@drawable/delete_fill0_wght400_grad0_opsz24"
android:title="@string/delete_alarm"
android:visible="true"
TheTimeMachine:showAsAction="ifRoom"/>
TheTimeMachine:showAsAction="always"/>



<!-- Settings -->
<item
Expand Down

0 comments on commit 6c0987f

Please sign in to comment.