Skip to content
This repository was archived by the owner on Nov 18, 2024. It is now read-only.

Commit f00e405

Browse files
committed
Added general notification feature + Bug fixes
1 parent b1aefe9 commit f00e405

9 files changed

Lines changed: 178 additions & 6 deletions

File tree

app/src/main/java/apps/jizzu/simpletodo/activity/MainActivity.java

Lines changed: 126 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package apps.jizzu.simpletodo.activity;
22

3+
import android.app.Notification;
4+
import android.app.NotificationChannel;
5+
import android.app.NotificationManager;
6+
import android.app.PendingIntent;
37
import android.appwidget.AppWidgetManager;
48
import android.content.ComponentName;
59
import android.content.Context;
610
import android.content.Intent;
11+
import android.graphics.Color;
712
import android.os.Bundle;
813
import android.os.Handler;
914
import android.support.design.widget.FloatingActionButton;
15+
import android.support.v4.app.NotificationCompat;
1016
import android.support.v4.content.ContextCompat;
1117
import android.support.v7.app.AppCompatActivity;
1218
import android.support.v7.widget.LinearLayoutManager;
@@ -34,6 +40,7 @@
3440
import apps.jizzu.simpletodo.adapter.RecyclerViewAdapter;
3541
import apps.jizzu.simpletodo.adapter.RecyclerViewEmptySupport;
3642
import apps.jizzu.simpletodo.alarm.AlarmHelper;
43+
import apps.jizzu.simpletodo.alarm.AlarmReceiver;
3744
import apps.jizzu.simpletodo.database.DBHelper;
3845
import apps.jizzu.simpletodo.model.ModelTask;
3946
import apps.jizzu.simpletodo.settings.SettingsActivity;
@@ -49,7 +56,7 @@
4956
import static android.content.ContentValues.TAG;
5057

5158

