Skip to content

Commit

Permalink
Merge pull request #17496 from wordpress-mobile/issue/15816-manage-lo…
Browse files Browse the repository at this point in the history
…cal-notifs-with-switch

Disable local notifications with Notification Settings switch
  • Loading branch information
irfano authored Nov 22, 2022
2 parents 5434beb + 7df6d32 commit cb9c999
Show file tree
Hide file tree
Showing 13 changed files with 322 additions and 193 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

21.3
-----
* [*] Disable local notifications with Notification Settings switch. [https://github.com/wordpress-mobile/WordPress-Android/pull/17496]
* [*] [Jetpack-only] Comments: fix a crash in My Site > Comments with Jetpack standalone plugins. [https://github.com/wordpress-mobile/WordPress-Android/pull/17456]
* [*] [internal] Fix an issue preventing the text and button on the 'WordPress is Better with Jetpack' overlay to be entirely visible with big fonts. [https://github.com/wordpress-mobile/WordPress-Android/pull/17503]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import androidx.lifecycle.LiveData;
import androidx.preference.PreferenceManager;

import com.tenor.android.core.network.ApiClient;
import com.tenor.android.core.network.ApiService;
Expand Down Expand Up @@ -109,6 +111,11 @@ public abstract class ApplicationModule {
@ContributesAndroidInjector
abstract BasicDialog contributeBasicDialog();

@Provides
public static SharedPreferences provideSharedPrefs(@ApplicationContext Context context) {
return PreferenceManager.getDefaultSharedPreferences(context);
}

@Provides
public static WizardManager<SiteCreationStep> provideWizardManager(
SiteCreationStepsProvider stepsProvider) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ private boolean isGooglePlayServicesAvailable(Activity activity) {

private void scheduleLocalNotifications() {
mCreateSiteNotificationScheduler.scheduleCreateSiteNotificationIfNeeded();
mWeeklyRoundupScheduler.schedule();
mWeeklyRoundupScheduler.scheduleIfNeeded();
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package org.wordpress.android.ui.prefs.notifications

import android.os.Bundle
import android.text.TextUtils
import android.view.MenuItem
import android.view.View
import android.widget.CompoundButton
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.preference.PreferenceManager
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode.MAIN
import org.wordpress.android.R.id
import org.wordpress.android.R.layout
import org.wordpress.android.R.string
import org.wordpress.android.analytics.AnalyticsTracker
import org.wordpress.android.analytics.AnalyticsTracker.Stat.NOTIFICATION_SETTINGS_APP_NOTIFICATIONS_DISABLED
import org.wordpress.android.analytics.AnalyticsTracker.Stat.NOTIFICATION_SETTINGS_APP_NOTIFICATIONS_ENABLED
import org.wordpress.android.modules.APPLICATION_SCOPE
import org.wordpress.android.ui.LocaleAwareActivity
import org.wordpress.android.ui.notifications.NotificationEvents.NotificationsSettingsStatusChanged
import org.wordpress.android.ui.prefs.notifications.PrefMainSwitchToolbarView.MainSwitchToolbarListener
import org.wordpress.android.ui.prefs.notifications.usecase.UpdateNotificationSettingsUseCase
import javax.inject.Inject
import javax.inject.Named

@AndroidEntryPoint
class NotificationsSettingsActivity : LocaleAwareActivity(), MainSwitchToolbarListener {
@Inject lateinit var updateNotificationSettingsUseCase: UpdateNotificationSettingsUseCase
@Inject @Named(APPLICATION_SCOPE) lateinit var applicationScope: CoroutineScope

private lateinit var messageTextView: TextView
private lateinit var messageContainer: View

private lateinit var fragmentContainer: View

public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(layout.notifications_settings_activity)
fragmentContainer = findViewById(id.fragment_container)

setUpPrimaryToolbar()

setUpMainSwitch()

if (savedInstanceState == null) {
@Suppress("DEPRECATION")
fragmentManager.beginTransaction()
.add(id.fragment_container, NotificationsSettingsFragment())
.commit()
}

messageContainer = findViewById(id.notifications_settings_message_container)
messageTextView = findViewById(id.notifications_settings_message)
}

override fun onStop() {
EventBus.getDefault().unregister(this)
super.onStop()
}

override fun onStart() {
super.onStart()
EventBus.getDefault().register(this)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> {
onBackPressed()
return true
}
}
return super.onOptionsItemSelected(item)
}

@Subscribe(threadMode = MAIN)
fun onEventMainThread(event: NotificationsSettingsStatusChanged) {
if (TextUtils.isEmpty(event.message)) {
messageContainer.visibility = View.GONE
} else {
messageContainer.visibility = View.VISIBLE
messageTextView.text = event.message
}
}

/**
* Set up primary toolbar for navigation and search
*/
private fun setUpPrimaryToolbar() {
val toolbar = findViewById<Toolbar>(id.toolbar_with_search)

toolbar?.let { setSupportActionBar(it) }

supportActionBar?.apply {
setTitle(string.notification_settings)
setDisplayHomeAsUpEnabled(true)
setDisplayShowTitleEnabled(true)
}
}

/**
* Sets up main switch to disable/enable all notification settings
*/
private fun setUpMainSwitch() {
val mainSwitchToolBarView = findViewById<PrefMainSwitchToolbarView>(id.main_switch)
mainSwitchToolBarView.setMainSwitchToolbarListener(this)

// Set main switch state from shared preferences.
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this@NotificationsSettingsActivity)
val isMainChecked = sharedPreferences.getBoolean(getString(string.wp_pref_notifications_main), true)
mainSwitchToolBarView.loadInitialState(isMainChecked)
hideDisabledView(isMainChecked)
}

override fun onMainSwitchCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
applicationScope.launch { updateNotificationSettingsUseCase.updateNotificationSettings(isChecked) }

hideDisabledView(isChecked)

if (isChecked) {
AnalyticsTracker.track(NOTIFICATION_SETTINGS_APP_NOTIFICATIONS_ENABLED)
} else {
AnalyticsTracker.track(NOTIFICATION_SETTINGS_APP_NOTIFICATIONS_DISABLED)
}
}

/**
* Hide view when Notification Settings are disabled by toggling the main switch off.
*
* @param isMainChecked TRUE to hide disabled view, FALSE to show disabled view
*/
private fun hideDisabledView(isMainChecked: Boolean) {
val notificationsDisabledView = findViewById<LinearLayout>(id.notification_settings_disabled_view)
notificationsDisabledView.visibility = if (isMainChecked) View.INVISIBLE else View.VISIBLE
fragmentContainer.visibility = if (isMainChecked) View.VISIBLE else View.GONE
}
}
Loading

0 comments on commit cb9c999

Please sign in to comment.