Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anniversaries #6

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import com.djonique.birdays.adapters.FamousFragmentAdapter;
import com.djonique.birdays.alarm.AlarmHelper;
import com.djonique.birdays.database.DbHelper;
import com.djonique.birdays.models.AnniversaryType;
import com.djonique.birdays.models.DisplayedAge;
import com.djonique.birdays.models.Person;
import com.djonique.birdays.utils.Constants;
import com.djonique.birdays.utils.Utils;
Expand Down Expand Up @@ -82,10 +84,14 @@ public class DetailActivity extends AppCompatActivity {
RelativeLayout rlDaysSinceBirthday;
@BindView(R.id.textview_detail_since)
TextView tvDaysSinceBirthday;
@BindView(R.id.textview_detail_label)
TextView tvAnniversaryLabel;
@BindView(R.id.imageview_detail_zodiac)
ImageView ivZodiacSign;
@BindView(R.id.textview_detail_zodiac)
TextView tvZodiacSign;
@BindView(R.id.textview_detail_zodiac_label)
TextView tvZodiacSignLabel;
@BindView(R.id.cardview_detail_info)
CardView cardViewInfo;
@BindView(R.id.relativelayout_detail_phone)
Expand All @@ -103,7 +109,8 @@ public class DetailActivity extends AppCompatActivity {
private DbHelper dbHelper;
private Person person;
private long timeStamp, date;
private String phoneNumber, email, displayedAge;
private String phoneNumber, email;
private DisplayedAge displayedAge;
private boolean yearUnknown;

@Override
Expand All @@ -114,7 +121,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean adEnabled = preferences.getBoolean(getString(R.string.ad_interstitial_key), true);
displayedAge = preferences.getString(Constants.DISPLAYED_AGE_KEY, "0");
displayedAge = Utils.getDisplayedAge(preferences.getString(Constants.DISPLAYED_AGE_KEY, DisplayedAge.CURRENT.name()));

/*
* Interstitial doesn't work on Android API 26+
Expand Down Expand Up @@ -202,6 +209,7 @@ private void setupUI() {
setSeasonImage();

tvDaysLeft.setText(Utils.daysLeft(this, date));
tvAnniversaryLabel.setText(person.getAnniversaryLabel());

if (yearUnknown) {
tvDate.setText(Utils.getDateWithoutYear(date));
Expand All @@ -213,9 +221,16 @@ private void setupUI() {
tvDaysSinceBirthday.setText(Utils.daysSinceBirthday(date));
}

int zodiacId = Utils.getZodiacId(date);
tvZodiacSign.setText(getString(zodiacId));
ivZodiacSign.setImageResource(Utils.getZodiacImage(zodiacId));
if (person.getAnniversaryType() == AnniversaryType.BIRTHDAY) {
int zodiacId = Utils.getZodiacId(date);
tvZodiacSign.setText(getString(zodiacId));
ivZodiacSign.setImageResource(Utils.getZodiacImage(zodiacId));
}
else {
tvZodiacSign.setVisibility(View.GONE);
tvZodiacSignLabel.setVisibility(View.GONE);
ivZodiacSign.setVisibility(View.GONE);
}

if (isEmpty(phoneNumber) && isEmpty(email))
cardViewInfo.setVisibility(View.GONE);
Expand Down Expand Up @@ -284,7 +299,7 @@ private void deletePersonDialog(final Person person) {
@Override
public void onClick(DialogInterface dialog, int which) {

new AlarmHelper(getApplicationContext()).removeAlarms(timeStamp);
new AlarmHelper(getApplicationContext()).removeAlarms(person);
dbHelper.removeRecord(timeStamp);
Utils.notifyWidget(getApplicationContext());
dialog.dismiss();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private String updateText(EditText editText) {

private void setAlarms(Person person) {
AlarmHelper alarmHelper = new AlarmHelper(this);
alarmHelper.removeAlarms(person.getTimeStamp());
alarmHelper.removeAlarms(person);
alarmHelper.setAlarms(person);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.os.Bundle;
import android.os.PowerManager;
import android.preference.ListPreference;
import android.preference.MultiSelectListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
Expand All @@ -53,7 +54,10 @@
import com.djonique.birdays.utils.PermissionManager;
import com.djonique.birdays.utils.Utils;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SettingsActivity extends AppCompatActivity implements ContactsHelper.LoadingContactsListener {

Expand Down Expand Up @@ -182,21 +186,47 @@ public static class BirdaysPreferenceFragment extends PreferenceFragment

private SharedPreferences preferences;

private String getAdditionalNotificationsSelectedValues(MultiSelectListPreference preference) {
final Map<CharSequence, CharSequence> names = new LinkedHashMap<>();

for (int i = 0; i < preference.getEntryValues().length; i++) {
names.put(preference.getEntryValues()[i], preference.getEntries()[i]);
}
StringBuilder sb = new StringBuilder();
for (CharSequence candidate : names.keySet()) {
if (preference.getValues().contains(candidate)) {
sb.append(names.get(candidate)).append(", ");
}
}
if (sb.length() > 2) {
sb.setLength(sb.length() - 2); //remove last ', '
}
else {
sb.append(getActivity().getApplicationContext().getText(R.string.never));
}

return sb.toString();
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);

preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());

MultiSelectListPreference multi = (MultiSelectListPreference) findPreference(Constants.ADDITIONAL_NOTIFICATION_KEY);
multi.setSummary(getAdditionalNotificationsSelectedValues(multi));

/*
* Set summary for additional notification
*/
findPreference(Constants.ADDITIONAL_NOTIFICATION_KEY).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
((ListPreference) preference).setValue(newValue.toString());
preference.setSummary(((ListPreference) preference).getEntry());
public boolean onPreferenceChange(Preference uncastPreference, Object newValue) {
final MultiSelectListPreference preference = (MultiSelectListPreference) uncastPreference;
preference.setValues((Set<String>) newValue);
preference.setSummary(getAdditionalNotificationsSelectedValues(preference));
return true;
}
});
Expand Down Expand Up @@ -424,13 +454,11 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
new Thread(new Runnable() {
@Override
public void run() {
if (isChecked) {
for (Person person : persons) {
for (Person person : persons) {
if (isChecked) {
alarmHelper.setAlarms(person);
}
} else {
for (Person person : persons) {
alarmHelper.removeAlarms(person.getTimeStamp());
} else {
alarmHelper.removeAlarms(person);
}
}
}
Expand All @@ -452,32 +480,29 @@ public void run() {
@Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}

private void restartAlarms(final AlarmHelper alarmHelper, final List<Person> persons) {
new Thread(new Runnable() {
@Override
public void run() {
for (Person person : persons) {
alarmHelper.removeAlarms(person.getTimeStamp());
alarmHelper.removeAlarms(person);
alarmHelper.setAlarms(person);
}
}
}).start();
}

private void restartApp() {
Intent intent = getActivity().getPackageManager()
.getLaunchIntentForPackage(getActivity().getPackageName());
Intent intent = getActivity().getPackageManager().getLaunchIntentForPackage(getActivity().getPackageName());
if (intent != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Expand Down
Loading