mirror of https://github.com/iSoron/uhabits.git
parent
50a6c6d9dd
commit
f8a9da59dd
@ -1,99 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.activities.habits.list.controllers;
|
||||
|
||||
import android.support.annotation.*;
|
||||
|
||||
import com.google.auto.factory.*;
|
||||
|
||||
import org.isoron.uhabits.activities.habits.list.views.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
import org.isoron.uhabits.core.preferences.*;
|
||||
|
||||
@AutoFactory
|
||||
public class CheckmarkButtonController
|
||||
{
|
||||
@Nullable
|
||||
private CheckmarkButtonView view;
|
||||
|
||||
@Nullable
|
||||
private Listener listener;
|
||||
|
||||
@NonNull
|
||||
private final Preferences prefs;
|
||||
|
||||
@NonNull
|
||||
private Habit habit;
|
||||
|
||||
private long timestamp;
|
||||
|
||||
public CheckmarkButtonController(@Provided @NonNull Preferences prefs,
|
||||
@NonNull Habit habit,
|
||||
long timestamp)
|
||||
{
|
||||
this.habit = habit;
|
||||
this.timestamp = timestamp;
|
||||
this.prefs = prefs;
|
||||
}
|
||||
|
||||
public void onClick()
|
||||
{
|
||||
if (prefs.isShortToggleEnabled()) performToggle();
|
||||
else performInvalidToggle();
|
||||
}
|
||||
|
||||
public boolean onLongClick()
|
||||
{
|
||||
performToggle();
|
||||
return true;
|
||||
}
|
||||
|
||||
public void performInvalidToggle()
|
||||
{
|
||||
if (listener != null) listener.onInvalidToggle();
|
||||
}
|
||||
|
||||
public void performToggle()
|
||||
{
|
||||
if (view != null) view.toggle();
|
||||
if (listener != null) listener.onToggle(habit, timestamp);
|
||||
}
|
||||
|
||||
public void setListener(@Nullable Listener listener)
|
||||
{
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
public void setView(@Nullable CheckmarkButtonView view)
|
||||
{
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
public interface Listener
|
||||
{
|
||||
/**
|
||||
* Called when the user's attempt to perform a toggle is rejected.
|
||||
*/
|
||||
void onInvalidToggle();
|
||||
|
||||
|
||||
void onToggle(@NonNull Habit habit, long timestamp);
|
||||
}
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.activities.habits.list.controllers;
|
||||
|
||||
import android.support.annotation.*;
|
||||
|
||||
import org.isoron.uhabits.activities.habits.list.views.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
|
||||
public class HabitCardController implements HabitCardView.Controller
|
||||
{
|
||||
@Nullable
|
||||
private HabitCardView view;
|
||||
|
||||
@Nullable
|
||||
private Listener listener;
|
||||
|
||||
@Override
|
||||
public void onEdit(@NonNull Habit habit, long timestamp)
|
||||
{
|
||||
if(listener != null) listener.onEdit(habit, timestamp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInvalidEdit()
|
||||
{
|
||||
if(listener != null) listener.onInvalidEdit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInvalidToggle()
|
||||
{
|
||||
if (listener != null) listener.onInvalidToggle();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onToggle(@NonNull Habit habit, long timestamp)
|
||||
{
|
||||
if (view != null) view.triggerRipple(timestamp);
|
||||
if (listener != null) listener.onToggle(habit, timestamp);
|
||||
}
|
||||
|
||||
public void setListener(@Nullable Listener listener)
|
||||
{
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
public void setView(@Nullable HabitCardView view)
|
||||
{
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
public interface Listener extends CheckmarkButtonController.Listener,
|
||||
NumberButtonController.Listener
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@ -1,93 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.activities.habits.list.controllers;
|
||||
|
||||
import android.support.annotation.*;
|
||||
|
||||
import com.google.auto.factory.*;
|
||||
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
import org.isoron.uhabits.core.preferences.*;
|
||||
|
||||
@AutoFactory
|
||||
public class NumberButtonController
|
||||
{
|
||||
@Nullable
|
||||
private Listener listener;
|
||||
|
||||
@NonNull
|
||||
private final Preferences prefs;
|
||||
|
||||
@NonNull
|
||||
private Habit habit;
|
||||
|
||||
private long timestamp;
|
||||
|
||||
public NumberButtonController(@Provided @NonNull Preferences prefs,
|
||||
@NonNull Habit habit,
|
||||
long timestamp)
|
||||
{
|
||||
this.habit = habit;
|
||||
this.timestamp = timestamp;
|
||||
this.prefs = prefs;
|
||||
}
|
||||
|
||||
public void onClick()
|
||||
{
|
||||
if (prefs.isShortToggleEnabled()) performEdit();
|
||||
else performInvalidToggle();
|
||||
}
|
||||
|
||||
public boolean onLongClick()
|
||||
{
|
||||
performEdit();
|
||||
return true;
|
||||
}
|
||||
|
||||
public void performInvalidToggle()
|
||||
{
|
||||
if (listener != null) listener.onInvalidEdit();
|
||||
}
|
||||
|
||||
public void performEdit()
|
||||
{
|
||||
if (listener != null) listener.onEdit(habit, timestamp);
|
||||
}
|
||||
|
||||
public void setListener(@Nullable Listener listener)
|
||||
{
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
public interface Listener
|
||||
{
|
||||
/**
|
||||
* Called when the user's attempt to edit the value is rejected.
|
||||
*/
|
||||
void onInvalidEdit();
|
||||
|
||||
/**
|
||||
* Called when a the user's attempt to edit the value has been accepted.
|
||||
* @param habit the habit being edited
|
||||
* @param timestamp the timestamp being edited
|
||||
*/
|
||||
void onEdit(@NonNull Habit habit, long timestamp);
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Provides controllers that are specific for {@link org.isoron.uhabits.activities.habits.list.ListHabitsActivity}.
|
||||
*/
|
||||
package org.isoron.uhabits.activities.habits.list.controllers;
|
@ -1,96 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.activities.habits.list.controllers;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.activities.habits.list.views.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
import org.isoron.uhabits.core.preferences.*;
|
||||
import org.junit.*;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class CheckmarkButtonControllerTest extends BaseAndroidUnitTest
|
||||
{
|
||||
private CheckmarkButtonController controller;
|
||||
|
||||
private CheckmarkButtonView view;
|
||||
|
||||
private CheckmarkButtonController.Listener listener;
|
||||
|
||||
private Habit habit;
|
||||
|
||||
private int timestamp;
|
||||
|
||||
private Preferences prefs;
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
timestamp = 0;
|
||||
habit = mock(Habit.class);
|
||||
prefs = mock(Preferences.class);
|
||||
|
||||
this.view = mock(CheckmarkButtonView.class);
|
||||
this.listener = mock(CheckmarkButtonController.Listener.class);
|
||||
this.controller =
|
||||
new CheckmarkButtonController(prefs, habit, timestamp);
|
||||
controller.setView(view);
|
||||
controller.setListener(listener);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnClick_withShortToggle() throws Exception
|
||||
{
|
||||
doReturn(true).when(prefs).isShortToggleEnabled();
|
||||
controller.onClick();
|
||||
verifyToggle();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnClick_withoutShortToggle() throws Exception
|
||||
{
|
||||
doReturn(false).when(prefs).isShortToggleEnabled();
|
||||
controller.onClick();
|
||||
verifyInvalidToggle();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnLongClick() throws Exception
|
||||
{
|
||||
controller.onLongClick();
|
||||
verifyToggle();
|
||||
}
|
||||
|
||||
protected void verifyInvalidToggle()
|
||||
{
|
||||
verifyZeroInteractions(view);
|
||||
verify(listener).onInvalidToggle();
|
||||
}
|
||||
|
||||
protected void verifyToggle()
|
||||
{
|
||||
verify(view).toggle();
|
||||
verify(listener).onToggle(habit, timestamp);
|
||||
}
|
||||
}
|
@ -1,72 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.activities.habits.list.controllers;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
import org.isoron.uhabits.activities.habits.list.views.*;
|
||||
import org.isoron.uhabits.core.utils.*;
|
||||
import org.junit.*;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class HabitCardControllerTest extends BaseAndroidUnitTest
|
||||
{
|
||||
|
||||
private Habit habit;
|
||||
|
||||
private HabitCardController controller;
|
||||
|
||||
private HabitCardController.Listener listener;
|
||||
|
||||
private HabitCardView view;
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
this.habit = mock(Habit.class);
|
||||
this.listener = mock(HabitCardController.Listener.class);
|
||||
this.view = mock(HabitCardView.class);
|
||||
|
||||
this.controller = new HabitCardController();
|
||||
controller.setListener(listener);
|
||||
controller.setView(view);
|
||||
view.setController(controller);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnInvalidToggle()
|
||||
{
|
||||
controller.onInvalidToggle();
|
||||
verify(listener).onInvalidToggle();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnToggle()
|
||||
{
|
||||
long timestamp = DateUtils.getStartOfToday();
|
||||
controller.onToggle(habit, timestamp);
|
||||
verify(view).triggerRipple(timestamp);
|
||||
verify(listener).onToggle(habit, timestamp);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue