Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
iSoron committed Oct 11, 2017
1 parent 819a8d3 commit a201273
Show file tree
Hide file tree
Showing 13 changed files with 189 additions and 233 deletions.
5 changes: 5 additions & 0 deletions android-pickers/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

compileOptions {
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public interface OnTimeSetListener {
*/
void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute);

void onTimeCleared(RadialPickerLayout view);
default void onTimeCleared(RadialPickerLayout view) {}
}

public TimePickerDialog() {
Expand Down
2 changes: 1 addition & 1 deletion uhabits-android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.habits.list.ListHabitsActivity"/>
</activity>
<activity android:name=".notifications.SnoozeDelayActivity"
<activity android:name=".notifications.SnoozeDelayPickerActivity"
android:excludeFromRecents="true"
android:launchMode="singleInstance"
android:theme="@android:style/Theme.Translucent.NoTitleBar">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.isoron.uhabits.core.ui.screens.habits.list.*;
import org.isoron.uhabits.core.utils.*;
import org.isoron.uhabits.intents.*;
import org.isoron.uhabits.receivers.*;
import org.isoron.uhabits.sync.*;
import org.isoron.uhabits.tasks.*;
import org.isoron.uhabits.widgets.*;
Expand Down Expand Up @@ -80,6 +81,8 @@ public interface HabitsApplicationComponent

ReminderScheduler getReminderScheduler();

ReminderController getReminderController();

SyncManager getSyncManager();

TaskRunner getTaskRunner();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.isoron.uhabits.notifications;

import android.os.*;
import android.support.annotation.*;
import android.support.v4.app.*;
import android.support.v7.app.*;
import android.text.format.*;
import android.view.*;
import android.widget.*;

import com.android.datetimepicker.time.*;

import org.isoron.uhabits.*;
import org.isoron.uhabits.R;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.receivers.*;

import java.util.*;

import butterknife.*;

import static android.content.ContentUris.*;

public class SnoozeDelayPickerActivity extends AppCompatActivity implements AdapterView
.OnItemClickListener
{
public static final String ACTION_ASK_SNOOZE = "org.isoron.uhabits.ACTION_ASK_SNOOZE";

private Habit habit;

private ReminderController reminderController;

@BindArray(R.array.snooze_picker_names)
protected String[] snoozeNames;

@BindArray(R.array.snooze_picker_values)
protected int[] snoozeValues;

@Override
protected void onCreate(@Nullable Bundle bundle)
{
super.onCreate(bundle);
if (getIntent() == null) finish();
if (getIntent().getData() == null) finish();

HabitsApplication app = (HabitsApplication) getApplicationContext();
HabitsApplicationComponent appComponent = app.getComponent();
reminderController = appComponent.getReminderController();
habit = appComponent.getHabitList().getById(parseId(getIntent().getData()));
if (habit == null) finish();

setTheme(R.style.Theme_AppCompat_Light_Dialog_Alert);
ButterKnife.bind(this);

ListView listView = new ListView(this);
listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
snoozeNames));
listView.setOnItemClickListener(this);
setContentView(listView);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
if (snoozeValues[position] >= 0)
{
reminderController.onSnoozeDelayPicked(habit, snoozeValues[position]);
finish();
}
else showTimePicker();
}

private void showTimePicker()
{
final Calendar calendar = Calendar.getInstance();
int defaultHour = calendar.get(Calendar.HOUR_OF_DAY);
int defaultMinute = calendar.get(Calendar.MINUTE);
TimePickerDialog dialog = TimePickerDialog.newInstance(
(view, hour, minute) ->
reminderController.onSnoozeTimePicked(habit, hour, minute),
defaultHour, defaultMinute, DateFormat.is24HourFormat(this));

dialog.show(getSupportFragmentManager(), "timePicker");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@
import android.net.*;
import android.support.annotation.*;

import org.isoron.uhabits.core.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.preferences.*;
import org.isoron.uhabits.core.reminders.*;
import org.isoron.uhabits.core.ui.*;
import org.isoron.uhabits.core.utils.*;
import org.isoron.uhabits.notifications.*;

import javax.inject.*;

import static org.isoron.uhabits.core.utils.DateUtils.applyTimezone;
import static org.isoron.uhabits.core.utils.DateUtils.getLocalTime;

@ReceiverScope
@AppScope
public class ReminderController
{
@NonNull
Expand All @@ -43,6 +42,7 @@ public class ReminderController
@NonNull
private final NotificationTray notificationTray;

@NonNull
private Preferences preferences;

@Inject
Expand All @@ -68,38 +68,46 @@ public void onShowReminder(@NonNull Habit habit,
reminderScheduler.scheduleAll();
}

public void onSnooze(@NonNull Habit habit, final Context context)
public void onSnoozePressed(@NonNull Habit habit, final Context context)
{
long snoozeInterval = preferences.getSnoozeInterval();

if (snoozeInterval < 0)
{
context.sendBroadcast( new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
Intent intent = new Intent( SnoozeDelayActivity.ACTION_ASK_SNOOZE, Uri.parse( habit.getUriString()),
context, SnoozeDelayActivity.class );
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
return;
}

snoozeNotificationAddDelay(habit, snoozeInterval);
long delay = preferences.getSnoozeInterval();

if (delay < 0)
showSnoozeDelayPicker(habit, context);
else
scheduleReminderMinutesFromNow(habit, delay);
}

public void snoozeNotificationAddDelay(@NonNull Habit habit, long snoozeInterval)
public void onSnoozeDelayPicked(Habit habit, int delay)
{
long now = applyTimezone(getLocalTime());
long reminderTime = now + snoozeInterval * 60 * 1000;
snoozeNotificationSetReminderTime(habit, reminderTime);
scheduleReminderMinutesFromNow(habit, delay);
}

public void snoozeNotificationSetReminderTime(@NonNull Habit habit, long reminderTime)
public void onSnoozeTimePicked(Habit habit, int hour, int minute)
{
reminderScheduler.schedule(habit, reminderTime);
Long time = DateUtils.getUpcomingTimeInMillis(hour, minute);
reminderScheduler.scheduleAtTime(habit, time);
notificationTray.cancel(habit);
}

public void onDismiss(@NonNull Habit habit)
{
notificationTray.cancel(habit);
}

private void scheduleReminderMinutesFromNow(Habit habit, long minutes)
{
reminderScheduler.scheduleMinutesFromNow(habit, minutes);
notificationTray.cancel(habit);
}

private void showSnoozeDelayPicker(@NonNull Habit habit, Context context)
{
context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
Intent intent = new Intent(SnoozeDelayPickerActivity.ACTION_ASK_SNOOZE,
Uri.parse(habit.getUriString()),
context, SnoozeDelayPickerActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
Loading

0 comments on commit a201273

Please sign in to comment.