diff --git a/app/src/main/java/org/isoron/uhabits/ui/BaseRootView.java b/app/src/main/java/org/isoron/uhabits/ui/BaseRootView.java index 4016442ab..eb9891f95 100644 --- a/app/src/main/java/org/isoron/uhabits/ui/BaseRootView.java +++ b/app/src/main/java/org/isoron/uhabits/ui/BaseRootView.java @@ -96,8 +96,8 @@ public abstract class BaseRootView extends FrameLayout View view = findViewById(R.id.toolbarShadow); if (view != null) view.setVisibility(View.GONE); - view = findViewById(R.id.headerShadow); - if (view != null) view.setVisibility(View.GONE); +// view = findViewById(R.id.headerShadow); +// if (view != null) view.setVisibility(View.GONE); } protected void initToolbar() diff --git a/app/src/main/java/org/isoron/uhabits/ui/BaseScreen.java b/app/src/main/java/org/isoron/uhabits/ui/BaseScreen.java index bd803cc4a..00e4aee07 100644 --- a/app/src/main/java/org/isoron/uhabits/ui/BaseScreen.java +++ b/app/src/main/java/org/isoron/uhabits/ui/BaseScreen.java @@ -89,8 +89,8 @@ public abstract class BaseScreen View view = activity.findViewById(R.id.toolbarShadow); if (view != null) view.setVisibility(View.GONE); - view = activity.findViewById(R.id.headerShadow); - if (view != null) view.setVisibility(View.GONE); +// view = activity.findViewById(R.id.headerShadow); +// if (view != null) view.setVisibility(View.GONE); } } @@ -270,8 +270,8 @@ public abstract class BaseScreen View view = activity.findViewById(R.id.toolbarShadow); if (view != null) view.setVisibility(View.GONE); - view = activity.findViewById(R.id.headerShadow); - if (view != null) view.setVisibility(View.GONE); +// view = activity.findViewById(R.id.headerShadow); +// if (view != null) view.setVisibility(View.GONE); } private class ActionModeWrapper implements ActionMode.Callback diff --git a/app/src/main/java/org/isoron/uhabits/ui/habits/show/ShowHabitFragment.java b/app/src/main/java/org/isoron/uhabits/ui/habits/show/ShowHabitFragment.java index 56279b767..9352e58d3 100644 --- a/app/src/main/java/org/isoron/uhabits/ui/habits/show/ShowHabitFragment.java +++ b/app/src/main/java/org/isoron/uhabits/ui/habits/show/ShowHabitFragment.java @@ -19,38 +19,20 @@ package org.isoron.uhabits.ui.habits.show; -import android.os.Bundle; -import android.support.v4.app.Fragment; -import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import android.widget.AdapterView; -import android.widget.Spinner; - -import org.isoron.uhabits.R; -import org.isoron.uhabits.models.Habit; -import org.isoron.uhabits.models.ModelObservable; -import org.isoron.uhabits.tasks.BaseTask; -import org.isoron.uhabits.ui.habits.edit.BaseDialogFragment; -import org.isoron.uhabits.ui.habits.edit.EditHabitDialogFragment; -import org.isoron.uhabits.ui.habits.edit.HistoryEditorDialog; -import org.isoron.uhabits.utils.DateUtils; -import org.isoron.uhabits.utils.InterfaceUtils; -import org.isoron.uhabits.ui.habits.show.views.HabitDataView; -import org.isoron.uhabits.ui.habits.show.views.HabitFrequencyView; -import org.isoron.uhabits.ui.habits.show.views.HabitHistoryView; -import org.isoron.uhabits.ui.habits.show.views.HabitScoreView; -import org.isoron.uhabits.ui.habits.show.views.HabitStreakView; - -import java.util.LinkedList; -import java.util.List; - -import butterknife.BindView; -import butterknife.ButterKnife; -import butterknife.OnClick; +import android.os.*; +import android.support.v4.app.*; +import android.view.*; +import android.widget.*; + +import org.isoron.uhabits.*; +import org.isoron.uhabits.models.*; +import org.isoron.uhabits.ui.habits.edit.*; +import org.isoron.uhabits.ui.habits.show.views.*; +import org.isoron.uhabits.utils.*; + +import java.util.*; + +import butterknife.*; public class ShowHabitFragment extends Fragment implements ModelObservable.Listener @@ -90,6 +72,12 @@ public class ShowHabitFragment extends Fragment @BindView(R.id.streakView) HabitStreakView habitStreakView; + @BindView(R.id.subtitle) + SubtitleCardView subtitleView; + + @BindView(R.id.overview) + OverviewCardView overview; + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -103,7 +91,6 @@ public class ShowHabitFragment extends Fragment habit = activity.getHabit(); helper.updateColors(); - helper.updateMainHeader(view); int defaultScoreInterval = InterfaceUtils.getDefaultScoreInterval(getContext()); @@ -131,6 +118,9 @@ public class ShowHabitFragment extends Fragment private void createDataViews() { + subtitleView.setHabit(habit); + overview.setHabit(habit); + dataViews = new LinkedList<>(); dataViews.add(habitScoreView); dataViews.add(habitHistoryView); @@ -141,13 +131,6 @@ public class ShowHabitFragment extends Fragment dataView.setHabit(habit); } - @Override - public void onResume() - { - super.onResume(); - refreshData(); - } - @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { @@ -173,11 +156,6 @@ public class ShowHabitFragment extends Fragment return true; } - public void refreshData() - { - new RefreshTask().execute(); - } - private void setScoreBucketSize(int position) { if (getView() == null) return; @@ -185,8 +163,6 @@ public class ShowHabitFragment extends Fragment habitScoreView.setBucketSize( HabitScoreView.DEFAULT_BUCKET_SIZES[position]); - if (position != previousScoreInterval) refreshData(); - InterfaceUtils.setDefaultScoreInterval(getContext(), position); previousScoreInterval = position; } @@ -194,10 +170,8 @@ public class ShowHabitFragment extends Fragment @Override public void onModelChange() { - refreshData(); activity.runOnUiThread(() -> { helper.updateColors(); - helper.updateMainHeader(getView()); helper.updateCardHeaders(getView()); if (activity != null) activity.setupHabitActionBar(); }); @@ -217,31 +191,6 @@ public class ShowHabitFragment extends Fragment super.onPause(); } - private class RefreshTask extends BaseTask - { - @Override - protected void doInBackground() - { - if (habit == null) return; - if (dataViews == null) return; - - long today = DateUtils.getStartOfToday(); - long lastMonth = today - 30 * DateUtils.millisecondsInOneDay; - long lastYear = today - 365 * DateUtils.millisecondsInOneDay; - - todayScore = (float) habit.getScores().getTodayValue(); - lastMonthScore = (float) habit.getScores().getValue(lastMonth); - lastYearScore = (float) habit.getScores().getValue(lastYear); - } - - @Override - protected void onPostExecute(Void aVoid) - { - helper.updateScore(getView()); - super.onPostExecute(aVoid); - } - } - private class OnItemSelectedListener implements AdapterView.OnItemSelectedListener { diff --git a/app/src/main/java/org/isoron/uhabits/ui/habits/show/ShowHabitHelper.java b/app/src/main/java/org/isoron/uhabits/ui/habits/show/ShowHabitHelper.java index 8539f5792..168355e21 100644 --- a/app/src/main/java/org/isoron/uhabits/ui/habits/show/ShowHabitHelper.java +++ b/app/src/main/java/org/isoron/uhabits/ui/habits/show/ShowHabitHelper.java @@ -19,13 +19,10 @@ package org.isoron.uhabits.ui.habits.show; -import android.content.res.*; import android.view.*; import android.widget.*; import org.isoron.uhabits.*; -import org.isoron.uhabits.models.*; -import org.isoron.uhabits.ui.habits.show.views.*; import org.isoron.uhabits.utils.*; public class ShowHabitHelper @@ -37,35 +34,9 @@ public class ShowHabitHelper this.fragment = fragment; } - String getFreqText() - { - if (fragment.habit == null) return ""; - - Resources resources = fragment.getResources(); - Frequency freq = fragment.habit.getFrequency(); - Integer freqNum = freq.getNumerator(); - Integer freqDen = freq.getDenominator(); - - if (freqNum.equals(freqDen)) - return resources.getString(R.string.every_day); - - if (freqNum == 1) - { - if (freqDen == 7) return resources.getString(R.string.every_week); - if (freqDen % 7 == 0) - return resources.getString(R.string.every_x_weeks, freqDen / 7); - return resources.getString(R.string.every_x_days, freqDen); - } - - String times_every = resources.getString(R.string.time_every); - return String.format("%d %s %d %s", freqNum, times_every, freqDen, - resources.getString(R.string.days)); - } - void updateCardHeaders(View view) { updateColor(view, R.id.tvHistory); - updateColor(view, R.id.tvOverview); updateColor(view, R.id.tvStrength); updateColor(view, R.id.tvStreaks); updateColor(view, R.id.tvWeekdayFreq); @@ -90,76 +61,4 @@ public class ShowHabitHelper InterfaceUtils.getStyledColor(fragment.getContext(), R.attr.mediumContrastTextColor); } - - void updateMainHeader(View view) - { - if (fragment.habit == null) return; - - TextView questionLabel = - (TextView) view.findViewById(R.id.questionLabel); - questionLabel.setTextColor(fragment.activeColor); - questionLabel.setText(fragment.habit.getDescription()); - - TextView reminderLabel = - (TextView) view.findViewById(R.id.reminderLabel); - - if (fragment.habit.hasReminder()) - { - Reminder reminder = fragment.habit.getReminder(); - reminderLabel.setText( - DateUtils.formatTime(fragment.getActivity(), reminder.getHour(), - reminder.getMinute())); - } - else - { - reminderLabel.setText( - fragment.getResources().getString(R.string.reminder_off)); - } - - TextView frequencyLabel = - (TextView) view.findViewById(R.id.frequencyLabel); - frequencyLabel.setText(getFreqText()); - - if (fragment.habit.getDescription().isEmpty()) - questionLabel.setVisibility(View.GONE); - } - - void updateScore(View view) - { - if (fragment.habit == null) return; - if (view == null) return; - - float todayPercentage = fragment.todayScore / Score.MAX_VALUE; - float monthDiff = - todayPercentage - (fragment.lastMonthScore / Score.MAX_VALUE); - float yearDiff = - todayPercentage - (fragment.lastYearScore / Score.MAX_VALUE); - - RingView scoreRing = (RingView) view.findViewById(R.id.scoreRing); - int androidColor = ColorUtils.getColor(fragment.getActivity(), - fragment.habit.getColor()); - scoreRing.setColor(androidColor); - scoreRing.setPercentage(todayPercentage); - - TextView scoreLabel = (TextView) view.findViewById(R.id.scoreLabel); - TextView monthDiffLabel = - (TextView) view.findViewById(R.id.monthDiffLabel); - TextView yearDiffLabel = - (TextView) view.findViewById(R.id.yearDiffLabel); - - scoreLabel.setText(String.format("%.0f%%", todayPercentage * 100)); - - String minus = "\u2212"; - monthDiffLabel.setText( - String.format("%s%.0f%%", (monthDiff >= 0 ? "+" : minus), - Math.abs(monthDiff) * 100)); - yearDiffLabel.setText( - String.format("%s%.0f%%", (yearDiff >= 0 ? "+" : minus), - Math.abs(yearDiff) * 100)); - - monthDiffLabel.setTextColor( - monthDiff >= 0 ? fragment.activeColor : fragment.inactiveColor); - yearDiffLabel.setTextColor( - yearDiff >= 0 ? fragment.activeColor : fragment.inactiveColor); - } } diff --git a/app/src/main/java/org/isoron/uhabits/ui/habits/show/views/HabitDataView.java b/app/src/main/java/org/isoron/uhabits/ui/habits/show/views/HabitDataView.java index d9163f0e5..223703393 100644 --- a/app/src/main/java/org/isoron/uhabits/ui/habits/show/views/HabitDataView.java +++ b/app/src/main/java/org/isoron/uhabits/ui/habits/show/views/HabitDataView.java @@ -26,6 +26,4 @@ public interface HabitDataView void setHabit(Habit habit); void refreshData(); - - void postInvalidate(); } diff --git a/app/src/main/java/org/isoron/uhabits/ui/habits/show/views/OverviewCardView.java b/app/src/main/java/org/isoron/uhabits/ui/habits/show/views/OverviewCardView.java new file mode 100644 index 000000000..ae952597b --- /dev/null +++ b/app/src/main/java/org/isoron/uhabits/ui/habits/show/views/OverviewCardView.java @@ -0,0 +1,186 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * 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 . + */ + +package org.isoron.uhabits.ui.habits.show.views; + +import android.content.*; +import android.support.annotation.*; +import android.util.*; +import android.widget.*; + +import org.isoron.uhabits.*; +import org.isoron.uhabits.models.*; +import org.isoron.uhabits.tasks.*; +import org.isoron.uhabits.utils.*; + +import butterknife.*; + +public class OverviewCardView extends LinearLayout + implements ModelObservable.Listener +{ + @Nullable + private Habit habit; + + @NonNull + private Cache cache; + + @BindView(R.id.scoreRing) + RingView scoreRing; + + @BindView(R.id.scoreLabel) + TextView scoreLabel; + + @BindView(R.id.monthDiffLabel) + TextView monthDiffLabel; + + @BindView(R.id.yearDiffLabel) + TextView yearDiffLabel; + + @BindView(R.id.title) + TextView title; + + private int color; + + public OverviewCardView(Context context) + { + super(context); + init(); + } + + public OverviewCardView(Context context, AttributeSet attrs) + { + super(context, attrs); + init(); + } + + @Override + public void onModelChange() + { + refreshCache(); + } + + public void setHabit(@Nullable Habit habit) + { + this.habit = habit; + color = ColorUtils.getColor(getContext(), habit.getColor()); + } + + @Override + protected void onAttachedToWindow() + { + super.onAttachedToWindow(); + refreshCache(); + if(habit != null) habit.getObservable().addListener(this); + } + + @Override + protected void onDetachedFromWindow() + { + if(habit != null) habit.getObservable().removeListener(this); + super.onDetachedFromWindow(); + } + + private void init() + { + inflate(getContext(), R.layout.show_habit_overview, this); + ButterKnife.bind(this); + cache = new Cache(); + + if(isInEditMode()) initEditMode(); + } + + private void initEditMode() + { + color = ColorUtils.getAndroidTestColor(1); + cache.todayScore = Score.MAX_VALUE * 0.6f; + cache.lastMonthScore = Score.MAX_VALUE * 0.42f; + cache.lastYearScore = Score.MAX_VALUE * 0.75f; + updateScore(); + } + + private void refreshCache() + { + new BaseTask() + { + @Override + protected void doInBackground() + { + if(habit == null) return; + ScoreList scores = habit.getScores(); + + + long today = DateUtils.getStartOfToday(); + long lastMonth = today - 30 * DateUtils.millisecondsInOneDay; + long lastYear = today - 365 * DateUtils.millisecondsInOneDay; + + cache.todayScore = (float) scores.getTodayValue(); + cache.lastMonthScore = (float) scores.getValue(lastMonth); + cache.lastYearScore = (float) scores.getValue(lastYear); + } + + @Override + protected void onPostExecute(Void aVoid) + { + updateScore(); + super.onPostExecute(aVoid); + } + }.execute(); + } + + void updateScore() + { + float todayPercentage = cache.todayScore / Score.MAX_VALUE; + float monthDiff = todayPercentage - (cache.lastMonthScore / Score.MAX_VALUE); + float yearDiff = todayPercentage - (cache.lastYearScore / Score.MAX_VALUE); + + scoreRing.setColor(color); + scoreRing.setPercentage(todayPercentage); + + scoreLabel.setTextColor(color); + scoreLabel.setText(String.format("%.0f%%", todayPercentage * 100)); + + title.setTextColor(color); + + monthDiffLabel.setText(formatPercentageDiff(monthDiff)); + yearDiffLabel.setText(formatPercentageDiff(yearDiff)); + + int inactiveColor = InterfaceUtils.getStyledColor(getContext(), + R.attr.mediumContrastTextColor); + + monthDiffLabel.setTextColor(monthDiff >= 0 ? color : inactiveColor); + yearDiffLabel.setTextColor(yearDiff >= 0 ? color : inactiveColor); + + postInvalidate(); + } + + private String formatPercentageDiff(float percentageDiff) + { + return String.format("%s%.0f%%", (percentageDiff >= 0 ? "+" : "\u2212"), + Math.abs(percentageDiff) * 100); + } + + private class Cache + { + public float todayScore; + + public float lastMonthScore; + + public float lastYearScore; + } +} diff --git a/app/src/main/java/org/isoron/uhabits/ui/habits/show/views/SubtitleCardView.java b/app/src/main/java/org/isoron/uhabits/ui/habits/show/views/SubtitleCardView.java new file mode 100644 index 000000000..dad8fac72 --- /dev/null +++ b/app/src/main/java/org/isoron/uhabits/ui/habits/show/views/SubtitleCardView.java @@ -0,0 +1,153 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * 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 . + */ + +package org.isoron.uhabits.ui.habits.show.views; + +import android.annotation.*; +import android.content.*; +import android.content.res.*; +import android.support.annotation.*; +import android.util.*; +import android.view.*; +import android.widget.*; + +import org.isoron.uhabits.*; +import org.isoron.uhabits.models.*; +import org.isoron.uhabits.utils.*; + +import butterknife.*; + +public class SubtitleCardView extends LinearLayout + implements ModelObservable.Listener +{ + @BindView(R.id.questionLabel) + TextView questionLabel; + + @BindView(R.id.frequencyLabel) + TextView frequencyLabel; + + @BindView(R.id.reminderLabel) + TextView reminderLabel; + + @Nullable + private Habit habit; + + public SubtitleCardView(Context context) + { + super(context); + init(); + } + + public SubtitleCardView(Context context, AttributeSet attrs) + { + super(context, attrs); + init(); + } + + @Override + public void onModelChange() + { + refreshData(); + } + + public void setHabit(@Nullable Habit habit) + { + this.habit = habit; + refreshData(); + } + + @Override + protected void onAttachedToWindow() + { + super.onAttachedToWindow(); + if (habit != null) habit.getObservable().addListener(this); + } + + @Override + protected void onDetachedFromWindow() + { + if (habit != null) habit.getObservable().removeListener(this); + super.onDetachedFromWindow(); + } + + private void init() + { + Context context = getContext(); + inflate(context, R.layout.show_habit_subtitle, this); + ButterKnife.bind(this); + + if (isInEditMode()) initEditMode(); + } + + @SuppressLint("SetTextI18n") + private void initEditMode() + { + questionLabel.setTextColor(ColorUtils.getAndroidTestColor(1)); + questionLabel.setText("Have you meditated today?"); + reminderLabel.setText("08:00"); + } + + private void refreshData() + { + if (habit == null) return; + int color = ColorUtils.getColor(getContext(), habit.getColor()); + + reminderLabel.setText(getResources().getString(R.string.reminder_off)); + questionLabel.setVisibility(VISIBLE); + + questionLabel.setTextColor(color); + questionLabel.setText(habit.getDescription()); + frequencyLabel.setText(toText(habit.getFrequency())); + + if (habit.hasReminder()) updateReminderText(habit.getReminder()); + + if (habit.getDescription().isEmpty()) + questionLabel.setVisibility(View.GONE); + + postInvalidate(); + } + + String toText(Frequency freq) + { + Resources resources = getResources(); + Integer num = freq.getNumerator(); + Integer den = freq.getDenominator(); + + if (num.equals(den)) return resources.getString(R.string.every_day); + + if (num == 1) + { + if (den == 7) return resources.getString(R.string.every_week); + if (den % 7 == 0) + return resources.getString(R.string.every_x_weeks, den / 7); + return resources.getString(R.string.every_x_days, den); + } + + String times_every = resources.getString(R.string.times_every); + return String.format("%d %s %d %s", num, times_every, den, + resources.getString(R.string.days)); + } + + private void updateReminderText(Reminder reminder) + { + reminderLabel.setText( + DateUtils.formatTime(getContext(), reminder.getHour(), + reminder.getMinute())); + } +} diff --git a/app/src/main/java/org/isoron/uhabits/utils/ColorUtils.java b/app/src/main/java/org/isoron/uhabits/utils/ColorUtils.java index e8116c1b0..9499bccbf 100644 --- a/app/src/main/java/org/isoron/uhabits/utils/ColorUtils.java +++ b/app/src/main/java/org/isoron/uhabits/utils/ColorUtils.java @@ -19,11 +19,11 @@ package org.isoron.uhabits.utils; -import android.content.Context; -import android.graphics.Color; -import android.util.Log; +import android.content.*; +import android.graphics.*; +import android.util.*; -import org.isoron.uhabits.R; +import org.isoron.uhabits.*; public abstract class ColorUtils { diff --git a/app/src/main/res/layout/list_habits_preview.xml b/app/src/main/res/layout/list_habits_preview.xml index ff52cd785..87250848d 100644 --- a/app/src/main/res/layout/list_habits_preview.xml +++ b/app/src/main/res/layout/list_habits_preview.xml @@ -60,13 +60,13 @@ android:layout_width="match_parent" android:layout_height="wrap_content" /> - - - + - - - + diff --git a/app/src/main/res/layout/show_habit.xml b/app/src/main/res/layout/show_habit.xml index 045f6b5fa..9bfd2c569 100644 --- a/app/src/main/res/layout/show_habit.xml +++ b/app/src/main/res/layout/show_habit.xml @@ -20,7 +20,6 @@ + android:clipToPadding="false"> - - - - - - - + - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + style="@style/Card" /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/show_habit_preview.xml b/app/src/main/res/layout/show_habit_preview.xml new file mode 100644 index 000000000..4ff01a1a9 --- /dev/null +++ b/app/src/main/res/layout/show_habit_preview.xml @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/show_habit_subtitle.xml b/app/src/main/res/layout/show_habit_subtitle.xml new file mode 100644 index 000000000..4c57473c0 --- /dev/null +++ b/app/src/main/res/layout/show_habit_subtitle.xml @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/styles_show_habit.xml b/app/src/main/res/values/styles_show_habit.xml new file mode 100644 index 000000000..e0434f2b3 --- /dev/null +++ b/app/src/main/res/values/styles_show_habit.xml @@ -0,0 +1,37 @@ + + + + + + \ No newline at end of file