mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-07 01:28:52 -06:00
Add action for randomizing habit history (dev mode)
This commit is contained in:
@@ -24,6 +24,7 @@ import android.view.*;
|
|||||||
|
|
||||||
import org.isoron.androidbase.activities.*;
|
import org.isoron.androidbase.activities.*;
|
||||||
import org.isoron.uhabits.*;
|
import org.isoron.uhabits.*;
|
||||||
|
import org.isoron.uhabits.core.preferences.Preferences;
|
||||||
import org.isoron.uhabits.core.ui.screens.habits.show.*;
|
import org.isoron.uhabits.core.ui.screens.habits.show.*;
|
||||||
|
|
||||||
import javax.inject.*;
|
import javax.inject.*;
|
||||||
@@ -35,13 +36,26 @@ public class ShowHabitsMenu extends BaseMenu
|
|||||||
{
|
{
|
||||||
@NonNull
|
@NonNull
|
||||||
private Lazy<ShowHabitMenuBehavior> behavior;
|
private Lazy<ShowHabitMenuBehavior> behavior;
|
||||||
|
@NonNull
|
||||||
|
private final Preferences prefs;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public ShowHabitsMenu(@NonNull BaseActivity activity,
|
public ShowHabitsMenu(@NonNull BaseActivity activity,
|
||||||
@NonNull Lazy<ShowHabitMenuBehavior> behavior)
|
@NonNull Lazy<ShowHabitMenuBehavior> behavior,
|
||||||
|
@NonNull Preferences prefs)
|
||||||
{
|
{
|
||||||
super(activity);
|
super(activity);
|
||||||
this.behavior = behavior;
|
this.behavior = behavior;
|
||||||
|
this.prefs = prefs;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate(@NonNull Menu menu)
|
||||||
|
{
|
||||||
|
super.onCreate(menu);
|
||||||
|
|
||||||
|
if (prefs.isDeveloper())
|
||||||
|
menu.findItem(R.id.action_randomize).setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -61,6 +75,10 @@ public class ShowHabitsMenu extends BaseMenu
|
|||||||
behavior.get().onDeleteHabit();
|
behavior.get().onDeleteHabit();
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
case R.id.action_randomize:
|
||||||
|
behavior.get().onRandomize();
|
||||||
|
return true;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,4 +37,10 @@
|
|||||||
android:title="@string/edit"
|
android:title="@string/edit"
|
||||||
app:showAsAction="ifRoom"/>
|
app:showAsAction="ifRoom"/>
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/action_randomize"
|
||||||
|
android:title="Randomize"
|
||||||
|
android:visible="false"
|
||||||
|
app:showAsAction="never"/>
|
||||||
|
|
||||||
</menu>
|
</menu>
|
||||||
@@ -25,12 +25,16 @@ import org.isoron.uhabits.core.commands.*;
|
|||||||
import org.isoron.uhabits.core.models.*;
|
import org.isoron.uhabits.core.models.*;
|
||||||
import org.isoron.uhabits.core.tasks.*;
|
import org.isoron.uhabits.core.tasks.*;
|
||||||
import org.isoron.uhabits.core.ui.callbacks.*;
|
import org.isoron.uhabits.core.ui.callbacks.*;
|
||||||
|
import org.isoron.uhabits.core.utils.*;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import javax.inject.*;
|
import javax.inject.*;
|
||||||
|
|
||||||
|
import static java.lang.Math.*;
|
||||||
|
|
||||||
|
|
||||||
public class ShowHabitMenuBehavior
|
public class ShowHabitMenuBehavior
|
||||||
{
|
{
|
||||||
private HabitList habitList;
|
private HabitList habitList;
|
||||||
@@ -95,6 +99,27 @@ public class ShowHabitMenuBehavior
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void onRandomize()
|
||||||
|
{
|
||||||
|
Random random = new Random();
|
||||||
|
habit.getRepetitions().removeAll();
|
||||||
|
double strength = 50;
|
||||||
|
|
||||||
|
for (int i = 0; i < 365 * 5; i++)
|
||||||
|
{
|
||||||
|
if (i % 7 == 0) strength = max(0, min(100, strength + 10 * random.nextGaussian()));
|
||||||
|
if (random.nextInt(100) > strength) continue;
|
||||||
|
|
||||||
|
int value = 1;
|
||||||
|
if (habit.isNumerical())
|
||||||
|
value = (int) (1000 + 250 * random.nextGaussian() * strength / 100) * 1000;
|
||||||
|
|
||||||
|
habit.getRepetitions().add(new Repetition(DateUtils.getToday().minus(i), value));
|
||||||
|
}
|
||||||
|
|
||||||
|
habit.invalidateNewerThan(Timestamp.ZERO);
|
||||||
|
}
|
||||||
|
|
||||||
public enum Message
|
public enum Message
|
||||||
{
|
{
|
||||||
COULD_NOT_EXPORT, HABIT_DELETED
|
COULD_NOT_EXPORT, HABIT_DELETED
|
||||||
|
|||||||
Reference in New Issue
Block a user