Skip to content
This repository has been archived by the owner on Apr 10, 2021. It is now read-only.

Commit

Permalink
Added general notification feature + Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jizzu committed Mar 14, 2018
1 parent b1aefe9 commit f00e405
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 6 deletions.
130 changes: 126 additions & 4 deletions app/src/main/java/apps/jizzu/simpletodo/activity/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package apps.jizzu.simpletodo.activity;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
Expand Down Expand Up @@ -34,6 +40,7 @@
import apps.jizzu.simpletodo.adapter.RecyclerViewAdapter;
import apps.jizzu.simpletodo.adapter.RecyclerViewEmptySupport;
import apps.jizzu.simpletodo.alarm.AlarmHelper;
import apps.jizzu.simpletodo.alarm.AlarmReceiver;
import apps.jizzu.simpletodo.database.DBHelper;
import apps.jizzu.simpletodo.model.ModelTask;
import apps.jizzu.simpletodo.settings.SettingsActivity;
Expand All @@ -49,7 +56,7 @@
import static android.content.ContentValues.TAG;


public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements RecyclerViewAdapter.UpdateNotificationDataCallback {

private RecyclerViewEmptySupport mRecyclerView;
public RecyclerViewAdapter mAdapter;
Expand All @@ -60,6 +67,7 @@ public class MainActivity extends AppCompatActivity {
private RecyclerView.LayoutManager mLayoutManager;
public static boolean mSearchViewIsOpen;
private MaterialSearchView mSearchView;
private NotificationManager mNotificationManager;
public static boolean mShowAnimation;
public static boolean mActivityIsShown;

Expand All @@ -74,7 +82,7 @@ protected void onCreate(Bundle savedInstanceState) {

// Set up "What's New" screen
WhatsNew whatsNew = WhatsNew.newInstance(
new WhatsNewItem(getString(R.string.whats_new_item_1_title), getString(R.string.whats_new_item_1_text), R.drawable.whats_new_release)
new WhatsNewItem(getString(R.string.whats_new_item_1_title), getString(R.string.whats_new_item_1_text))
);
whatsNew.setTitleColor(ContextCompat.getColor(this, R.color.colorAccent));
whatsNew.setTitleText(getString(R.string.whats_new_title));
Expand Down Expand Up @@ -111,7 +119,22 @@ protected void onCreate(Bundle savedInstanceState) {
mRecyclerView.setEmptyView(mEmptyView);
mFab = findViewById(R.id.fab);

ItemTouchHelper.Callback callback = new ListItemTouchHelper(mAdapter, mRecyclerView);
mAdapter.registerCallback(this);

ItemTouchHelper.Callback callback = new ListItemTouchHelper(mAdapter, mRecyclerView) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
super.onMove(recyclerView, viewHolder, target);
updateGeneralNotification();
return true;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
super.onSwiped(viewHolder, direction);
updateGeneralNotification();
}
};
ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
touchHelper.attachToRecyclerView(mRecyclerView);

Expand All @@ -126,7 +149,7 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public boolean onQueryTextSubmit(String query) {
Log.d(TAG, "onQueryTextSubmit");
return false;
return true;
}

@Override
Expand Down Expand Up @@ -259,6 +282,103 @@ public void updateWidget() {
sendBroadcast(intent);
}

/**
* Updates general notification data.
*/
public void updateGeneralNotification() {
if (mPreferenceHelper.getBoolean(PreferenceHelper.GENERAL_NOTIFICATION_IS_ON)) {
if (mAdapter.getItemCount() != 0) {
showGeneralNotification();
} else {
removeGeneralNotification();
}
} else {
removeGeneralNotification();
}
}

/**
* Set up and show general notification.
*/
public void showGeneralNotification() {
StringBuilder stringBuilder = new StringBuilder();

Intent resultIntent = new Intent(this, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

for (ModelTask task : RecyclerViewAdapter.mItems) {
stringBuilder.append("• ").append(task.getTitle());

if (task.getPosition() < mAdapter.getItemCount() - 1) {
stringBuilder.append("\n\n");
}
}

String notificationTitle = "";
switch (mAdapter.getItemCount() % 10) {
case 1:
notificationTitle = getString(R.string.general_notification_1) + " " + mAdapter.getItemCount() + " " + getString(R.string.general_notification_2);
break;

case 2:
case 3:
case 4:
notificationTitle = getString(R.string.general_notification_1) + " " + mAdapter.getItemCount() + " " + getString(R.string.general_notification_3);
break;

case 0:
case 5:
case 6:
case 7:
case 8:
case 9:
notificationTitle = getString(R.string.general_notification_1) + " " + mAdapter.getItemCount() + " " + getString(R.string.general_notification_4);
break;
}

mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

// Set NotificationChannel for Android Oreo
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(AlarmReceiver.CHANNEL_ID, "SimpleToDo Notifications",
NotificationManager.IMPORTANCE_HIGH);
channel.enableLights(true);
channel.setLightColor(Color.GREEN);
channel.enableVibration(true);
mNotificationManager.createNotificationChannel(channel);
}

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, AlarmReceiver.CHANNEL_ID)
.setContentTitle(notificationTitle)
.setContentText(stringBuilder.toString())
.setNumber(mAdapter.getItemCount())
.setStyle(new NotificationCompat.BigTextStyle().bigText(stringBuilder.toString()))
.setColor(ContextCompat.getColor(this, R.color.colorAccent))
.setSmallIcon(R.drawable.ic_check_circle_white_24dp)
.setContentIntent(resultPendingIntent)
.setOngoing(true);

Notification notification = builder.build();
mNotificationManager.notify(1, notification);
}

/**
* Removes general notification.
*/
public void removeGeneralNotification() {
if (mNotificationManager != null) {
mNotificationManager.cancel(1);
}
}

/**
* Updates general notification data when user click the "Cancel" snackbar button.
*/
@Override
public void updateData() {
updateGeneralNotification();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
Expand Down Expand Up @@ -327,6 +447,7 @@ public void run() {
mFab.setVisibility(View.VISIBLE);
}
MyApplication.activityResumed();
updateGeneralNotification();
}

@Override
Expand Down Expand Up @@ -365,6 +486,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
long id = mHelper.saveTask(task);
task.setId(id);
mAdapter.addItem(task);
updateGeneralNotification();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,27 @@
*/
public class RecyclerViewAdapter extends RecyclerViewEmptySupport.Adapter<RecyclerView.ViewHolder> {

/**
* Callback for update general notification data from another class.
*/
public interface UpdateNotificationDataCallback {
void updateData();
}

public static List<ModelTask> mItems = new ArrayList<>();
private DBHelper mHelper = DBHelper.getInstance(MainActivity.mContext);
private AlarmHelper mAlarmHelper = AlarmHelper.getInstance();
private Context mContext;
private static RecyclerViewAdapter mInstance;
private boolean mCancelButtonIsClicked;
private UpdateNotificationDataCallback mCallback;

/**
* Registers callback from another class.
*/
public void registerCallback(UpdateNotificationDataCallback callback) {
mCallback = callback;
}

/**
* Constructor is private to prevent direct instantiation.
Expand All @@ -63,7 +78,7 @@ public static RecyclerViewAdapter getInstance() {
/**
* Custom OnClickListener which is needed to pass task id for the Snackbar onClick() method.
*/
class CustomOnClickListener implements View.OnClickListener {
public static class CustomOnClickListener implements View.OnClickListener {
long taskID;

public CustomOnClickListener(long taskID) {
Expand Down Expand Up @@ -128,6 +143,7 @@ public void onClick(View view) {
addItem(task, task.getPosition());
isRemoved[0] = false;
}
mCallback.updateData();
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
public class AlarmReceiver extends BroadcastReceiver {

private static final String CHANNEL_ID = "1";
public static final String CHANNEL_ID = "1";

@Override
public void onReceive(Context context, Intent intent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class SettingsFragment extends PreferenceFragment {

PreferenceHelper mPreferenceHelper;
CheckBoxPreference mAnimation;
CheckBoxPreference mGeneralNotification;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand All @@ -42,6 +43,19 @@ public boolean onPreferenceClick(Preference preference) {
}
});

mGeneralNotification = (CheckBoxPreference) findPreference("general_notification");
mGeneralNotification.setChecked(mPreferenceHelper.getBoolean(PreferenceHelper.GENERAL_NOTIFICATION_IS_ON));

mGeneralNotification.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {

mPreferenceHelper.putBoolean(PreferenceHelper.GENERAL_NOTIFICATION_IS_ON, mGeneralNotification.isChecked());

return true;
}
});

Preference rateThisApp = findPreference("rate_app");
rateThisApp.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class PreferenceHelper {

public static final String ANIMATION_IS_ON = "animation_is_on";
public static final String GENERAL_NOTIFICATION_IS_ON = "general_notification_is_on";

private static PreferenceHelper mInstance;
private Context mContext;
Expand Down
Binary file removed app/src/main/res/drawable/whats_new_release.png
Binary file not shown.
7 changes: 7 additions & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

<string name="preferences_animation_title">Включить анимацию</string>
<string name="preferences_animation_summary">Включить анимацию для элементов интерфейса</string>
<string name="preferences_general_notification_title">Включить общее уведомление</string>
<string name="preferences_general_notification_summary">Показывать отдельное уведомление с вашим текущим списком задач</string>
<string name="preferences_rate_app_title">Оценить приложение</string>
<string name="preferences_rate_app_summary">Оцените это приложение в Google Play</string>
<string name="preferences_send_feedback_title">Написать автору</string>
Expand Down Expand Up @@ -51,6 +53,11 @@
<string name="dialog_title"><b>Вы уверены?</b></string>
<string name="dialog_message">Удалить эту задачу?</string>

<string name="general_notification_1">У вас есть</string>
<string name="general_notification_2">активная задача:</string>
<string name="general_notification_3">активных задачи:</string>
<string name="general_notification_4">активных задач:</string>

////////////////<!-- "What's New?" page -->////////////////
<string name="whats_new_title">Что нового?</string>
<string name="whats_new_button_text">Продолжить</string>
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

<string name="preferences_animation_title">Enable animations</string>
<string name="preferences_animation_summary">Enable animations for UI elements</string>
<string name="preferences_general_notification_title">Enable general notification</string>
<string name="preferences_general_notification_summary">Show general notification with you current task list</string>
<string name="preferences_rate_app_title">Rate this app</string>
<string name="preferences_rate_app_summary">Rate this app on Google Play store</string>
<string name="preferences_send_feedback_title">Send feedback</string>
Expand Down Expand Up @@ -52,6 +54,11 @@
<string name="dialog_title"><b>Are you sure?</b></string>
<string name="dialog_message">Delete this task?</string>

<string name="general_notification_1">You have</string>
<string name="general_notification_2">active task:</string>
<string name="general_notification_3">active tasks:</string>
<string name="general_notification_4">active tasks:</string>

////////////////<!-- "What's New?" page -->////////////////
<string name="whats_new_title">What\'s New?</string>
<string name="whats_new_button_text">Continue</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
android:key="animation"
android:summary="@string/preferences_animation_summary"
android:title="@string/preferences_animation_title" />
<CheckBoxPreference
android:defaultValue="true"
android:key="general_notification"
android:summary="@string/preferences_general_notification_summary"
android:title="@string/preferences_general_notification_title" />
<PreferenceCategory android:title="@string/category_additional" />
<Preference
android:key="rate_app"
Expand Down

0 comments on commit f00e405

Please sign in to comment.