Skip to content

Commit

Permalink
Fixed Notification bugs and layout changes.
Browse files Browse the repository at this point in the history
Fixed Notification bugs and layout changes.
  • Loading branch information
Pronoy999 authored Apr 30, 2018
2 parents 4fd9241 + b4c0955 commit c9ecb85
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class AddTodoDialog extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_todo_dialog);
setTitle(getResources().getString(R.string.addTodo));
initializeViews();
_pickerLayout.setVisibility(View.GONE);
addCategoryToList();
Expand Down Expand Up @@ -116,22 +117,27 @@ public void onClick(View view) {
_saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (_isReminder.isChecked()) {
if (!_date.getText().toString().equalsIgnoreCase("") &&
!_time.getText().toString().equalsIgnoreCase("") &&
!category.equalsIgnoreCase("") &&
try {
if (_isReminder.isChecked()) {
if (!_date.getText().toString().equalsIgnoreCase("") &&
!_time.getText().toString().equalsIgnoreCase("") &&
!category.equalsIgnoreCase("") &&
!priority.equalsIgnoreCase("")) {
saveReminder(true);
} else {
Messages.toastMessage(getApplicationContext(),
"Please select a date and time.", "");
}
} else if (!category.equalsIgnoreCase("") &&
!priority.equalsIgnoreCase("")) {
saveReminder(true);
saveReminder(false);
} else {
Messages.toastMessage(getApplicationContext(),
"Please select a date and time.", "");
"Please select all the fields.", "");
}
} else if (!category.equalsIgnoreCase("") &&
!priority.equalsIgnoreCase("")) {
saveReminder(false);
} else {
Messages.toastMessage(getApplicationContext(),
"Please select all the fields.", "");
} catch (NullPointerException e) {
Messages.logMessage(TAG_CLASS, e.toString());
Messages.toastMessage(getApplicationContext(), "Fill in the details.", "");
}
}
});
Expand Down Expand Up @@ -281,21 +287,25 @@ private void saveReminder(boolean isReminder) {
values.put(Constants.TODO_TABLE_DESC, desc);
values.put(Constants.TODO_TABLE_PRIOROTY, Integer.valueOf(priority));
values.put(Constants.TODO_TABLE_CATEGORYID, categoryId);
long reminderTime = 0;
if (isReminder) {
Calendar calendar = Calendar.getInstance();
calendar.set(yearSelected, monthSelected, daySelected, hourSelected, minuteSelected,0);
long reminderTime = calendar.getTimeInMillis();
calendar.set(yearSelected, monthSelected, daySelected, hourSelected, minuteSelected, 0);
reminderTime = calendar.getTimeInMillis();
Messages.logMessage(TAG_CLASS, "***" + reminderTime + "");
values.put(Constants.TODO_TABLE_TIME_MILIS, reminderTime);
setAlarm(title, desc, reminderTime);
} else {
values.put(Constants.TODO_TABLE_TIME_MILIS, 0);
}
if (Constants.databaseController.insertDataTodo(values) < 0) {
long id = Constants.databaseController.insertDataTodo(values);
if (id < 0) {
Messages.toastMessage(getApplicationContext(), "Couldn't save Reminder.", "");
finishActivity(false);
return;
}
if (isReminder) {
setAlarm(title, desc, reminderTime, (int) id);
}
Messages.toastMessage(getApplicationContext(), "Reminder saved.", "");
finishActivity(true);
} catch (NullPointerException | JSONException e) {
Expand All @@ -312,14 +322,14 @@ private void saveReminder(boolean isReminder) {
* @param desc: The description of the TO_DO.
* @param reminderTime: The exact time for the reminder.
*/
private void setAlarm(String title, String desc, long reminderTime) {
private void setAlarm(String title, String desc, long reminderTime, int id) {
Intent intent = new Intent(AddTodoDialog.this, AlarmReceiver.class);
Bundle bundle = new Bundle();
bundle.putString(Constants.TODO_TABLE_TITLE, title);
bundle.putString(Constants.TODO_TABLE_DESC, desc);
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AddTodoDialog.this,
Constants.NOTIFICATION_CHANNEL_ID,
id,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class TodoActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todo);
setTitle(getResources().getString(R.string.todoTitle));
initializeViews();
getAllData();
floatingActionButton.setOnClickListener(new View.OnClickListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@
public class TodoAdapter extends ArrayAdapter {
private ArrayList<Todo> todoList;
Context context;

public TodoAdapter(Activity context, ArrayList<Todo> todoList) {
super(context, 0, todoList);
this.context=context;
this.todoList=todoList;
this.context = context;
this.todoList = todoList;
}

@NonNull
Expand All @@ -60,24 +61,22 @@ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup
final Todo todo = todoList.get(position);
_title.setText(todo.getTitle());
_desc.setText(todo.getDesc());
String date=String.valueOf(todo.getDate());
String parts[]=date.split(":");
String newDate=parts[0]+":"+parts[1];
String date = String.valueOf(todo.getDate());
String parts[] = date.split(":");
String newDate = parts[0] + ":" + parts[1];
_time.setText(newDate);
String temp="Priority: "+todo.getPriority();
String temp = "Priority: " + todo.getPriority();
_priority.setText(String.valueOf(temp));
temp="Category: "+todo.getCategory();
temp = "Category: " + todo.getCategory();
_category.setText(temp);
_deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
todoList.remove(todo);
Constants.databaseController.deleteTodoById(todo.getId());
cancelAlarm(todo.getTitle(), todo.getDesc(), todo.getId());
Messages.snackbar(view, todo.getTitle() + " deleted.", "");
notifyDataSetChanged();
Calendar calendar=Calendar.getInstance();
calendar.setTime(todo.getDate());
cancelAlarm(todo.getTitle(),todo.getDesc(),calendar.getTimeInMillis());
Messages.snackbar(view,todo.getTitle()+" deleted.","");
}
});
_circle.setOnClickListener(new View.OnClickListener() {
Expand All @@ -87,16 +86,21 @@ public void onClick(View view) {
todoList.remove(todo);
Constants.databaseController.deleteTodoById(todo.getId());
notifyDataSetChanged();
Messages.snackbar(view,todo.getTitle()+" completed.","");
Messages.snackbar(view, todo.getTitle() + " completed.", "");
}
});
}
return todoItem;
}
private void cancelAlarm(String title, String desc, long reminderTime) {

private void cancelAlarm(String title, String desc, int id) {
Intent intent = new Intent(context, AlarmReceiver.class);
Bundle bundle = new Bundle();
bundle.putString(Constants.TODO_TABLE_TITLE, title);
bundle.putString(Constants.TODO_TABLE_DESC, desc);
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
Constants.NOTIFICATION_CHANNEL_ID,
id,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pronoy.mukhe.todoapplication.Helper;

import android.app.AlarmManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
Expand All @@ -9,6 +10,7 @@
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

Expand All @@ -34,19 +36,44 @@ public void onReceive(Context context, Intent intent) {
}
Intent todoIntent = new Intent(context, TodoActivity.class);
todoIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, todoIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notification_event)
.setContentTitle(title)
.setContentText(desc)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Constants.NOTIFICATION_CHANNEL_ID, builder.build());
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone ringtone = RingtoneManager.getRingtone(context, uri);
ringtone.play();
PendingIntent pendingIntent = PendingIntent.getActivity(context,
0,
todoIntent,
0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(String
.valueOf(Constants.NOTIFICATION_CHANNEL_ID),
Constants.NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,
channel.getId())
.setSmallIcon(R.drawable.notification_event)
.setContentTitle(title)
.setContentText(desc)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setChannelId(String.valueOf(Constants.NOTIFICATION_CHANNEL_ID))
.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Constants.NOTIFICATION_ID, builder.build());
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone ringtone = RingtoneManager.getRingtone(context, uri);
ringtone.play();
} else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notification_event)
.setContentTitle(title)
.setContentText(desc)
.setChannelId(String.valueOf(Constants.NOTIFICATION_CHANNEL_ID))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Constants.NOTIFICATION_ID, builder.build());
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone ringtone = RingtoneManager.getRingtone(context, uri);
ringtone.play();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class Constants {
public static final String LOG_IN_STATUS="loginStatus";
public static final int ADD_TODO_DIALOG_REQUEST_CODE =69;
public static final int ADD_CATEGORY_DIALOG_REQUEST_CODE=169;
public static final int NOTIFICATION_CHANNEL_ID=269;
public static final int PENDING_INTENT_REQUEST_CODE=55;
public static final int NOTIFICATION_ID =269;
public static final int NOTIFICATION_CHANNEL_ID=59;
public static final String NOTIFICATION_CHANNEL_NAME="Todo Tasks";
}
14 changes: 7 additions & 7 deletions app/src/main/res/layout/activity_add_category.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
android:id="@+id/categoryList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="10dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
Expand All @@ -22,15 +22,15 @@
app:layout_constraintVertical_bias="0.0" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/addCategory"
android:layout_width="50dp"
android:layout_height="90dp"
android:id="@+id/addCategory"
android:layout_marginBottom="40dp"
android:layout_marginBottom="36dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:src="@drawable/add_action"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.893"
app:layout_constraintHorizontal_bias="0.924"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
12 changes: 6 additions & 6 deletions app/src/main/res/layout/activity_todo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
android:id="@+id/todoList"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="0dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/addTodo"
android:layout_width="50dp"
android:layout_width="47dp"
android:layout_height="90dp"
android:layout_marginBottom="28dp"
android:layout_marginEnd="44dp"
android:layout_marginBottom="32dp"
android:layout_marginEnd="36dp"
android:src="@drawable/add_action"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
Expand Down
25 changes: 17 additions & 8 deletions app/src/main/res/layout/add_category_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="200dp">