52-
public class MainActivity extends AppCompatActivity {
59+
public class MainActivity extends AppCompatActivity implements RecyclerViewAdapter.UpdateNotificationDataCallback {
5360

5461
private RecyclerViewEmptySupport mRecyclerView;
5562
public RecyclerViewAdapter mAdapter;
@@ -60,6 +67,7 @@ public class MainActivity extends AppCompatActivity {
6067
private RecyclerView.LayoutManager mLayoutManager;
6168
public static boolean mSearchViewIsOpen;
6269
private MaterialSearchView mSearchView;
70+
private NotificationManager mNotificationManager;
6371
public static boolean mShowAnimation;
6472
public static boolean mActivityIsShown;
6573

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

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

114-
ItemTouchHelper.Callback callback = new ListItemTouchHelper(mAdapter, mRecyclerView);
122+
mAdapter.registerCallback(this);
123+
124+
ItemTouchHelper.Callback callback = new ListItemTouchHelper(mAdapter, mRecyclerView) {
125+
@Override
126+
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
127+
super.onMove(recyclerView, viewHolder, target);
128+
updateGeneralNotification();
129+
return true;
130+
}
131+
132+
@Override
133+
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
134+
super.onSwiped(viewHolder, direction);
135+
updateGeneralNotification();
136+
}
137+
};
115138
ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
116139
touchHelper.attachToRecyclerView(mRecyclerView);
117140

@@ -126,7 +149,7 @@ protected void onCreate(Bundle savedInstanceState) {
126149
@Override
127150
public boolean onQueryTextSubmit(String query) {
128151
Log.d(TAG, "onQueryTextSubmit");
129-
return false;
152+
return true;
130153
}
131154

132155
@Override
@@ -259,6 +282,103 @@ public void updateWidget() {
259282
sendBroadcast(intent);
260283
}
261284

285+
/**
286+
* Updates general notification data.
287+
*/
288+
public void updateGeneralNotification() {
289+
if (mPreferenceHelper.getBoolean(PreferenceHelper.GENERAL_NOTIFICATION_IS_ON)) {
290+
if (mAdapter.getItemCount() != 0) {
291+
showGeneralNotification();
292+
} else {
293+
removeGeneralNotification();
294+
}
295+
} else {
296+
removeGeneralNotification();
297+
}
298+
}
299+
300+
/**
301+
* Set up and show general notification.
302+
*/
303+
public void showGeneralNotification() {
304+
StringBuilder stringBuilder = new StringBuilder();
305+
306+
Intent resultIntent = new Intent(this, MainActivity.class);
307+
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
308+
309+
for (ModelTask task : RecyclerViewAdapter.mItems) {
310+
stringBuilder.append("• ").append(task.getTitle());
311+
312+
if (task.getPosition() < mAdapter.getItemCount() - 1) {
313+
stringBuilder.append("\n\n");
314+
}
315+
}
316+
317+
String notificationTitle = "";
318+
switch (mAdapter.getItemCount() % 10) {
319+
case 1:
320+
notificationTitle = getString(R.string.general_notification_1) + " " + mAdapter.getItemCount() + " " + getString(R.string.general_notification_2);
321+
break;
322+
323+
case 2:
324+
case 3:
325+
case 4:
326+
notificationTitle = getString(R.string.general_notification_1) + " " + mAdapter.getItemCount() + " " + getString(R.string.general_notification_3);
327+
break;
328+
329+
case 0:
330+
case 5:
331+
case 6:
332+
case 7:
333+
case 8:
334+
case 9:
335+
notificationTitle = getString(R.string.general_notification_1) + " " + mAdapter.getItemCount() + " " + getString(R.string.general_notification_4);
336+
break;
337+
}
338+
339+
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
340+
341+
// Set NotificationChannel for Android Oreo
342+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
343+
NotificationChannel channel = new NotificationChannel(AlarmReceiver.CHANNEL_ID, "SimpleToDo Notifications",
344+
NotificationManager.IMPORTANCE_HIGH);
345+
channel.enableLights(true);
346+
channel.setLightColor(Color.GREEN);
347+
channel.enableVibration(true);
348+
mNotificationManager.createNotificationChannel(channel);
349+
}
350+
351+
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, AlarmReceiver.CHANNEL_ID)
352+
.setContentTitle(notificationTitle)
353+
.setContentText(stringBuilder.toString())
354+
.setNumber(mAdapter.getItemCount())
355+
.setStyle(new NotificationCompat.BigTextStyle().bigText(stringBuilder.toString()))
356+
.setColor(ContextCompat.getColor(this, R.color.colorAccent))
357+
.setSmallIcon(R.drawable.ic_check_circle_white_24dp)
358+
.setContentIntent(resultPendingIntent)
359+
.setOngoing(true);
360+
361+
Notification notification = builder.build();
362+
mNotificationManager.notify(1, notification);
363+
}
364+
365+
/**
366+
* Removes general notification.
367+
*/
368+
public void removeGeneralNotification() {
369+
if (mNotificationManager != null) {
370+
mNotificationManager.cancel(1);
371+
}
372+
}
373+
374+
/**
375+
* Updates general notification data when user click the "Cancel" snackbar button.
376+
*/
377+
@Override
378+
public void updateData() {
379+
updateGeneralNotification();
380+
}
381+
262382
@Override
263383
public boolean onCreateOptionsMenu(Menu menu) {
264384
getMenuInflater().inflate(R.menu.menu, menu);
@@ -327,6 +447,7 @@ public void run() {
327447
mFab.setVisibility(View.VISIBLE);
328448
}
329449
MyApplication.activityResumed();
450+
updateGeneralNotification();
330451
}
331452

332453
@Override
@@ -365,6 +486,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
365486
long id = mHelper.saveTask(task);
366487
task.setId(id);
367488
mAdapter.addItem(task);
489+
updateGeneralNotification();
368490
}
369491

370492
@Override

