mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Simplify and optimize loading time of ShowHabitFragment
This commit is contained in:
@@ -20,27 +20,16 @@
|
|||||||
package org.isoron.uhabits;
|
package org.isoron.uhabits;
|
||||||
|
|
||||||
import android.app.ActionBar;
|
import android.app.ActionBar;
|
||||||
import android.content.BroadcastReceiver;
|
|
||||||
import android.content.ContentUris;
|
import android.content.ContentUris;
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.content.IntentFilter;
|
|
||||||
import android.graphics.drawable.ColorDrawable;
|
import android.graphics.drawable.ColorDrawable;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v4.content.LocalBroadcastManager;
|
|
||||||
|
|
||||||
import org.isoron.uhabits.fragments.ShowHabitFragment;
|
|
||||||
import org.isoron.uhabits.models.Habit;
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
|
||||||
public class ShowHabitActivity extends BaseActivity
|
public class ShowHabitActivity extends BaseActivity
|
||||||
{
|
{
|
||||||
|
private Habit habit;
|
||||||
public Habit habit;
|
|
||||||
private Receiver receiver;
|
|
||||||
private LocalBroadcastManager localBroadcastManager;
|
|
||||||
|
|
||||||
private ShowHabitFragment fragment;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState)
|
protected void onCreate(Bundle savedInstanceState)
|
||||||
@@ -51,37 +40,18 @@ public class ShowHabitActivity extends BaseActivity
|
|||||||
habit = Habit.get(ContentUris.parseId(data));
|
habit = Habit.get(ContentUris.parseId(data));
|
||||||
ActionBar actionBar = getActionBar();
|
ActionBar actionBar = getActionBar();
|
||||||
|
|
||||||
if(actionBar != null)
|
if(actionBar != null && getHabit() != null)
|
||||||
{
|
{
|
||||||
actionBar.setTitle(habit.name);
|
actionBar.setTitle(getHabit().name);
|
||||||
|
|
||||||
if (android.os.Build.VERSION.SDK_INT >= 21)
|
if (android.os.Build.VERSION.SDK_INT >= 21)
|
||||||
actionBar.setBackgroundDrawable(new ColorDrawable(habit.color));
|
actionBar.setBackgroundDrawable(new ColorDrawable(getHabit().color));
|
||||||
}
|
}
|
||||||
|
|
||||||
setContentView(R.layout.show_habit_activity);
|
setContentView(R.layout.show_habit_activity);
|
||||||
|
|
||||||
fragment = (ShowHabitFragment) getFragmentManager().findFragmentById(R.id.fragment2);
|
|
||||||
|
|
||||||
receiver = new Receiver();
|
|
||||||
localBroadcastManager = LocalBroadcastManager.getInstance(this);
|
|
||||||
localBroadcastManager.registerReceiver(receiver,
|
|
||||||
new IntentFilter(MainActivity.ACTION_REFRESH));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class Receiver extends BroadcastReceiver
|
public Habit getHabit()
|
||||||
{
|
{
|
||||||
@Override
|
return habit;
|
||||||
public void onReceive(Context context, Intent intent)
|
|
||||||
{
|
|
||||||
fragment.refreshData();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onDestroy()
|
|
||||||
{
|
|
||||||
localBroadcastManager.unregisterReceiver(receiver);
|
|
||||||
super.onDestroy();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,18 +33,17 @@ import android.view.View;
|
|||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.AdapterView;
|
import android.widget.AdapterView;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.LinearLayout;
|
|
||||||
import android.widget.Spinner;
|
import android.widget.Spinner;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import org.isoron.uhabits.helpers.ColorHelper;
|
|
||||||
import org.isoron.uhabits.helpers.UIHelper;
|
|
||||||
import org.isoron.uhabits.HabitBroadcastReceiver;
|
import org.isoron.uhabits.HabitBroadcastReceiver;
|
||||||
import org.isoron.uhabits.R;
|
import org.isoron.uhabits.R;
|
||||||
import org.isoron.uhabits.ShowHabitActivity;
|
import org.isoron.uhabits.ShowHabitActivity;
|
||||||
import org.isoron.uhabits.commands.Command;
|
import org.isoron.uhabits.commands.Command;
|
||||||
import org.isoron.uhabits.dialogs.HistoryEditorDialog;
|
import org.isoron.uhabits.dialogs.HistoryEditorDialog;
|
||||||
|
import org.isoron.uhabits.helpers.ColorHelper;
|
||||||
import org.isoron.uhabits.helpers.ReminderHelper;
|
import org.isoron.uhabits.helpers.ReminderHelper;
|
||||||
|
import org.isoron.uhabits.helpers.UIHelper;
|
||||||
import org.isoron.uhabits.models.Habit;
|
import org.isoron.uhabits.models.Habit;
|
||||||
import org.isoron.uhabits.models.Score;
|
import org.isoron.uhabits.models.Score;
|
||||||
import org.isoron.uhabits.tasks.BaseTask;
|
import org.isoron.uhabits.tasks.BaseTask;
|
||||||
@@ -53,7 +52,6 @@ import org.isoron.uhabits.views.HabitFrequencyView;
|
|||||||
import org.isoron.uhabits.views.HabitHistoryView;
|
import org.isoron.uhabits.views.HabitHistoryView;
|
||||||
import org.isoron.uhabits.views.HabitScoreView;
|
import org.isoron.uhabits.views.HabitScoreView;
|
||||||
import org.isoron.uhabits.views.HabitStreakView;
|
import org.isoron.uhabits.views.HabitStreakView;
|
||||||
import org.isoron.uhabits.views.RepetitionCountView;
|
|
||||||
import org.isoron.uhabits.views.RingView;
|
import org.isoron.uhabits.views.RingView;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
@@ -78,6 +76,8 @@ public class ShowHabitFragment extends Fragment
|
|||||||
@Nullable
|
@Nullable
|
||||||
private SharedPreferences prefs;
|
private SharedPreferences prefs;
|
||||||
|
|
||||||
|
private int previousScoreInterval;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStart()
|
public void onStart()
|
||||||
{
|
{
|
||||||
@@ -90,7 +90,7 @@ public class ShowHabitFragment extends Fragment
|
|||||||
{
|
{
|
||||||
View view = inflater.inflate(R.layout.show_habit, container, false);
|
View view = inflater.inflate(R.layout.show_habit, container, false);
|
||||||
activity = (ShowHabitActivity) getActivity();
|
activity = (ShowHabitActivity) getActivity();
|
||||||
habit = activity.habit;
|
habit = activity.getHabit();
|
||||||
|
|
||||||
dataViews = new LinkedList<>();
|
dataViews = new LinkedList<>();
|
||||||
|
|
||||||
@@ -102,19 +102,17 @@ public class ShowHabitFragment extends Fragment
|
|||||||
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
||||||
int defaultScoreInterval = prefs.getInt("pref_score_view_interval", 1);
|
int defaultScoreInterval = prefs.getInt("pref_score_view_interval", 1);
|
||||||
if(defaultScoreInterval > 5 || defaultScoreInterval < 0) defaultScoreInterval = 1;
|
if(defaultScoreInterval > 5 || defaultScoreInterval < 0) defaultScoreInterval = 1;
|
||||||
|
previousScoreInterval = defaultScoreInterval;
|
||||||
setScoreBucketSize(defaultScoreInterval);
|
setScoreBucketSize(defaultScoreInterval);
|
||||||
|
|
||||||
|
|
||||||
sStrengthInterval.setSelection(defaultScoreInterval);
|
sStrengthInterval.setSelection(defaultScoreInterval);
|
||||||
sStrengthInterval.setOnItemSelectedListener(this);
|
sStrengthInterval.setOnItemSelectedListener(this);
|
||||||
|
|
||||||
dataViews.add((HabitStreakView) view.findViewById(R.id.streakView));
|
|
||||||
dataViews.add((HabitStreakView) view.findViewById(R.id.smallStreakView));
|
|
||||||
dataViews.add((HabitScoreView) view.findViewById(R.id.scoreView));
|
dataViews.add((HabitScoreView) view.findViewById(R.id.scoreView));
|
||||||
dataViews.add((HabitHistoryView) view.findViewById(R.id.historyView));
|
dataViews.add((HabitHistoryView) view.findViewById(R.id.historyView));
|
||||||
dataViews.add((HabitFrequencyView) view.findViewById(R.id.punchcardView));
|
dataViews.add((HabitFrequencyView) view.findViewById(R.id.punchcardView));
|
||||||
|
dataViews.add((HabitStreakView) view.findViewById(R.id.streakView));
|
||||||
LinearLayout llRepetition = (LinearLayout) view.findViewById(R.id.llRepetition);
|
|
||||||
for(int i = 0; i < llRepetition.getChildCount(); i++)
|
|
||||||
dataViews.add((RepetitionCountView) llRepetition.getChildAt(i));
|
|
||||||
|
|
||||||
updateHeaders(view);
|
updateHeaders(view);
|
||||||
|
|
||||||
@@ -145,11 +143,17 @@ public class ShowHabitFragment extends Fragment
|
|||||||
}
|
}
|
||||||
|
|
||||||
setHasOptionsMenu(true);
|
setHasOptionsMenu(true);
|
||||||
refreshData();
|
|
||||||
|
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResume()
|
||||||
|
{
|
||||||
|
super.onResume();
|
||||||
|
refreshData();
|
||||||
|
}
|
||||||
|
|
||||||
private void updateScoreRing(View view)
|
private void updateScoreRing(View view)
|
||||||
{
|
{
|
||||||
if(habit == null) return;
|
if(habit == null) return;
|
||||||
@@ -175,7 +179,6 @@ public class ShowHabitFragment extends Fragment
|
|||||||
updateColor(view, R.id.tvStrength);
|
updateColor(view, R.id.tvStrength);
|
||||||
updateColor(view, R.id.tvStreaks);
|
updateColor(view, R.id.tvStreaks);
|
||||||
updateColor(view, R.id.tvWeekdayFreq);
|
updateColor(view, R.id.tvWeekdayFreq);
|
||||||
updateColor(view, R.id.tvCount);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateColor(View view, int viewId)
|
private void updateColor(View view, int viewId)
|
||||||
@@ -236,23 +239,24 @@ public class ShowHabitFragment extends Fragment
|
|||||||
new BaseTask()
|
new BaseTask()
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... params)
|
protected void doInBackground()
|
||||||
{
|
{
|
||||||
if(dataViews == null) return null;
|
if(dataViews == null) return;
|
||||||
updateScoreRing(getView());
|
updateScoreRing(getView());
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
for(HabitDataView view : dataViews)
|
for(HabitDataView view : dataViews)
|
||||||
|
{
|
||||||
view.refreshData();
|
view.refreshData();
|
||||||
|
onProgressUpdate(count++);
|
||||||
return null;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(Void aVoid)
|
protected void onProgressUpdate(Integer... values)
|
||||||
{
|
{
|
||||||
if(dataViews == null) return;
|
if(dataViews == null) return;
|
||||||
for(HabitDataView view : dataViews)
|
dataViews.get(values[0]).postInvalidate();
|
||||||
view.invalidate();
|
|
||||||
}
|
}
|
||||||
}.execute();
|
}.execute();
|
||||||
|
|
||||||
@@ -267,17 +271,18 @@ public class ShowHabitFragment extends Fragment
|
|||||||
|
|
||||||
private void setScoreBucketSize(int position)
|
private void setScoreBucketSize(int position)
|
||||||
{
|
{
|
||||||
|
if(scoreView == null) return;
|
||||||
|
|
||||||
int sizes[] = { 1, 7, 31, 92, 365 };
|
int sizes[] = { 1, 7, 31, 92, 365 };
|
||||||
int size = sizes[position];
|
int size = sizes[position];
|
||||||
|
|
||||||
if(scoreView != null)
|
|
||||||
{
|
|
||||||
scoreView.setBucketSize(size);
|
scoreView.setBucketSize(size);
|
||||||
refreshData();
|
if(position != previousScoreInterval) refreshData();
|
||||||
}
|
|
||||||
|
|
||||||
if(prefs != null)
|
if(prefs != null)
|
||||||
prefs.edit().putInt("pref_score_view_interval", position).apply();
|
prefs.edit().putInt("pref_score_view_interval", position).apply();
|
||||||
|
|
||||||
|
previousScoreInterval = position;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ public class HabitStreakView extends View implements HabitDataView
|
|||||||
dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
|
dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
|
||||||
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
|
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||||
rect = new RectF();
|
rect = new RectF();
|
||||||
|
maxStreakCount = 10;
|
||||||
baseSize = getResources().getDimensionPixelSize(R.dimen.baseSize);
|
baseSize = getResources().getDimensionPixelSize(R.dimen.baseSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -53,75 +53,6 @@
|
|||||||
app:maxDiameter="80"
|
app:maxDiameter="80"
|
||||||
app:textSize="@dimen/smallTextSize"/>
|
app:textSize="@dimen/smallTextSize"/>
|
||||||
|
|
||||||
<LinearLayout
|
|
||||||
style="@style/smallDataViewStyle"
|
|
||||||
android:orientation="vertical">
|
|
||||||
|
|
||||||
<org.isoron.uhabits.views.HabitStreakView
|
|
||||||
android:id="@+id/smallStreakView"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="80dp"/>
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:text="@string/best_streaks"
|
|
||||||
android:layout_marginTop="9dp"
|
|
||||||
android:textColor="@color/fadedTextColor"
|
|
||||||
android:gravity="center"/>
|
|
||||||
|
|
||||||
</LinearLayout>
|
|
||||||
|
|
||||||
</LinearLayout>
|
|
||||||
|
|
||||||
</LinearLayout>
|
|
||||||
|
|
||||||
<LinearLayout
|
|
||||||
style="@style/cardStyle"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:gravity="top|start"
|
|
||||||
android:visibility="gone">
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/tvCount"
|
|
||||||
style="@style/cardHeaderStyle"
|
|
||||||
android:text="@string/number_of_repetitions"/>
|
|
||||||
|
|
||||||
<LinearLayout
|
|
||||||
android:id="@+id/llRepetition"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_gravity="center"
|
|
||||||
android:orientation="horizontal">
|
|
||||||
|
|
||||||
<org.isoron.uhabits.views.RepetitionCountView
|
|
||||||
style="@style/smallDataViewStyle"
|
|
||||||
app:interval="30"
|
|
||||||
app:labelFormat="@string/last_x_days"
|
|
||||||
app:labelValue="30"
|
|
||||||
app:textSize="12"/>
|
|
||||||
|
|
||||||
<org.isoron.uhabits.views.RepetitionCountView
|
|
||||||
style="@style/smallDataViewStyle"
|
|
||||||
app:interval="92"
|
|
||||||
app:labelFormat="@string/last_x_months"
|
|
||||||
app:labelValue="4"
|
|
||||||
app:textSize="12"/>
|
|
||||||
|
|
||||||
<org.isoron.uhabits.views.RepetitionCountView
|
|
||||||
style="@style/smallDataViewStyle"
|
|
||||||
app:interval="365"
|
|
||||||
app:labelFormat="@string/last_x_months"
|
|
||||||
app:labelValue="12"
|
|
||||||
app:textSize="12"/>
|
|
||||||
|
|
||||||
<org.isoron.uhabits.views.RepetitionCountView
|
|
||||||
style="@style/smallDataViewStyle"
|
|
||||||
app:interval="0"
|
|
||||||
app:label="@string/all_time"
|
|
||||||
app:textSize="12"/>
|
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|||||||
Reference in New Issue
Block a user