mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Implement hints
This commit is contained in:
@@ -19,8 +19,10 @@ package org.isoron.helpers;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.DialogInterface.OnClickListener;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Typeface;
|
||||
import android.os.Vibrator;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.view.View;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
import android.widget.TextView;
|
||||
@@ -49,4 +51,18 @@ public abstract class DialogHelper
|
||||
Vibrator vb = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
|
||||
vb.vibrate(duration);
|
||||
}
|
||||
|
||||
|
||||
public static void incrementLaunchCount(Context context)
|
||||
{
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
int count = prefs.getInt("launch_count", 0);
|
||||
prefs.edit().putInt("launch_count", count + 1).apply();
|
||||
}
|
||||
|
||||
public static int getLaunchCount(Context context)
|
||||
{
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
return prefs.getInt("launch_count", 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import android.preference.PreferenceManager;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import org.isoron.helpers.DialogHelper;
|
||||
import org.isoron.helpers.ReplayableActivity;
|
||||
import org.isoron.uhabits.fragments.ListHabitsFragment;
|
||||
import org.isoron.uhabits.helpers.ReminderHelper;
|
||||
@@ -33,6 +34,7 @@ public class MainActivity extends ReplayableActivity
|
||||
implements ListHabitsFragment.OnHabitClickListener
|
||||
{
|
||||
private ListHabitsFragment listHabitsFragment;
|
||||
SharedPreferences prefs;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState)
|
||||
@@ -40,20 +42,23 @@ public class MainActivity extends ReplayableActivity
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.list_habits_activity);
|
||||
|
||||
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
|
||||
|
||||
prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
listHabitsFragment =
|
||||
(ListHabitsFragment) getFragmentManager().findFragmentById(R.id.fragment1);
|
||||
|
||||
onStartup();
|
||||
}
|
||||
|
||||
private void onStartup()
|
||||
{
|
||||
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
|
||||
ReminderHelper.createReminderAlarms(MainActivity.this);
|
||||
|
||||
DialogHelper.incrementLaunchCount(this);
|
||||
showTutorial();
|
||||
|
||||
}
|
||||
|
||||
private void showTutorial()
|
||||
{
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
Boolean firstRun = prefs.getBoolean("pref_first_run", true);
|
||||
|
||||
if (firstRun)
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.isoron.uhabits.fragments;
|
||||
|
||||
import android.animation.Animator;
|
||||
import android.animation.AnimatorListenerAdapter;
|
||||
import android.app.Activity;
|
||||
import android.app.Fragment;
|
||||
import android.content.Context;
|
||||
@@ -68,6 +70,9 @@ public class ListHabitsFragment extends Fragment
|
||||
{
|
||||
public static final int INACTIVE_COLOR = Color.rgb(230, 230, 230);
|
||||
|
||||
public static final int HINT_INTERVAL = 5;
|
||||
public static final int HINT_INTERVAL_OFFSET = 2;
|
||||
|
||||
public interface OnHabitClickListener
|
||||
{
|
||||
void onHabitClicked(Habit habit);
|
||||
@@ -82,12 +87,14 @@ public class ListHabitsFragment extends Fragment
|
||||
private int tvNameWidth;
|
||||
private int buttonCount;
|
||||
private View llEmpty;
|
||||
private View llHint;
|
||||
|
||||
private OnHabitClickListener habitClickListener;
|
||||
private boolean isShortToggleEnabled;
|
||||
|
||||
private HabitListLoader loader;
|
||||
boolean showArchived;
|
||||
private boolean showArchived;
|
||||
private SharedPreferences prefs;
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
@@ -106,7 +113,6 @@ public class ListHabitsFragment extends Fragment
|
||||
tvNameHeader = (TextView) view.findViewById(R.id.tvNameHeader);
|
||||
|
||||
ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
|
||||
progressBar.setVisibility(View.INVISIBLE);
|
||||
loader.setProgressBar(progressBar);
|
||||
|
||||
adapter = new ListHabitsAdapter(getActivity());
|
||||
@@ -126,6 +132,9 @@ public class ListHabitsFragment extends Fragment
|
||||
listView.setOnTouchListener(controller);
|
||||
listView.setDragEnabled(true);
|
||||
|
||||
llHint = view.findViewById(R.id.llHint);
|
||||
llHint.setOnClickListener(this);
|
||||
|
||||
Typeface fontawesome = Typeface.createFromAsset(getActivity().getAssets(),
|
||||
"fontawesome-webfont.ttf");
|
||||
((TextView) view.findViewById(R.id.tvStarEmpty)).setTypeface(fontawesome);
|
||||
@@ -134,6 +143,8 @@ public class ListHabitsFragment extends Fragment
|
||||
loader.updateAllHabits(true);
|
||||
setHasOptionsMenu(true);
|
||||
|
||||
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
@@ -142,8 +153,10 @@ public class ListHabitsFragment extends Fragment
|
||||
public void onAttach(Activity activity)
|
||||
{
|
||||
super.onAttach(activity);
|
||||
habitClickListener = (OnHabitClickListener) activity;
|
||||
this.activity = (ReplayableActivity) activity;
|
||||
|
||||
habitClickListener = (OnHabitClickListener) activity;
|
||||
prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -157,9 +170,9 @@ public class ListHabitsFragment extends Fragment
|
||||
|
||||
updateEmptyMessage();
|
||||
updateHeader();
|
||||
adapter.notifyDataSetChanged();
|
||||
showNextHint();
|
||||
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
adapter.notifyDataSetChanged();
|
||||
isShortToggleEnabled = prefs.getBoolean("pref_short_toggle", false);
|
||||
}
|
||||
|
||||
@@ -346,6 +359,44 @@ public class ListHabitsFragment extends Fragment
|
||||
activity.executeCommand(c, refreshKey);
|
||||
}
|
||||
|
||||
private void hideHint()
|
||||
{
|
||||
llHint.animate().alpha(0f).setDuration(500).setListener(new AnimatorListenerAdapter()
|
||||
{
|
||||
@Override
|
||||
public void onAnimationEnd(Animator animation)
|
||||
{
|
||||
llHint.setVisibility(View.GONE);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void showNextHint()
|
||||
{
|
||||
int launchCount = DialogHelper.getLaunchCount(activity);
|
||||
if(launchCount % HINT_INTERVAL == HINT_INTERVAL_OFFSET)
|
||||
{
|
||||
int lastHint = prefs.getInt("last_shown_hint", -1);
|
||||
showHint(lastHint + 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void showHint(int number)
|
||||
{
|
||||
String[] hints = activity.getResources().getStringArray(R.array.hints);
|
||||
if(number >= hints.length) return;
|
||||
|
||||
prefs.edit().putInt("last_shown_hint", number).apply();
|
||||
|
||||
TextView tvContent = (TextView) llHint.findViewById(R.id.hintContent);
|
||||
tvContent.setText(hints[number]);
|
||||
|
||||
llHint.setAlpha(0.0f);
|
||||
llHint.setVisibility(View.VISIBLE);
|
||||
llHint.animate().alpha(1f).setDuration(500);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drop(int from, int to)
|
||||
{
|
||||
@@ -363,6 +414,10 @@ public class ListHabitsFragment extends Fragment
|
||||
if (isShortToggleEnabled) toggleCheck(v);
|
||||
else activity.showToast(R.string.long_press_to_toggle);
|
||||
break;
|
||||
|
||||
case R.id.llHint:
|
||||
hideHint();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -60,4 +60,32 @@
|
||||
android:layout_marginTop="37dp"
|
||||
/>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/llHint"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@color/indigo_500"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:orientation="vertical"
|
||||
android:animateLayoutChanges="true"
|
||||
android:visibility="invisible"
|
||||
style="@style/cardStyle">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/hint_title"
|
||||
android:textStyle="bold"
|
||||
android:textColor="@color/white"
|
||||
android:layout_weight="5"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/hintContent"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textColor="@color/white"
|
||||
android:layout_weight="5"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</RelativeLayout>
|
||||
@@ -101,4 +101,13 @@
|
||||
<string name="settings">Settings</string>
|
||||
<string name="snooze_interval">Snooze interval</string>
|
||||
|
||||
<string name="hint_title">Did you know?</string>
|
||||
<string name="hint_drag">To rearrange the entries, you can drag them by the star.</string>
|
||||
<string name="hint_landscape">You can see more days by putting your phone in landscape mode.</string>
|
||||
|
||||
<string-array name="hints">
|
||||
<item>@string/hint_drag</item>
|
||||
<item>@string/hint_landscape</item>
|
||||
</string-array>
|
||||
|
||||
</resources>
|
||||
Reference in New Issue
Block a user