Skip to content

Commit

Permalink
Add quick selection for commonly used habit frequencies
Browse files Browse the repository at this point in the history
Closes #25
  • Loading branch information
iSoron committed Mar 19, 2016
1 parent d39e197 commit c3ff1fb
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 66 deletions.
19 changes: 19 additions & 0 deletions NOTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,22 @@ Material design icons are the official icon set from Google that are designed
under the material design guidelines. Available under the Creative Common
Attribution 4.0 International License (CC-BY 4.0).

### Android Flow Layout

<https://github.com/ApmeM/android-flowlayout>

Extended linear layout that wrap its content when there is no place in the current line.

Copyright 2011, Artem Votincev (apmem.org)

Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations
under the License.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ android {
dependencies {
compile 'com.android.support:support-v4:23.1.1'
compile 'com.github.paolorotolo:appintro:3.4.0'
compile 'org.apmem.tools:layouts:1.10@aar'
compile project(':libs:drag-sort-listview:library')
compile files('libs/ActiveAndroid.jar')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package org.isoron.uhabits.ui;

import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.espresso.NoMatchingViewException;

import org.isoron.uhabits.R;
import org.isoron.uhabits.models.Habit;
Expand All @@ -37,14 +37,18 @@
import static android.support.test.espresso.action.ViewActions.longClick;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.RootMatchers.isPlatformPopup;
import static android.support.test.espresso.matcher.ViewMatchers.Visibility.VISIBLE;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.startsWith;
import static org.isoron.uhabits.ui.HabitMatchers.containsHabit;
import static org.isoron.uhabits.ui.HabitMatchers.withName;

Expand Down Expand Up @@ -97,6 +101,20 @@ public static void typeHabitData(String name, String description, String num, St
.perform(replaceText(name));
onView(withId(R.id.input_description))
.perform(replaceText(description));

try
{
onView(allOf(withId(R.id.sFrequency), withEffectiveVisibility(VISIBLE)))
.perform(click());
onData(allOf(instanceOf(String.class), startsWith("Custom")))
.inRoot(isPlatformPopup())
.perform(click());
}
catch(NoMatchingViewException e)
{
// ignored
}

onView(withId(R.id.input_freq_num))
.perform(replaceText(num));
onView(withId(R.id.input_freq_den))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@

package org.isoron.uhabits.fragments;

import android.annotation.SuppressLint;
import android.app.DialogFragment;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.android.colorpicker.ColorPickerDialog;
Expand All @@ -49,11 +52,10 @@
import org.isoron.uhabits.models.Habit;

import java.util.Arrays;
import java.util.Date;

public class EditHabitFragment extends DialogFragment
implements OnClickListener, WeekdayPickerDialog.OnWeekdaysPickedListener,
TimePickerDialog.OnTimeSetListener
TimePickerDialog.OnTimeSetListener, Spinner.OnItemSelectedListener
{
private Integer mode;
static final int EDIT_MODE = 0;
Expand All @@ -71,6 +73,10 @@ public class EditHabitFragment extends DialogFragment
private TextView tvReminderTime;
private TextView tvReminderDays;

private Spinner sFrequency;
private ViewGroup llCustomFrequency;
private ViewGroup llReminderDays;

private SharedPreferences prefs;
private boolean is24HourMode;

Expand Down Expand Up @@ -105,6 +111,10 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
tvReminderTime = (TextView) view.findViewById(R.id.inputReminderTime);
tvReminderDays = (TextView) view.findViewById(R.id.inputReminderDays);

sFrequency = (Spinner) view.findViewById(R.id.sFrequency);
llCustomFrequency = (ViewGroup) view.findViewById(R.id.llCustomFrequency);
llReminderDays = (ViewGroup) view.findViewById(R.id.llReminderDays);

Button buttonSave = (Button) view.findViewById(R.id.buttonSave);
Button buttonDiscard = (Button) view.findViewById(R.id.buttonDiscard);
ImageButton buttonPickColor = (ImageButton) view.findViewById(R.id.buttonPickColor);
Expand All @@ -114,6 +124,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
tvReminderTime.setOnClickListener(this);
tvReminderDays.setOnClickListener(this);
buttonPickColor.setOnClickListener(this);
sFrequency.setOnItemSelectedListener(this);

prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());

Expand Down Expand Up @@ -163,6 +174,7 @@ else if (mode == EDIT_MODE)
tvFreqDen.append(modifiedHabit.freqDen.toString());

changeColor(modifiedHabit.color);
updateFrequency();
updateReminder();

return view;
Expand All @@ -183,19 +195,17 @@ private void updateReminder()
{
if (modifiedHabit.hasReminder())
{
tvReminderTime.setTextColor(Color.BLACK);
tvReminderTime.setText(DateHelper.formatTime(getActivity(), modifiedHabit.reminderHour,
modifiedHabit.reminderMin));
tvReminderDays.setVisibility(View.VISIBLE);
llReminderDays.setVisibility(View.VISIBLE);

boolean weekdays[] = DateHelper.unpackWeekdayList(modifiedHabit.reminderDays);
tvReminderDays.setText(DateHelper.formatWeekdayList(getActivity(), weekdays));
}
else
{
tvReminderTime.setTextColor(Color.GRAY);
tvReminderTime.setText(R.string.reminder_off);
tvReminderDays.setVisibility(View.GONE);
llReminderDays.setVisibility(View.GONE);
}
}

Expand Down Expand Up @@ -378,4 +388,79 @@ public void onSaveInstanceState(Bundle outState)
outState.putInt("reminderDays", modifiedHabit.reminderDays);
}
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if(parent.getId() == R.id.sFrequency)
{
switch (position)
{
case 0:
modifiedHabit.freqNum = 1;
modifiedHabit.freqDen = 1;
break;

case 1:
modifiedHabit.freqNum = 1;
modifiedHabit.freqDen = 7;
break;

case 2:
modifiedHabit.freqNum = 2;
modifiedHabit.freqDen = 7;
break;

case 3:
modifiedHabit.freqNum = 5;
modifiedHabit.freqDen = 7;
break;

case 4:
modifiedHabit.freqNum = 3;
modifiedHabit.freqDen = 7;
break;
}
}

updateFrequency();
}

@SuppressLint("SetTextI18n")
private void updateFrequency()
{
int quickSelectPosition = -1;

if(modifiedHabit.freqNum.equals(modifiedHabit.freqDen))
quickSelectPosition = 0;

else if(modifiedHabit.freqNum == 1 && modifiedHabit.freqDen == 7)
quickSelectPosition = 1;

else if(modifiedHabit.freqNum == 2 && modifiedHabit.freqDen == 7)
quickSelectPosition = 2;

else if(modifiedHabit.freqNum == 5 && modifiedHabit.freqDen == 7)
quickSelectPosition = 3;

if(quickSelectPosition >= 0)
{
sFrequency.setVisibility(View.VISIBLE);
sFrequency.setSelection(quickSelectPosition);
llCustomFrequency.setVisibility(View.GONE);
tvFreqNum.setText(modifiedHabit.freqNum.toString());
tvFreqDen.setText(modifiedHabit.freqDen.toString());
}
else
{
sFrequency.setVisibility(View.GONE);
llCustomFrequency.setVisibility(View.VISIBLE);
}
}

@Override
public void onNothingSelected(AdapterView<?> parent)
{

}
}
Loading

0 comments on commit c3ff1fb

Please sign in to comment.