<android.support.v7.widget.AppCompatEditText
android:id="@+id/categoryDescEnter"
android:textColor="#000"
android:inputType="textCapSentences"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
Expand All @@ -16,30 +17,38 @@
android:hint="@string/category_hint"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent">
<requestFocus/>
</android.support.v7.widget.AppCompatEditText>

<android.support.v7.widget.AppCompatButton
android:id="@+id/addCategoryButton"
android:layout_width="129dp"
android:layout_height="wrap_content"
android:layout_marginEnd="40dp"
android:layout_marginTop="76dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="48dp"
android:layout_marginTop="48dp"
android:background="@drawable/circle_button"
android:text="@string/add_category_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/categoryDescEnter" />
app:layout_constraintTop_toBottomOf="@+id/categoryDescEnter"
app:layout_constraintVertical_bias="0.0" />

<android.support.v7.widget.AppCompatButton
android:id="@+id/discardCategoryButton"
android:layout_width="129dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="76dp"
android:layout_marginTop="48dp"
android:background="@drawable/circle_button"
android:text="@string/discard"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/addCategoryButton"
app:layout_constraintHorizontal_bias="0.317"
app:layout_constraintHorizontal_bias="0.514"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/categoryDescEnter" />
app:layout_constraintTop_toBottomOf="@+id/categoryDescEnter"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
Loading

0 comments on commit c9ecb85

Please sign in to comment.