Simplify and optimize loading time of ShowHabitFragment

pull/77/merge
Alinson S. Xavier 10 years ago
parent 357646152f
commit eb8dd1d450

@ -20,27 +20,16 @@
package org.isoron.uhabits;
import android.app.ActionBar;
import android.content.BroadcastReceiver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import org.isoron.uhabits.fragments.ShowHabitFragment;
import org.isoron.uhabits.models.Habit;
public class ShowHabitActivity extends BaseActivity
{
public Habit habit;
private Receiver receiver;
private LocalBroadcastManager localBroadcastManager;
private ShowHabitFragment fragment;
private Habit habit;
@Override
protected void onCreate(Bundle savedInstanceState)
@ -51,37 +40,18 @@ public class ShowHabitActivity extends BaseActivity
habit = Habit.get(ContentUris.parseId(data));
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)
actionBar.setBackgroundDrawable(new ColorDrawable(habit.color));
actionBar.setBackgroundDrawable(new ColorDrawable(getHabit().color));
}
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
{
@Override
public void onReceive(Context context, Intent intent)
{
fragment.refreshData();
}
}
@Override
protected void onDestroy()
public Habit getHabit()
{
localBroadcastManager.unregisterReceiver(receiver);
super.onDestroy();
return habit;
}
}

@ -33,18 +33,17 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
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.R;
import org.isoron.uhabits.ShowHabitActivity;
import org.isoron.uhabits.commands.Command;
import org.isoron.uhabits.dialogs.HistoryEditorDialog;
import org.isoron.uhabits.helpers.ColorHelper;
import org.isoron.uhabits.helpers.ReminderHelper;
import org.isoron.uhabits.helpers.UIHelper;
import org.isoron.uhabits.models.Habit;
import org.isoron.uhabits.models.Score;
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.HabitScoreView;
import org.isoron.uhabits.views.HabitStreakView;
import org.isoron.uhabits.views.RepetitionCountView;
import org.isoron.uhabits.views.RingView;
import java.util.LinkedList;
@ -78,6 +76,8 @@ public class ShowHabitFragment extends Fragment
@Nullable
private SharedPreferences prefs;
private int previousScoreInterval;
@Override
public void onStart()
{
@ -90,7 +90,7 @@ public class ShowHabitFragment extends Fragment
{
View view = inflater.inflate(R.layout.show_habit, container, false);
activity = (ShowHabitActivity) getActivity();
habit = activity.habit;
habit = activity.getHabit();
dataViews = new LinkedList<>();
@ -102,19 +102,17 @@ public class ShowHabitFragment extends Fragment
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
int defaultScoreInterval = prefs.getInt("pref_score_view_interval", 1);
if(defaultScoreInterval > 5 || defaultScoreInterval < 0) defaultScoreInterval = 1;
previousScoreInterval = defaultScoreInterval;
setScoreBucketSize(defaultScoreInterval);
sStrengthInterval.setSelection(defaultScoreInterval);
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((HabitHistoryView) view.findViewById(R.id.historyView));
dataViews.add((HabitFrequencyView) view.findViewById(R.id.punchcardView));
LinearLayout llRepetition = (LinearLayout) view.findViewById(R.id.llRepetition);
for(int i = 0; i < llRepetition.getChildCount(); i++)
dataViews.add((RepetitionCountView) llRepetition.getChildAt(i));
dataViews.add((HabitStreakView) view.findViewById(R.id.streakView));
updateHeaders(view);
@ -145,11 +143,17 @@ public class ShowHabitFragment extends Fragment
}
setHasOptionsMenu(true);
refreshData();
return view;
}
@Override
public void onResume()
{
super.onResume();
refreshData();
}
private void updateScoreRing(View view)
{
if(habit == null) return;
@ -175,7 +179,6 @@ public class ShowHabitFragment extends Fragment
updateColor(view, R.id.tvStrength);
updateColor(view, R.id.tvStreaks);
updateColor(view, R.id.tvWeekdayFreq);
updateColor(view, R.id.tvCount);
}
private void updateColor(View view, int viewId)
@ -236,23 +239,24 @@ public class ShowHabitFragment extends Fragment
new BaseTask()
{
@Override
protected Void doInBackground(Void... params)
protected void doInBackground()
{
if(dataViews == null) return null;
if(dataViews == null) return;
updateScoreRing(getView());
int count = 0;
for(HabitDataView view : dataViews)
{
view.refreshData();
return null;
onProgressUpdate(count++);
}
}
@Override
protected void onPostExecute(Void aVoid)
protected void onProgressUpdate(Integer... values)
{
if(dataViews == null) return;
for(HabitDataView view : dataViews)
view.invalidate();
dataViews.get(values[0]).postInvalidate();
}
}.execute();
@ -267,17 +271,18 @@ public class ShowHabitFragment extends Fragment
private void setScoreBucketSize(int position)
{
if(scoreView == null) return;
int sizes[] = { 1, 7, 31, 92, 365 };
int size = sizes[position];
if(scoreView != null)
{
scoreView.setBucketSize(size);
refreshData();
}
if(position != previousScoreInterval) refreshData();
if(prefs != null)
prefs.edit().putInt("pref_score_view_interval", position).apply();
previousScoreInterval = position;
}
@Override

@ -91,7 +91,7 @@ public class HabitStreakView extends View implements HabitDataView
dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
rect = new RectF();
maxStreakCount = 10;
baseSize = getResources().getDimensionPixelSize(R.dimen.baseSize);
}

@ -53,75 +53,6 @@
app:maxDiameter="80"
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>

Loading…
Cancel
Save