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

Part-4: Compiler Warnings as Errors - WordPress Module - FragmentManager and PreferenceFragment #19876

Closed
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 @@ -26,7 +26,20 @@ public class NotificationsSettings {
public enum Channel {
OTHER,
BLOGS,
WPCOM
WPCOM;

public static Channel toNotificationChannel(Integer ordinal) {
switch (ordinal) {
case 0:
return OTHER;
case 1:
return BLOGS;
case 2:
return WPCOM;
default:
throw new IllegalArgumentException("Ordinal does not conform to any existing enum.");
}
}
}

// The notification setting type, used in BLOGS and OTHER channels
Expand All @@ -35,6 +48,19 @@ public enum Type {
EMAIL,
DEVICE;

public static Type toNotificationType(Integer ordinal) {
switch (ordinal) {
case 0:
return TIMELINE;
case 1:
return EMAIL;
case 2:
return DEVICE;
default:
throw new IllegalArgumentException("Ordinal does not conform to any existing enum.");
}
}

public String toString() {
switch (this) {
case TIMELINE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@
import org.wordpress.android.ui.prefs.homepage.HomepageSettingsDialog;
import org.wordpress.android.ui.prefs.language.LocalePickerBottomSheet;
import org.wordpress.android.ui.prefs.notifications.NotificationsSettingsFragment;
import org.wordpress.android.ui.prefs.notifications.NotificationsSettingsMySitesFragment;
import org.wordpress.android.ui.prefs.notifications.NotificationsSettingsTypesFragment;
import org.wordpress.android.ui.prefs.timezone.SiteSettingsTimezoneBottomSheet;
import org.wordpress.android.ui.publicize.PublicizeAccountChooserListAdapter;
import org.wordpress.android.ui.publicize.PublicizeButtonPrefsFragment;
Expand Down Expand Up @@ -294,6 +296,10 @@ public interface AppComponent {

void inject(NotificationsSettingsFragment object);

void inject(NotificationsSettingsTypesFragment object);

void inject(NotificationsSettingsMySitesFragment object);

void inject(NotificationsDetailActivity object);

void inject(NotificationsPendingDraftsReceiver object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class RequestCodes {
public static final int SMART_LOCK_SAVE = 1400;
public static final int SMART_LOCK_READ = 1500;
public static final int NOTIFICATION_SETTINGS = 1600;
public static final int NOTIFICATION_SETTINGS_ALERT_RINGTONE = 1610;
public static final int ACTIVITY_LOG_DETAIL = 1700;
public static final int BACKUP_DOWNLOAD = 1710;
public static final int RESTORE = 1720;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.wordpress.android.R;

/**@see WPSwitchPreferenceX*/
public class WPSwitchPreference extends SwitchPreference implements PreferenceHint {
private String mHint;
private ColorStateList mTint;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package org.wordpress.android.ui.prefs

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.ColorStateList
import android.text.TextUtils
import android.util.AttributeSet
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Switch
import android.widget.TextView
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.view.ViewCompat
import androidx.preference.PreferenceViewHolder
import androidx.preference.SwitchPreference
import org.wordpress.android.R

/** AndroidX implementation of [WPSwitchPreference]
* @see WPSwitchPreference*/
class WPSwitchPreferenceX(context: Context, attrs: AttributeSet?) :
SwitchPreference(context, attrs), PreferenceHint {
private var mHint: String? = null
private var mTint: ColorStateList? = null
private var mThumbTint: ColorStateList? = null
private var mStartOffset = 0

init {
val array = context.obtainStyledAttributes(attrs, R.styleable.SummaryEditTextPreference)
for (i in 0 until array.indexCount) {
val index = array.getIndex(i)
if (index == R.styleable.SummaryEditTextPreference_longClickHint) {
mHint = array.getString(index)
} else if (index == R.styleable.SummaryEditTextPreference_iconTint) {
val resourceId = array.getResourceId(index, 0)
if (resourceId != 0) {
mTint = AppCompatResources.getColorStateList(context, resourceId)
}
} else if (index == R.styleable.SummaryEditTextPreference_switchThumbTint) {
mThumbTint = array.getColorStateList(index)
} else if (index == R.styleable.SummaryEditTextPreference_startOffset) {
mStartOffset = array.getDimensionPixelSize(index, 0)
}
}
array.recycle()
}

@SuppressLint("UseSwitchCompatOrMaterialCode")
override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder)
val view = holder.itemView
val icon = view.findViewById<ImageView>(android.R.id.icon)
if (icon != null && mTint != null) {
icon.imageTintList = mTint
}
val titleView = view.findViewById<TextView>(android.R.id.title)
if (titleView != null) {
val res = context.resources

// add padding to the start of nested preferences
if (!TextUtils.isEmpty(dependency)) {
val margin = res.getDimensionPixelSize(R.dimen.margin_large)
ViewCompat.setPaddingRelative(titleView, margin + mStartOffset, 0, 0, 0)
} else {
ViewCompat.setPaddingRelative(titleView, mStartOffset, 0, 0, 0)
}
}

// style custom switch preference
val switchControl = getSwitch(view as ViewGroup)
if (switchControl != null) {
if (mThumbTint != null) {
switchControl.thumbTintList = mThumbTint
}
}

// Add padding to start of switch.
ViewCompat.setPaddingRelative(
getSwitch(view)!!,
context.resources.getDimensionPixelSize(R.dimen.margin_extra_large), 0, 0, 0
)
}
@SuppressLint("UseSwitchCompatOrMaterialCode")
private fun getSwitch(parentView: ViewGroup): Switch? {
for (i in 0 until parentView.childCount) {
val childView = parentView.getChildAt(i)
if (childView is Switch) {
return childView
} else if (childView is ViewGroup) {
val theSwitch = getSwitch(childView)
if (theSwitch != null) {
return theSwitch
}
}
}
return null
}

override fun hasHint(): Boolean {
return !TextUtils.isEmpty(mHint)
}

override fun getHint(): String {
return mHint!!
}

override fun setHint(hint: String) {
mHint = hint
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.wordpress.android.ui.prefs.notifications

import android.os.Bundle
import android.view.Gravity
import android.view.View
import androidx.preference.PreferenceFragmentCompat
import androidx.transition.Slide
import androidx.transition.Transition
import androidx.transition.TransitionManager
import com.google.android.material.appbar.AppBarLayout
import org.wordpress.android.R
import org.wordpress.android.util.AniUtils

/** Child Notification fragments should inherit from this class in order to make navigation consistent.*/
abstract class ChildNotificationSettingsFragment: PreferenceFragmentCompat() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setMainSwitchVisibility(View.GONE)
}

override fun onDestroy() {
super.onDestroy()
setMainSwitchVisibility(View.VISIBLE)
}

private fun setMainSwitchVisibility(visibility: Int) {
with(requireActivity()) {
val mainSwitchToolBarView = findViewById<PrefMainSwitchToolbarView>(R.id.main_switch)
val rootView = findViewById<AppBarLayout>(R.id.app_bar_layout)
val transition: Transition = Slide(Gravity.TOP)
transition.duration = AniUtils.Duration.SHORT.toMillis(context)
transition.addTarget(R.id.main_switch)

TransitionManager.beginDelayedTransition(rootView, transition)
mainSwitchToolBarView.visibility = visibility
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
Expand All @@ -15,6 +14,8 @@

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.SwitchCompat;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;

Expand Down Expand Up @@ -52,7 +53,7 @@ public class NotificationSettingsFollowedDialog extends DialogFragment implement

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
LayoutInflater inflater = requireActivity().getLayoutInflater();
@SuppressLint("InflateParams")
View layout = inflater.inflate(R.layout.followed_sites_dialog, null);

Expand Down Expand Up @@ -88,7 +89,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
}
}

AlertDialog.Builder builder = new MaterialAlertDialogBuilder(getActivity());
AlertDialog.Builder builder = new MaterialAlertDialogBuilder(requireActivity());
builder.setTitle(getString(R.string.notification_settings_followed_dialog_title));
builder.setPositiveButton(android.R.string.ok, this);
builder.setNegativeButton(R.string.cancel, this);
Expand Down Expand Up @@ -122,10 +123,7 @@ public void onClick(DialogInterface dialog, int which) {

@Override
public void onDismiss(DialogInterface dialog) {
// TODO: android.app.Fragment is deprecated since Android P.
// Needs to be replaced with android.support.v4.app.Fragment
// See https://developer.android.com/reference/android/app/Fragment
android.app.Fragment target = getTargetFragment();
Fragment target = getTargetFragment();

if (target != null) {
target.onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, getResultIntent());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.wordpress.android.ui.prefs.notifications

import android.content.Intent
import org.wordpress.android.analytics.AnalyticsTracker
import org.wordpress.android.datasets.ReaderBlogTable
import org.wordpress.android.fluxc.Dispatcher
import org.wordpress.android.fluxc.generated.AccountActionBuilder
import org.wordpress.android.fluxc.store.AccountStore.AddOrDeleteSubscriptionPayload
import org.wordpress.android.fluxc.store.AccountStore.AddOrDeleteSubscriptionPayload.SubscriptionAction
import org.wordpress.android.fluxc.store.AccountStore.UpdateSubscriptionPayload

/** Any notification preference fragment that deals with **My Sites** or **My Followed Sites**
* should implement this interface.*/
interface NotificationsMySitesSettingsFragment {
var mNotificationUpdatedSite: String?
var mPreviousEmailComments: Boolean
var mPreviousEmailPosts: Boolean
var mPreviousNotifyPosts: Boolean
var mUpdateEmailPostsFirst: Boolean
var mPreviousEmailPostsFrequency: String?
var mUpdateSubscriptionFrequencyPayload: UpdateSubscriptionPayload?
var mDispatcher: Dispatcher

fun onMySiteSettingsChanged(data: Intent?) {
if (data == null) return
val notifyPosts = data.getBooleanExtra(NotificationSettingsFollowedDialog.KEY_NOTIFICATION_POSTS, false)
val emailPosts = data.getBooleanExtra(NotificationSettingsFollowedDialog.KEY_EMAIL_POSTS, false)
val emailPostsFrequency = data.getStringExtra(NotificationSettingsFollowedDialog.KEY_EMAIL_POSTS_FREQUENCY)
val emailComments = data.getBooleanExtra(NotificationSettingsFollowedDialog.KEY_EMAIL_COMMENTS, false)
if (notifyPosts != mPreviousNotifyPosts) {
ReaderBlogTable.setNotificationsEnabledByBlogId(mNotificationUpdatedSite!!.toLong(), notifyPosts)
val payload: AddOrDeleteSubscriptionPayload = if (notifyPosts) {
AnalyticsTracker.track(AnalyticsTracker.Stat.FOLLOWED_BLOG_NOTIFICATIONS_SETTINGS_ON)
AddOrDeleteSubscriptionPayload(mNotificationUpdatedSite!!, SubscriptionAction.NEW)
} else {
AnalyticsTracker.track(AnalyticsTracker.Stat.FOLLOWED_BLOG_NOTIFICATIONS_SETTINGS_OFF)
AddOrDeleteSubscriptionPayload(mNotificationUpdatedSite!!, SubscriptionAction.DELETE)
}
mDispatcher.dispatch(AccountActionBuilder.newUpdateSubscriptionNotificationPostAction(payload))
}
if (emailPosts != mPreviousEmailPosts) {
val payload: AddOrDeleteSubscriptionPayload = if (emailPosts) {
AnalyticsTracker.track(AnalyticsTracker.Stat.FOLLOWED_BLOG_NOTIFICATIONS_SETTINGS_EMAIL_ON)
AddOrDeleteSubscriptionPayload(mNotificationUpdatedSite!!, SubscriptionAction.NEW)
} else {
AnalyticsTracker.track(AnalyticsTracker.Stat.FOLLOWED_BLOG_NOTIFICATIONS_SETTINGS_EMAIL_OFF)
AddOrDeleteSubscriptionPayload(mNotificationUpdatedSite!!, SubscriptionAction.DELETE)
}
mDispatcher.dispatch(AccountActionBuilder.newUpdateSubscriptionEmailPostAction(payload))
}
if (emailPostsFrequency != null && !emailPostsFrequency.equals(mPreviousEmailPostsFrequency, ignoreCase = true)
) {
val subscriptionFrequency = getSubscriptionFrequencyFromString(emailPostsFrequency)
mUpdateSubscriptionFrequencyPayload = UpdateSubscriptionPayload(mNotificationUpdatedSite!!,
subscriptionFrequency)
/*
* The email post frequency update will be overridden by the email post update if the email post
* frequency callback returns first. Thus, the updates must be dispatched sequentially when the
* email post update is switched from disabled to enabled.
*/
if (emailPosts != mPreviousEmailPosts && emailPosts) {
mUpdateEmailPostsFirst = true
} else {
mDispatcher.dispatch(
AccountActionBuilder.newUpdateSubscriptionEmailPostFrequencyAction(
mUpdateSubscriptionFrequencyPayload)
)
}
}
if (emailComments != mPreviousEmailComments) {
val payload: AddOrDeleteSubscriptionPayload = if (emailComments) {
AnalyticsTracker.track(AnalyticsTracker.Stat.FOLLOWED_BLOG_NOTIFICATIONS_SETTINGS_COMMENTS_ON)
AddOrDeleteSubscriptionPayload(mNotificationUpdatedSite!!, SubscriptionAction.NEW)
} else {
AnalyticsTracker.track(AnalyticsTracker.Stat.FOLLOWED_BLOG_NOTIFICATIONS_SETTINGS_COMMENTS_OFF)
AddOrDeleteSubscriptionPayload(mNotificationUpdatedSite!!, SubscriptionAction.DELETE)
}
mDispatcher.dispatch(AccountActionBuilder.newUpdateSubscriptionEmailCommentAction(payload))
}
}

fun getSubscriptionFrequencyFromString(s: String): UpdateSubscriptionPayload.SubscriptionFrequency {
return if (s.equals(UpdateSubscriptionPayload.SubscriptionFrequency.DAILY.toString(), ignoreCase = true)) {
AnalyticsTracker.track(AnalyticsTracker.Stat.FOLLOWED_BLOG_NOTIFICATIONS_SETTINGS_EMAIL_DAILY)
UpdateSubscriptionPayload.SubscriptionFrequency.DAILY
} else if (s.equals(UpdateSubscriptionPayload.SubscriptionFrequency.WEEKLY.toString(), ignoreCase = true)) {
AnalyticsTracker.track(AnalyticsTracker.Stat.FOLLOWED_BLOG_NOTIFICATIONS_SETTINGS_EMAIL_WEEKLY)
UpdateSubscriptionPayload.SubscriptionFrequency.WEEKLY
} else {
AnalyticsTracker.track(AnalyticsTracker.Stat.FOLLOWED_BLOG_NOTIFICATIONS_SETTINGS_EMAIL_INSTANTLY)
UpdateSubscriptionPayload.SubscriptionFrequency.INSTANTLY
}
}
}
Loading
Loading