Skip to content

Commit

Permalink
moved functionality of RevengeDialogFactory to RevengeAlertDialogBuil…
Browse files Browse the repository at this point in the history
…derAdapter

Signed-off-by: PeterNaggschga <rep.akhcsztan@gmail.com>
  • Loading branch information
PeterNaggschga committed Jun 2, 2024
1 parent 25c30cf commit 78bff4a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
Expand Down Expand Up @@ -72,7 +71,7 @@ public static Completable remove(@NonNull Context context, @NonNull UnitReposito

/**
* Creates a Dialog asking whether the Ability#REVENGE ability should be activated.
* The Dialog is created using RevengeDialogFactory.
* The Dialog is created using an RevengeAlertDialogBuilderAdapter.
*
* @param context Context of the shown Dialog.
* @param repository UnitRepository where the UnitEntity objects are removed and avengers are inserted.
Expand All @@ -81,24 +80,23 @@ public static Completable remove(@NonNull Context context, @NonNull UnitReposito
* @param units Collection of UnitEntity objects that are removed.
* @param revengeUnits Long representing the number of revenge units.
* @return A Dialog asking whether the Ability#REVENGE ability should be activated.
* @see RevengeDialogFactory#getRevengeDialog(Context, DialogInterface.OnClickListener, DialogInterface.OnClickListener)
* @see RevengeDialogFactory#insertAvengers(UnitRepository, int)
* @see RevengeAlertDialogBuilderAdapter#insertAvengers(UnitRepository, int)
*/
@NonNull
private static Dialog getRevengeDialog(@NonNull Context context, @NonNull UnitRepository repository,
@NonNull CompletableEmitter emitter, @NonNull Collection<UnitEntity> units,
@IntRange(from = 1) int revengeUnits) {
return RevengeDialogFactory.getRevengeDialog(context,
(dialog, which) -> {
return new RevengeAlertDialogBuilderAdapter(context)
.setPositiveCallback((dialog, which) -> {
// noinspection CheckResult, ResultOfMethodCallIgnored
repository.delete(units)
.andThen(RevengeDialogFactory.insertAvengers(repository, revengeUnits))
.andThen(RevengeAlertDialogBuilderAdapter.insertAvengers(repository, revengeUnits))
.subscribe(emitter::onComplete);
},
((dialog, which) -> {
})
.setNegativeCallback(((dialog, which) -> {
// noinspection CheckResult, ResultOfMethodCallIgnored
repository.delete(units).subscribe(emitter::onComplete);
})
);
}))
.create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
Expand Down Expand Up @@ -76,7 +75,7 @@ private static Optional<UnitEntity> getRandomUnit(@NonNull List<UnitEntity> unit

/**
* Creates a Dialog asking whether the Ability#REVENGE ability should be activated.
* The Dialog is created using RevengeDialogFactory.
* The Dialog is created using an RevengeAlertDialogBuilderAdapter.
*
* @param context Context of the shown Dialog.
* @param repository UnitRepository where the UnitEntity objects are removed and avengers are inserted.
Expand All @@ -85,25 +84,24 @@ private static Optional<UnitEntity> getRandomUnit(@NonNull List<UnitEntity> unit
* @param keptUnit UnitEntity that should be kept.
* @param revengeUnits Long representing the number of revenge units.
* @return A Dialog asking whether the Ability#REVENGE ability should be activated.
* @see RevengeDialogFactory#getRevengeDialog(Context, DialogInterface.OnClickListener, DialogInterface.OnClickListener)
* @see RevengeDialogFactory#insertAvengers(UnitRepository, int)
* @see RevengeAlertDialogBuilderAdapter#insertAvengers(UnitRepository, int)
*/
@NonNull
private static Dialog getRevengeDialog(@NonNull Context context, @NonNull UnitRepository repository,
@NonNull CompletableEmitter emitter, @Nullable UnitEntity keptUnit,
@IntRange(from = 1) int revengeUnits) {
return RevengeDialogFactory.getRevengeDialog(context,
(dialogInterface, which) -> {
return new RevengeAlertDialogBuilderAdapter(context)
.setPositiveCallback((dialogInterface, which) -> {
// noinspection CheckResult, ResultOfMethodCallIgnored
repository.reset(keptUnit)
.andThen(RevengeDialogFactory.insertAvengers(repository, revengeUnits))
.andThen(RevengeAlertDialogBuilderAdapter.insertAvengers(repository, revengeUnits))
.subscribe(emitter::onComplete);
},
((dialog, which) -> {
})
.setNegativeCallback(((dialog, which) -> {
// noinspection CheckResult, ResultOfMethodCallIgnored
repository.reset(keptUnit).subscribe(emitter::onComplete);
})
);
}))
.create();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.peternaggschga.gwent.domain.cases;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;

Expand All @@ -17,14 +16,12 @@
import io.reactivex.rxjava3.core.Completable;

/**
* A factory class providing a Dialog asking whether the Ability#REVENGE ability should be activated
* and uses the given Callbacks in #getRevengeDialog().
* An adapter class adapting AlertDialog.Builder to provide an interface
* for creating an AlertDialog asking the user whether they want to invoke the Ability#REVENGE ability.
*
* @todo Restructure as an adapter class for AlertDialog.Builder (see ResetAlertDialogBuilderAdapter)
* @see Ability#REVENGE
* @see #getRevengeDialog(Context, DialogInterface.OnClickListener, DialogInterface.OnClickListener)
* @todo Add testing.
*/
class RevengeDialogFactory {
class RevengeAlertDialogBuilderAdapter {
/**
* Boolean constant defining
* whether the default UnitEntity summoned by the Ability#REVENGE ability is epic.
Expand Down Expand Up @@ -71,27 +68,25 @@ class RevengeDialogFactory {
private static final RowType AVENGER_ROW = RowType.MELEE;

/**
* Creates a Dialog
* asking whether the Ability#REVENGE ability of a removed UnitEntity should be activated.
* The onPositiveClickListener should call #insertAvengers().
*
* @param context Context of the created Dialog.
* @param onPositiveClickListener DialogInterface.OnClickListener which is called when the positive button is clicked.
* @param onNegativeClickListener DialogInterface.OnClickListener which is called when the negative button is clicked.
* @return A Dialog asking about the activation of the Ability#REVENGE ability.
* @see #insertAvengers(UnitRepository, int)
* AlertDialog.Builder that is adapted by this class.
*/
@NonNull
public static Dialog getRevengeDialog(@NonNull Context context, @NonNull DialogInterface.OnClickListener onPositiveClickListener,
@NonNull DialogInterface.OnClickListener onNegativeClickListener) {
return new AlertDialog.Builder(context)
private final AlertDialog.Builder adapteeBuilder;

/**
* Constructor of a RevengeAlertDialogBuilderAdapter.
* Initializes the buttons with empty callbacks.
*
* @param context Context of the built AlertDialog.
*/
RevengeAlertDialogBuilderAdapter(@NonNull Context context) {
this.adapteeBuilder = new AlertDialog.Builder(context)
.setIconAttribute(android.R.attr.alertDialogIcon)
.setTitle(R.string.alertDialog_revenge_title)
.setMessage(R.string.alertDialog_revenge_msg)
.setCancelable(false)
.setPositiveButton(R.string.alertDialog_revenge_positive, onPositiveClickListener)
.setNegativeButton(R.string.alertDialog_revenge_negative, onNegativeClickListener)
.create();
.setPositiveButton(R.string.alertDialog_revenge_positive, (dialog, which) -> dialog.cancel())
.setNegativeButton(R.string.alertDialog_revenge_negative, (dialog, which) -> dialog.cancel());
}

/**
Expand All @@ -107,4 +102,41 @@ public static Dialog getRevengeDialog(@NonNull Context context, @NonNull DialogI
public static Completable insertAvengers(@NonNull UnitRepository repository, @IntRange(from = 0) int numberOfAvengers) {
return repository.insertUnit(AVENGER_EPIC, AVENGER_DAMAGE, AVENGER_ABILITY, AVENGER_SQUAD, AVENGER_ROW, numberOfAvengers);
}

/**
* Creates an AlertDialog with the arguments supplied to this builder.
* Basically just calls AlertDialog.Builder#create() on #adapteeBuilder.
*
* @see AlertDialog.Builder#create()
*/
@NonNull
AlertDialog create() {
return adapteeBuilder.create();
}

/**
* Adds the given callback to the positive button of the built Dialog.
* Callback should call insertAvengers().
*
* @param onPositiveButtonClick DialogInterface.OnClickListener that is called, when the positive button is clicked.
* @return The RevengeAlertDialogBuilder with the updated positive callback.
* @see #insertAvengers(UnitRepository, int)
*/
@NonNull
RevengeAlertDialogBuilderAdapter setPositiveCallback(@NonNull DialogInterface.OnClickListener onPositiveButtonClick) {
adapteeBuilder.setPositiveButton(R.string.alertDialog_revenge_positive, onPositiveButtonClick);
return this;
}

/**
* Adds the given callback to the negative button of the built Dialog.
*
* @param onNegativeButtonClick DialogInterface.OnClickListener that is called, when the negative button is clicked.
* @return The RevengeAlertDialogBuilder with the updated negative callback.
*/
@NonNull
RevengeAlertDialogBuilderAdapter setNegativeCallback(@NonNull DialogInterface.OnClickListener onNegativeButtonClick) {
adapteeBuilder.setNegativeButton(R.string.alertDialog_revenge_negative, onNegativeButtonClick);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class RevengeDialogFactoryUnitTest {
public class RevengeAlertDialogBuilderAdapterUnitTest {
private static final int TESTING_DEPTH = 50;

@Test
public void insertAvengersInsertsUnits() {
UnitRepository repository = mock(UnitRepository.class);
for (int numberOfAvengers = 0; numberOfAvengers < TESTING_DEPTH; numberOfAvengers++) {
RevengeDialogFactory.insertAvengers(repository, numberOfAvengers);
RevengeAlertDialogBuilderAdapter.insertAvengers(repository, numberOfAvengers);
verify(repository, atLeastOnce()).insertUnit(anyBoolean(), anyInt(), any(), any(), any(), eq(numberOfAvengers));
}
}
Expand Down

0 comments on commit 78bff4a

Please sign in to comment.