Skip to content

Commit

Permalink
Allow creating a counter without specifying a name
Browse files Browse the repository at this point in the history
Resolves #110.
  • Loading branch information
leinadsened authored Oct 9, 2023
1 parent d724e3e commit 3afd9b9
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions app/src/main/java/me/tsukanov/counter/view/dialogs/AddDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;


import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import me.tsukanov.counter.CounterApplication;
import me.tsukanov.counter.R;
import me.tsukanov.counter.activities.MainActivity;
Expand Down Expand Up @@ -46,15 +51,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
.setPositiveButton(
getResources().getText(R.string.dialog_button_add),
(dialog1, which) -> {
final String name = nameInput.getText().toString().trim();
if (name.isEmpty()) {
Toast.makeText(
getActivity(),
getResources().getText(R.string.toast_no_name_message),
Toast.LENGTH_SHORT)
.show();
return;
}
String name = checkCounterName(nameInput.getText().toString().trim());

int value;
final String valueInputContents = valueInput.getText().toString().trim();
Expand Down Expand Up @@ -94,4 +91,25 @@ private void addCounter(@NonNull final IntegerCounter counter) {
CounterApplication.getComponent().localStorage().write(counter);
new BroadcastHelper(requireContext()).sendSelectCounterBroadcast(counter.getName());
}

private String checkCounterName(String name){
if (name.isEmpty()) {
int counterCount;
String genericName = getString(R.string.app_name) + " ";
boolean runAgain = true;
Set<String> counterNames;
counterNames = CounterApplication.getComponent().localStorage().readAll(false).stream().map(IntegerCounter::getName).collect(Collectors.toSet());
counterCount = counterNames.size() + 1;
while (runAgain) {
runAgain = false;
if (counterNames.contains(genericName + counterCount)){
runAgain = true;
counterCount++;
}
}
name = genericName + counterCount;
}
return name;
}

}

0 comments on commit 3afd9b9

Please sign in to comment.