app/src/main/java/apps/jizzu/simpletodo/adapter/RecyclerViewAdapter.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,27 @@
3636
*/
3737
public class RecyclerViewAdapter extends RecyclerViewEmptySupport.Adapter<RecyclerView.ViewHolder> {
3838

39+
/**
40+
* Callback for update general notification data from another class.
41+
*/
42+
public interface UpdateNotificationDataCallback {
43+
void updateData();
44+
}
45+
3946
public static List<ModelTask> mItems = new ArrayList<>();
4047
private DBHelper mHelper = DBHelper.getInstance(MainActivity.mContext);
4148
private AlarmHelper mAlarmHelper = AlarmHelper.getInstance();
4249
private Context mContext;
4350
private static RecyclerViewAdapter mInstance;
4451
private boolean mCancelButtonIsClicked;
52+
private UpdateNotificationDataCallback mCallback;
53+
54+
/**
55+
* Registers callback from another class.
56+
*/
57+
public void registerCallback(UpdateNotificationDataCallback callback) {
58+
mCallback = callback;
59+
}
4560

4661
/**
4762
* Constructor is private to prevent direct instantiation.
@@ -63,7 +78,7 @@ public static RecyclerViewAdapter getInstance() {
6378
/**
6479
* Custom OnClickListener which is needed to pass task id for the Snackbar onClick() method.
6580
*/
66-
class CustomOnClickListener implements View.OnClickListener {
81+
public static class CustomOnClickListener implements View.OnClickListener {
6782
long taskID;
6883

6984
public CustomOnClickListener(long taskID) {
@@ -128,6 +143,7 @@ public void onClick(View view) {
128143
addItem(task, task.getPosition());
129144
isRemoved[0] = false;
130145
}
146+
mCallback.updateData();
131147
}
132148
});
133149

app/src/main/java/apps/jizzu/simpletodo/alarm/AlarmReceiver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
public class AlarmReceiver extends BroadcastReceiver {
2222

23-
private static final String CHANNEL_ID = "1";
23+
public static final String CHANNEL_ID = "1";
2424

2525
@Override
2626
public void onReceive(Context context, Intent intent) {

app/src/main/java/apps/jizzu/simpletodo/settings/SettingsFragment.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class SettingsFragment extends PreferenceFragment {
2020

2121
PreferenceHelper mPreferenceHelper;
2222
CheckBoxPreference mAnimation;
23+
CheckBoxPreference mGeneralNotification;
2324

2425
@Override
2526
public void onCreate(Bundle savedInstanceState) {
@@ -42,6 +43,19 @@ public boolean onPreferenceClick(Preference preference) {
4243
}
4344
});
4445

46+
mGeneralNotification = (CheckBoxPreference) findPreference("general_notification");
47+
mGeneralNotification.setChecked(mPreferenceHelper.getBoolean(PreferenceHelper.GENERAL_NOTIFICATION_IS_ON));
48+
49+
mGeneralNotification.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
50+
@Override
51+
public boolean onPreferenceClick(Preference preference) {
52+
53+
mPreferenceHelper.putBoolean(PreferenceHelper.GENERAL_NOTIFICATION_IS_ON, mGeneralNotification.isChecked());
54+
55+
return true;
56+
}
57+
});
58+
4559
Preference rateThisApp = findPreference("rate_app");
4660
rateThisApp.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
4761
@Override

app/src/main/java/apps/jizzu/simpletodo/utils/PreferenceHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
public class PreferenceHelper {
1010

1111
public static final String ANIMATION_IS_ON = "animation_is_on";
12+
public static final String GENERAL_NOTIFICATION_IS_ON = "general_notification_is_on";
1213

1314
private static PreferenceHelper mInstance;
1415
private Context mContext;
-337 Bytes
Binary file not shown.

app/src/main/res/values-ru/strings.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

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

56+
<string name="general_notification_1">У вас есть</string>
57+
<string name="general_notification_2">активная задача:</string>
58+
<string name="general_notification_3">активных задачи:</string>
59+
<string name="general_notification_4">активных задач:</string>
60+
5461
////////////////<!-- "What's New?" page -->////////////////
5562
<string name="whats_new_title">Что нового?</string>
5663
<string name="whats_new_button_text">Продолжить</string>

app/src/main/res/values/strings.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
<string name="preferences_animation_title">Enable animations</string>
2020
<string name="preferences_animation_summary">Enable animations for UI elements</string>
21+
<string name="preferences_general_notification_title">Enable general notification</string>
22+
<string name="preferences_general_notification_summary">Show general notification with you current task list</string>
2123
<string name="preferences_rate_app_title">Rate this app</string>
2224
<string name="preferences_rate_app_summary">Rate this app on Google Play store</string>
2325
<string name="preferences_send_feedback_title">Send feedback</string>
@@ -52,6 +54,11 @@
5254
<string name="dialog_title"><b>Are you sure?</b></string>
5355
<string name="dialog_message">Delete this task?</string>
5456

57+
<string name="general_notification_1">You have</string>
58+
<string name="general_notification_2">active task:</string>
59+
<string name="general_notification_3">active tasks:</string>
60+
<string name="general_notification_4">active tasks:</string>
61+
5562
////////////////<!-- "What's New?" page -->////////////////
5663
<string name="whats_new_title">What\'s New?</string>
5764
<string name="whats_new_button_text">Continue</string>

app/src/main/res/xml/preferences.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
android:key="animation"
77
android:summary="@string/preferences_animation_summary"
88
android:title="@string/preferences_animation_title" />
9+
<CheckBoxPreference
10+
android:defaultValue="true"
11+
android:key="general_notification"
12+
android:summary="@string/preferences_general_notification_summary"
13+
android:title="@string/preferences_general_notification_title" />
914
<PreferenceCategory android:title="@string/category_additional" />
1015
<Preference
1116
android:key="rate_app"

0 commit comments

Comments
 (0)