Fixed multiple notification sound bug (yet to be tested), new booleans for AsyncTask

This commit is contained in:
Deniz Düzgören
2019-05-23 14:28:41 +02:00
parent a41d5127c5
commit 75d9ae25d8
2 changed files with 88 additions and 74 deletions
@@ -21,9 +21,19 @@ import java.lang.NullPointerException
import java.net.URL import java.net.URL
import java.net.URLConnection import java.net.URLConnection
class DataFetcher(isjobservice: Boolean, context: Context, application: Application, view: View) : AsyncTask<Void, Void, Void>() { class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, context: Context, application: Application, view: View) : AsyncTask<Void, Void, Void>() {
var jobservice = isjobservice var jobservice = isjobservice
var plan = isplan
var menu = ismenu
/* booleans to improve performance when fetching data
"plan" is required to fetch the substitution data
"menu" is required to fetch the food menu
"jobservice" is required to send a notification
"jobservice" = true has no effect unless "plan" is also true
*/
var mContext = context var mContext = context
var mApplication = application var mApplication = application
var mView = view var mView = view
@@ -81,6 +91,7 @@ class DataFetcher(isjobservice: Boolean, context: Context, application: Applicat
edit.putString("informational", informational).apply() edit.putString("informational", informational).apply()
if (successfulAttempt) { if (successfulAttempt) {
if (menu) {
var foodInt = 0 var foodInt = 0
while (foodInt in 0 until foodElements.size) { while (foodInt in 0 until foodElements.size) {
try { // what a mess this is! try { // what a mess this is!
@@ -130,7 +141,9 @@ class DataFetcher(isjobservice: Boolean, context: Context, application: Applicat
} }
easyPrefs.putListString("foodListPrefs", foodList) easyPrefs.putListString("foodListPrefs", foodList)
}
if (plan) {
substViewModel.deleteAllSubst() substViewModel.deleteAllSubst()
for (i in 0 until rows.size) { for (i in 0 until rows.size) {
@@ -173,7 +186,9 @@ class DataFetcher(isjobservice: Boolean, context: Context, application: Applicat
} }
} }
} }
}
}
if (jobservice) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channel = NotificationChannel(channelId, channelName, importance) channel = NotificationChannel(channelId, channelName, importance)
channel.enableLights(true) channel.enableLights(true)
@@ -212,7 +227,6 @@ class DataFetcher(isjobservice: Boolean, context: Context, application: Applicat
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
manager.notify(1, notification) // TODO notification plays multiple times manager.notify(1, notification) // TODO notification plays multiple times
} }
} }
} }
} }
@@ -101,7 +101,7 @@ public class FragmentPlan extends Fragment {
if (prefs.getInt("firstTimeOpening", 0) == 2) { if (prefs.getInt("firstTimeOpening", 0) == 2) {
if (!prefs.getBoolean("notif", false)) { if (!prefs.getBoolean("notif", false)) {
pullToRefresh.setRefreshing(true); pullToRefresh.setRefreshing(true);
new DataFetcher(false, getContext(), getActivity().getApplication(), getView().getRootView()).execute(); new DataFetcher(true, false, false, getContext(), getActivity().getApplication(), getView().getRootView()).execute();
edit.putInt("firstTimeOpening", 3); edit.putInt("firstTimeOpening", 3);
edit.apply(); edit.apply();
} }
@@ -109,7 +109,7 @@ public class FragmentPlan extends Fragment {
if (prefs.getBoolean("autoRefresh", false)) { if (prefs.getBoolean("autoRefresh", false)) {
pullToRefresh.setRefreshing(true); pullToRefresh.setRefreshing(true);
new DataFetcher(false, getContext(), getActivity().getApplication(), getView().getRootView()).execute(); new DataFetcher(true, false, false, getContext(), getActivity().getApplication(), getView().getRootView()).execute();
bottomSheetText.setText(prefs.getString("informational", getString(R.string.noinfo))); bottomSheetText.setText(prefs.getString("informational", getString(R.string.noinfo)));
} }
substViewModel = ViewModelProviders.of(getActivity()).get(SubstViewModel.class); substViewModel = ViewModelProviders.of(getActivity()).get(SubstViewModel.class);
@@ -127,7 +127,7 @@ public class FragmentPlan extends Fragment {
@Override @Override
public void onRefresh() { public void onRefresh() {
pullToRefresh.setRefreshing(true); pullToRefresh.setRefreshing(true);
new DataFetcher(true, getContext(), getActivity().getApplication(), getView().getRootView()).execute(); // TODO test for notification new DataFetcher(true, false, true, getContext(), getActivity().getApplication(), getView().getRootView()).execute(); // TODO test for notification
bottomSheetText.setText(prefs.getString("informational", getString(R.string.noinfo))); bottomSheetText.setText(prefs.getString("informational", getString(R.string.noinfo)));
} }
}); });