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;
|
||||
|
||||
|
||||
public abstract class Command {
|
||||
public abstract class Command
|
||||
{
|
||||
public abstract void execute();
|
||||
|
||||
public abstract void undo();
|
||||
|
||||
public Integer getExecuteStringId() {
|
||||
public Integer getExecuteStringId()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public Integer getUndoStringId() {
|
||||
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;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlarmManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.isoron.helpers.ReplayableActivity;
|
||||
import org.isoron.helpers.Command;
|
||||
import org.isoron.uhabits.dialogs.ListHabitsFragment;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
@@ -21,16 +19,11 @@ import org.isoron.uhabits.models.Habit;
|
||||
import java.text.DateFormat;
|
||||
import java.util.Calendar;
|
||||
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 LinkedList<Command> undoList;
|
||||
private LinkedList<Command> redoList;
|
||||
|
||||
private Toast toast;
|
||||
|
||||
public static void createReminderAlarms(Context context)
|
||||
{
|
||||
@@ -63,20 +56,21 @@ public class MainActivity extends Activity
|
||||
alarmIntent.setData(uri);
|
||||
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
|
||||
((int) (habit.getId() % Integer.MAX_VALUE)) + 1,
|
||||
alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
((int) (habit.getId() % Integer.MAX_VALUE)) + 1, alarmIntent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
if (Build.VERSION.SDK_INT >= 19)
|
||||
{
|
||||
manager.setExact(AlarmManager.RTC_WAKEUP, reminderTime, pendingIntent);
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
manager.set(AlarmManager.RTC_WAKEUP, reminderTime, pendingIntent);
|
||||
}
|
||||
|
||||
Log.d("Alarm", String.format("Setting alarm (%s): %s", DateFormat.getDateTimeInstance()
|
||||
.format(new Date(reminderTime)), habit.name));
|
||||
Log.d("Alarm", String.format("Setting alarm (%s): %s",
|
||||
DateFormat.getDateTimeInstance().format(new Date(reminderTime)), habit.name));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -84,19 +78,13 @@ public class MainActivity extends Activity
|
||||
{
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
if (android.os.Build.VERSION.SDK_INT >= 21)
|
||||
getActionBar().setElevation(5);
|
||||
if (android.os.Build.VERSION.SDK_INT >= 21) getActionBar().setElevation(5);
|
||||
|
||||
setContentView(R.layout.list_habits_activity);
|
||||
|
||||
listHabitsFragment = (ListHabitsFragment) getFragmentManager().findFragmentById(
|
||||
R.id.fragment1);
|
||||
|
||||
Log.d("MainActivity", "Creating activity");
|
||||
|
||||
undoList = new LinkedList<>();
|
||||
redoList = new LinkedList<>();
|
||||
|
||||
createReminderAlarms(MainActivity.this);
|
||||
}
|
||||
|
||||
@@ -105,7 +93,6 @@ public class MainActivity extends Activity
|
||||
{
|
||||
super.onStart();
|
||||
listHabitsFragment.notifyDataSetChanged();
|
||||
Log.d("MainActivity", "Starting activity");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -126,61 +113,19 @@ public class MainActivity extends Activity
|
||||
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();
|
||||
|
||||
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;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Fragment;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
@@ -55,6 +56,11 @@ public class ListHabitsFragment extends Fragment
|
||||
OnClickListener
|
||||
{
|
||||
|
||||
public interface OnHabitClickListener
|
||||
{
|
||||
public void onHabitClicked(Habit habit);
|
||||
}
|
||||
|
||||
ListHabitsAdapter adapter;
|
||||
DragSortListView listView;
|
||||
MainActivity mainActivity;
|
||||
@@ -64,6 +70,8 @@ public class ListHabitsFragment extends Fragment
|
||||
private int button_count;
|
||||
private View llEmpty;
|
||||
|
||||
private OnHabitClickListener habitClickListener;
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState)
|
||||
@@ -108,6 +116,13 @@ public class ListHabitsFragment extends Fragment
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Activity activity)
|
||||
{
|
||||
super.onAttach(activity);
|
||||
habitClickListener = (OnHabitClickListener) activity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume()
|
||||
{
|
||||
@@ -128,8 +143,6 @@ public class ListHabitsFragment extends Fragment
|
||||
LinearLayout llButtonsHeader = (LinearLayout) view.findViewById(R.id.llButtonsHeader);
|
||||
llButtonsHeader.removeAllViews();
|
||||
|
||||
Random r = new Random();
|
||||
|
||||
for (int i = 0; i < button_count; i++)
|
||||
{
|
||||
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;
|
||||
|
||||
Habit habit = Habit.getByPosition(position);
|
||||
|
||||
Intent intent = new Intent(getActivity(), ShowHabitActivity.class);
|
||||
intent.setData(Uri.parse("content://org.isoron.uhabits/habit/" + habit.getId()));
|
||||
startActivity(intent);
|
||||
habitClickListener.onHabitClicked(habit);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user