mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Simplify MainActivity and decouple ListHabitsFragment from MainActivity
This commit is contained in:
@@ -1,15 +1,19 @@
|
|||||||
package org.isoron.helpers;
|
package org.isoron.helpers;
|
||||||
|
|
||||||
|
|
||||||
public abstract class Command {
|
public abstract class Command
|
||||||
public abstract void execute();
|
{
|
||||||
public abstract void undo();
|
public abstract void execute();
|
||||||
|
|
||||||
public Integer getExecuteStringId() {
|
public abstract void undo();
|
||||||
return null;
|
|
||||||
}
|
public Integer getExecuteStringId()
|
||||||
|
{
|
||||||
public Integer getUndoStringId() {
|
return null;
|
||||||
return null;
|
}
|
||||||
}
|
|
||||||
|
public Integer getUndoStringId()
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
85
app/src/main/java/org/isoron/helpers/ReplayableActivity.java
Normal file
85
app/src/main/java/org/isoron/helpers/ReplayableActivity.java
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
package org.isoron.helpers;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.R;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
abstract public class ReplayableActivity extends Activity
|
||||||
|
{
|
||||||
|
private static int MAX_UNDO_LEVEL = 15;
|
||||||
|
|
||||||
|
private LinkedList<Command> undoList;
|
||||||
|
private LinkedList<Command> redoList;
|
||||||
|
private Toast toast;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState)
|
||||||
|
{
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
|
undoList = new LinkedList<>();
|
||||||
|
redoList = new LinkedList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void executeCommand(Command command)
|
||||||
|
{
|
||||||
|
executeCommand(command, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void undo()
|
||||||
|
{
|
||||||
|
if (undoList.isEmpty())
|
||||||
|
{
|
||||||
|
showToast(R.string.toast_nothing_to_undo);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Command last = undoList.pop();
|
||||||
|
redoList.push(last);
|
||||||
|
last.undo();
|
||||||
|
showToast(last.getUndoStringId());
|
||||||
|
|
||||||
|
onCommandExecuted(last);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void redo()
|
||||||
|
{
|
||||||
|
if (redoList.isEmpty())
|
||||||
|
{
|
||||||
|
showToast(R.string.toast_nothing_to_redo);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Command last = redoList.pop();
|
||||||
|
executeCommand(last, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void showToast(Integer stringId)
|
||||||
|
{
|
||||||
|
if (stringId == null) return;
|
||||||
|
if (toast == null) toast = Toast.makeText(this, stringId, Toast.LENGTH_SHORT);
|
||||||
|
else toast.setText(stringId);
|
||||||
|
toast.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void executeCommand(Command command, boolean clearRedoStack)
|
||||||
|
{
|
||||||
|
undoList.push(command);
|
||||||
|
if (undoList.size() > MAX_UNDO_LEVEL) undoList.removeLast();
|
||||||
|
if (clearRedoStack) redoList.clear();
|
||||||
|
|
||||||
|
command.execute();
|
||||||
|
onCommandExecuted(command);
|
||||||
|
|
||||||
|
showToast(command.getExecuteStringId());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void onCommandExecuted(Command command)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,19 +1,17 @@
|
|||||||
package org.isoron.uhabits;
|
package org.isoron.uhabits;
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.app.AlarmManager;
|
import android.app.AlarmManager;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.AsyncTask;
|
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
|
import org.isoron.helpers.ReplayableActivity;
|
||||||
import org.isoron.helpers.Command;
|
import org.isoron.helpers.Command;
|
||||||
import org.isoron.uhabits.dialogs.ListHabitsFragment;
|
import org.isoron.uhabits.dialogs.ListHabitsFragment;
|
||||||
import org.isoron.uhabits.models.Habit;
|
import org.isoron.uhabits.models.Habit;
|
||||||
@@ -21,16 +19,11 @@ import org.isoron.uhabits.models.Habit;
|
|||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.LinkedList;
|
|
||||||
|
|
||||||
public class MainActivity extends Activity
|
public class MainActivity extends ReplayableActivity
|
||||||
|
implements ListHabitsFragment.OnHabitClickListener
|
||||||
{
|
{
|
||||||
private static int MAX_UNDO_LEVEL = 15;
|
|
||||||
private ListHabitsFragment listHabitsFragment;
|
private ListHabitsFragment listHabitsFragment;
|
||||||
private LinkedList<Command> undoList;
|
|
||||||
private LinkedList<Command> redoList;
|
|
||||||
|
|
||||||
private Toast toast;
|
|
||||||
|
|
||||||
public static void createReminderAlarms(Context context)
|
public static void createReminderAlarms(Context context)
|
||||||
{
|
{
|
||||||
@@ -63,20 +56,21 @@ public class MainActivity extends Activity
|
|||||||
alarmIntent.setData(uri);
|
alarmIntent.setData(uri);
|
||||||
|
|
||||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
|
||||||
((int) (habit.getId() % Integer.MAX_VALUE)) + 1,
|
((int) (habit.getId() % Integer.MAX_VALUE)) + 1, alarmIntent,
|
||||||
alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
|
|
||||||
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||||
if (Build.VERSION.SDK_INT >= 19)
|
if (Build.VERSION.SDK_INT >= 19)
|
||||||
{
|
{
|
||||||
manager.setExact(AlarmManager.RTC_WAKEUP, reminderTime, pendingIntent);
|
manager.setExact(AlarmManager.RTC_WAKEUP, reminderTime, pendingIntent);
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
manager.set(AlarmManager.RTC_WAKEUP, reminderTime, pendingIntent);
|
manager.set(AlarmManager.RTC_WAKEUP, reminderTime, pendingIntent);
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.d("Alarm", String.format("Setting alarm (%s): %s", DateFormat.getDateTimeInstance()
|
Log.d("Alarm", String.format("Setting alarm (%s): %s",
|
||||||
.format(new Date(reminderTime)), habit.name));
|
DateFormat.getDateTimeInstance().format(new Date(reminderTime)), habit.name));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -84,19 +78,13 @@ public class MainActivity extends Activity
|
|||||||
{
|
{
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
if (android.os.Build.VERSION.SDK_INT >= 21)
|
if (android.os.Build.VERSION.SDK_INT >= 21) getActionBar().setElevation(5);
|
||||||
getActionBar().setElevation(5);
|
|
||||||
|
|
||||||
setContentView(R.layout.list_habits_activity);
|
setContentView(R.layout.list_habits_activity);
|
||||||
|
|
||||||
listHabitsFragment = (ListHabitsFragment) getFragmentManager().findFragmentById(
|
listHabitsFragment = (ListHabitsFragment) getFragmentManager().findFragmentById(
|
||||||
R.id.fragment1);
|
R.id.fragment1);
|
||||||
|
|
||||||
Log.d("MainActivity", "Creating activity");
|
|
||||||
|
|
||||||
undoList = new LinkedList<>();
|
|
||||||
redoList = new LinkedList<>();
|
|
||||||
|
|
||||||
createReminderAlarms(MainActivity.this);
|
createReminderAlarms(MainActivity.this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,7 +93,6 @@ public class MainActivity extends Activity
|
|||||||
{
|
{
|
||||||
super.onStart();
|
super.onStart();
|
||||||
listHabitsFragment.notifyDataSetChanged();
|
listHabitsFragment.notifyDataSetChanged();
|
||||||
Log.d("MainActivity", "Starting activity");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -126,61 +113,19 @@ public class MainActivity extends Activity
|
|||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void executeCommand(Command command)
|
|
||||||
|
@Override
|
||||||
|
public void onHabitClicked(Habit habit)
|
||||||
{
|
{
|
||||||
executeCommand(command, false);
|
Intent intent = new Intent(this, ShowHabitActivity.class);
|
||||||
|
intent.setData(Uri.parse("content://org.isoron.uhabits/habit/" + habit.getId()));
|
||||||
|
startActivity(intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void executeCommand(Command command, boolean clearRedoStack)
|
@Override
|
||||||
|
protected void onCommandExecuted(Command command)
|
||||||
{
|
{
|
||||||
undoList.push(command);
|
|
||||||
if (undoList.size() > MAX_UNDO_LEVEL)
|
|
||||||
undoList.removeLast();
|
|
||||||
if (clearRedoStack)
|
|
||||||
redoList.clear();
|
|
||||||
|
|
||||||
command.execute();
|
|
||||||
listHabitsFragment.notifyDataSetChanged();
|
listHabitsFragment.notifyDataSetChanged();
|
||||||
|
|
||||||
showToast(command.getExecuteStringId());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void undo()
|
|
||||||
{
|
|
||||||
if (undoList.isEmpty())
|
|
||||||
{
|
|
||||||
showToast(R.string.toast_nothing_to_undo);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Command last = undoList.pop();
|
|
||||||
redoList.push(last);
|
|
||||||
last.undo();
|
|
||||||
showToast(last.getUndoStringId());
|
|
||||||
|
|
||||||
listHabitsFragment.notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void redo()
|
|
||||||
{
|
|
||||||
if (redoList.isEmpty())
|
|
||||||
{
|
|
||||||
showToast(R.string.toast_nothing_to_redo);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Command last = redoList.pop();
|
|
||||||
executeCommand(last, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void showToast(Integer stringId)
|
|
||||||
{
|
|
||||||
if (stringId == null)
|
|
||||||
return;
|
|
||||||
if (toast == null)
|
|
||||||
toast = Toast.makeText(this, stringId, Toast.LENGTH_SHORT);
|
|
||||||
else
|
|
||||||
toast.setText(stringId);
|
|
||||||
toast.show();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.isoron.uhabits.dialogs;
|
package org.isoron.uhabits.dialogs;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
import android.app.Fragment;
|
import android.app.Fragment;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
@@ -55,6 +56,11 @@ public class ListHabitsFragment extends Fragment
|
|||||||
OnClickListener
|
OnClickListener
|
||||||
{
|
{
|
||||||
|
|
||||||
|
public interface OnHabitClickListener
|
||||||
|
{
|
||||||
|
public void onHabitClicked(Habit habit);
|
||||||
|
}
|
||||||
|
|
||||||
ListHabitsAdapter adapter;
|
ListHabitsAdapter adapter;
|
||||||
DragSortListView listView;
|
DragSortListView listView;
|
||||||
MainActivity mainActivity;
|
MainActivity mainActivity;
|
||||||
@@ -64,6 +70,8 @@ public class ListHabitsFragment extends Fragment
|
|||||||
private int button_count;
|
private int button_count;
|
||||||
private View llEmpty;
|
private View llEmpty;
|
||||||
|
|
||||||
|
private OnHabitClickListener habitClickListener;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||||
Bundle savedInstanceState)
|
Bundle savedInstanceState)
|
||||||
@@ -108,6 +116,13 @@ public class ListHabitsFragment extends Fragment
|
|||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAttach(Activity activity)
|
||||||
|
{
|
||||||
|
super.onAttach(activity);
|
||||||
|
habitClickListener = (OnHabitClickListener) activity;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResume()
|
public void onResume()
|
||||||
{
|
{
|
||||||
@@ -120,7 +135,7 @@ public class ListHabitsFragment extends Fragment
|
|||||||
LayoutInflater inflater = mainActivity.getLayoutInflater();
|
LayoutInflater inflater = mainActivity.getLayoutInflater();
|
||||||
View view = getView();
|
View view = getView();
|
||||||
|
|
||||||
if(view == null) return;
|
if (view == null) return;
|
||||||
|
|
||||||
GregorianCalendar day = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
|
GregorianCalendar day = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
|
||||||
day.setTimeInMillis(DateHelper.getStartOfDay(DateHelper.getLocalTime()));
|
day.setTimeInMillis(DateHelper.getStartOfDay(DateHelper.getLocalTime()));
|
||||||
@@ -128,8 +143,6 @@ public class ListHabitsFragment extends Fragment
|
|||||||
LinearLayout llButtonsHeader = (LinearLayout) view.findViewById(R.id.llButtonsHeader);
|
LinearLayout llButtonsHeader = (LinearLayout) view.findViewById(R.id.llButtonsHeader);
|
||||||
llButtonsHeader.removeAllViews();
|
llButtonsHeader.removeAllViews();
|
||||||
|
|
||||||
Random r = new Random();
|
|
||||||
|
|
||||||
for (int i = 0; i < button_count; i++)
|
for (int i = 0; i < button_count; i++)
|
||||||
{
|
{
|
||||||
View check = inflater.inflate(R.layout.list_habits_header_check, null);
|
View check = inflater.inflate(R.layout.list_habits_header_check, null);
|
||||||
@@ -203,10 +216,7 @@ public class ListHabitsFragment extends Fragment
|
|||||||
if (new Date().getTime() - lastLongClick < 1000) return;
|
if (new Date().getTime() - lastLongClick < 1000) return;
|
||||||
|
|
||||||
Habit habit = Habit.getByPosition(position);
|
Habit habit = Habit.getByPosition(position);
|
||||||
|
habitClickListener.onHabitClicked(habit);
|
||||||
Intent intent = new Intent(getActivity(), ShowHabitActivity.class);
|
|
||||||
intent.setData(Uri.parse("content://org.isoron.uhabits/habit/" + habit.getId()));
|
|
||||||
startActivity(intent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user