Enable notifications by account and disable feed notification permission switches if account notifications are disabled
This commit is contained in:
parent
9d1d9a31fe
commit
65aca8eb5d
@ -1,17 +1,20 @@
|
||||
package com.readrops.app.activities
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.drm.DrmInfoRequest.ACCOUNT_ID
|
||||
import android.os.Bundle
|
||||
import android.view.MenuItem
|
||||
import androidx.activity.viewModels
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.readrops.app.R
|
||||
import com.readrops.app.adapters.NotificationPermissionAdapter
|
||||
import com.readrops.app.databinding.ActivityNotificationPermissionBinding
|
||||
import com.readrops.app.utils.ReadropsKeys.ACCOUNT
|
||||
import com.readrops.app.utils.Utils
|
||||
import com.readrops.app.viewmodels.NotificationPermissionViewModel
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
|
||||
class NotificationPermissionActivity : AppCompatActivity() {
|
||||
|
||||
@ -24,19 +27,47 @@ class NotificationPermissionActivity : AppCompatActivity() {
|
||||
setTitle(R.string.notifications)
|
||||
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||
|
||||
val accountId = intent.getIntExtra(ACCOUNT_ID, 0)
|
||||
val viewModel by viewModels<NotificationPermissionViewModel>()
|
||||
viewModel.account = intent.getParcelableExtra(ACCOUNT)
|
||||
var adapter: NotificationPermissionAdapter? = null
|
||||
|
||||
val adapter = NotificationPermissionAdapter {
|
||||
viewModel.getAccount(accountId).observe(this, Observer { account ->
|
||||
viewModel.account = account
|
||||
|
||||
}
|
||||
|
||||
binding.notifPermissionAccountList.layoutManager = LinearLayoutManager(this)
|
||||
binding.notifPermissionAccountList.adapter = adapter
|
||||
if (adapter == null) {
|
||||
// execute the following lines only once
|
||||
adapter = NotificationPermissionAdapter(account.isNotificationsEnabled) { feed ->
|
||||
viewModel.setFeedNotificationState(feed)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.doOnError { Utils.showSnackbar(binding.root, it.message) }
|
||||
.subscribe()
|
||||
}
|
||||
|
||||
binding.notifPermissionAccountSwitch.isChecked = account.isNotificationsEnabled
|
||||
binding.notifPermissionAccountSwitch.setOnCheckedChangeListener { _, isChecked ->
|
||||
account.isNotificationsEnabled = isChecked
|
||||
adapter?.enableAll = isChecked
|
||||
adapter?.notifyDataSetChanged()
|
||||
|
||||
viewModel.setAccountNotificationsState(isChecked)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.doOnError { Utils.showSnackbar(binding.root, it.message) }
|
||||
.subscribe()
|
||||
}
|
||||
|
||||
binding.notifPermissionAccountList.layoutManager = LinearLayoutManager(this)
|
||||
binding.notifPermissionAccountList.adapter = adapter
|
||||
|
||||
viewModel.getFeedsWithNotifPermission().observe(this, Observer {
|
||||
adapter?.submitList(it)
|
||||
})
|
||||
}
|
||||
|
||||
viewModel.getFeedsWithNotifPermission().observe(this, Observer {
|
||||
adapter.submitList(it)
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
|
@ -12,10 +12,11 @@ import com.readrops.app.databinding.NotificationLayoutBinding
|
||||
import com.readrops.app.utils.GlideApp
|
||||
import com.readrops.readropsdb.entities.Feed
|
||||
|
||||
class NotificationPermissionAdapter(val listener: (feed: Feed) -> Unit) : ListAdapter<Feed, NotificationPermissionAdapter.NotificationPermissionViewHolder>(DIFF_CALLBACK) {
|
||||
class NotificationPermissionAdapter(var enableAll: Boolean, val listener: (feed: Feed) -> Unit) : ListAdapter<Feed, NotificationPermissionAdapter.NotificationPermissionViewHolder>(DIFF_CALLBACK) {
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NotificationPermissionViewHolder {
|
||||
val binding = DataBindingUtil.inflate<NotificationLayoutBinding>(LayoutInflater.from(parent.context), R.layout.notification_layout, parent, false)
|
||||
val binding = DataBindingUtil.inflate<NotificationLayoutBinding>(LayoutInflater.from(parent.context),
|
||||
R.layout.notification_layout, parent, false)
|
||||
|
||||
return NotificationPermissionViewHolder(binding)
|
||||
}
|
||||
@ -26,6 +27,10 @@ class NotificationPermissionAdapter(val listener: (feed: Feed) -> Unit) : ListAd
|
||||
holder.binding.notificationFeedName.text = feed.name
|
||||
holder.binding.notificationSwitch.isChecked = feed.isNotificationEnabled
|
||||
|
||||
holder.binding.notificationSwitch.isEnabled = enableAll
|
||||
holder.binding.notificationSwitch.setOnCheckedChangeListener { _, _ -> listener(feed) }
|
||||
holder.itemView.setOnClickListener { listener(feed) }
|
||||
|
||||
GlideApp.with(holder.itemView.context)
|
||||
.load(feed.iconUrl)
|
||||
.diskCacheStrategy(DiskCacheStrategy.ALL)
|
||||
|
@ -47,6 +47,7 @@ import io.reactivex.observers.DisposableCompletableObserver;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
import static android.app.Activity.RESULT_OK;
|
||||
import static android.drm.DrmInfoRequest.ACCOUNT_ID;
|
||||
import static com.readrops.app.utils.ReadropsKeys.ACCOUNT;
|
||||
import static com.readrops.app.utils.ReadropsKeys.EDIT_ACCOUNT;
|
||||
|
||||
@ -137,7 +138,7 @@ public class AccountSettingsFragment extends PreferenceFragmentCompat {
|
||||
|
||||
notificationPref.setOnPreferenceClickListener(preference -> {
|
||||
Intent intent = new Intent(getContext(), NotificationPermissionActivity.class);
|
||||
intent.putExtra(ACCOUNT, account);
|
||||
intent.putExtra(ACCOUNT_ID, account.getId());
|
||||
|
||||
startActivity(intent);
|
||||
return true;
|
||||
|
@ -3,6 +3,7 @@ package com.readrops.app.utils
|
||||
object ReadropsKeys {
|
||||
|
||||
const val ACCOUNT = "ACCOUNT_KEY"
|
||||
const val ACOUNT_ID = "ACCOUNT_ID"
|
||||
const val ACCOUNT_TYPE = "ACCOUNT_TYPE_KEY"
|
||||
const val EDIT_ACCOUNT = "EDIT_ACCOUNT"
|
||||
|
||||
|
@ -6,13 +6,21 @@ import androidx.lifecycle.LiveData
|
||||
import com.readrops.readropsdb.Database
|
||||
import com.readrops.readropsdb.entities.Feed
|
||||
import com.readrops.readropsdb.entities.account.Account
|
||||
import io.reactivex.Completable
|
||||
|
||||
class NotificationPermissionViewModel(application: Application) : AndroidViewModel(application) {
|
||||
|
||||
val database = Database.getInstance(application)
|
||||
var account: Account? = null
|
||||
|
||||
fun getAccount(accountId: Int) = database.accountDao().selectAsync(accountId)
|
||||
|
||||
fun getFeedsWithNotifPermission(): LiveData<List<Feed>> = database.feedDao()
|
||||
.getFeedsForNotifPermission(account?.id!!)
|
||||
|
||||
fun setAccountNotificationsState(enabled: Boolean): Completable = database.accountDao()
|
||||
.updateNotificationState(account?.id!!, enabled)
|
||||
|
||||
fun setFeedNotificationState(feed: Feed): Completable = database.feedDao()
|
||||
.updateNotificationState(feed.id, !feed.isNotificationEnabled)
|
||||
}
|
@ -8,6 +8,7 @@ import com.readrops.readropsdb.entities.account.Account;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import io.reactivex.Completable;
|
||||
import io.reactivex.Single;
|
||||
|
||||
@Dao
|
||||
@ -16,6 +17,9 @@ public interface AccountDao extends BaseDao<Account> {
|
||||
@Query("Select * from Account")
|
||||
LiveData<List<Account>> selectAllAsync();
|
||||
|
||||
@Query("Select * From Account Where id = :accountId")
|
||||
LiveData<Account> selectAsync(int accountId);
|
||||
|
||||
@Query("Select * from Account")
|
||||
List<Account> selectAll();
|
||||
|
||||
@ -28,12 +32,12 @@ public interface AccountDao extends BaseDao<Account> {
|
||||
@Query("Update Account set current_account = 1 Where id = :accountId")
|
||||
void setCurrentAccount(int accountId);
|
||||
|
||||
@Query("Select count(*) From Account Where account_type = :accountType")
|
||||
Integer getAccountCountByType(int accountType);
|
||||
|
||||
@Query("Select count(*) From Account")
|
||||
Single<Integer> getAccountCount();
|
||||
|
||||
@Query("Update Account set writeToken = :writeToken Where id = :accountId")
|
||||
void updateWriteToken(int accountId, String writeToken);
|
||||
|
||||
@Query("Update Account set notifications_enabled = :enabled Where id = :accountId")
|
||||
Completable updateNotificationState(int accountId, boolean enabled);
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ import com.readrops.readropsdb.pojo.FeedWithFolder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import io.reactivex.Completable;
|
||||
import io.reactivex.Single;
|
||||
|
||||
@Dao
|
||||
@ -83,6 +84,9 @@ public abstract class FeedDao implements BaseDao<Feed> {
|
||||
@Query("Select id From Folder Where remoteId = :remoteId And account_id = :accountId")
|
||||
abstract int getRemoteFolderLocalId(String remoteId, int accountId);
|
||||
|
||||
@Query("Update Feed set notification_enabled = :enabled Where id = :feedId")
|
||||
public abstract Completable updateNotificationState(int feedId, boolean enabled);
|
||||
|
||||
/**
|
||||
* Insert, update and delete feeds, by account
|
||||
*
|
||||
|
@ -56,7 +56,7 @@ public class Feed implements Parcelable {
|
||||
@ColumnInfo(name = "account_id", index = true)
|
||||
private int accountId;
|
||||
|
||||
@ColumnInfo(name = "notification_enabled")
|
||||
@ColumnInfo(name = "notification_enabled", defaultValue = "1")
|
||||
private boolean notificationEnabled;
|
||||
|
||||
@Ignore
|
||||
@ -93,6 +93,7 @@ public class Feed implements Parcelable {
|
||||
folderId = parcelFolderId == 0 ? null : parcelFolderId;
|
||||
|
||||
remoteId = in.readString();
|
||||
notificationEnabled = in.readByte() != 0;
|
||||
}
|
||||
|
||||
public static final Creator<Feed> CREATOR = new Creator<Feed>() {
|
||||
@ -265,5 +266,6 @@ public class Feed implements Parcelable {
|
||||
dest.writeString(lastModified);
|
||||
dest.writeInt(folderId == null ? 0 : folderId);
|
||||
dest.writeString(remoteId);
|
||||
dest.writeByte((byte) (notificationEnabled ? 1 : 0));
|
||||
}
|
||||
}
|
||||
|
@ -35,6 +35,9 @@ public class Account implements Parcelable {
|
||||
private String token;
|
||||
private String writeToken; // TODO : see if there is a better solution to store specific service account fields
|
||||
|
||||
@ColumnInfo(name = "notifications_enabled")
|
||||
private boolean notificationsEnabled;
|
||||
|
||||
@Ignore
|
||||
private String login;
|
||||
|
||||
@ -63,6 +66,7 @@ public class Account implements Parcelable {
|
||||
password = in.readString();
|
||||
token = in.readString();
|
||||
writeToken = in.readString();
|
||||
notificationsEnabled = in.readByte() != 0;
|
||||
}
|
||||
|
||||
public static final Creator<Account> CREATOR = new Creator<Account>() {
|
||||
@ -173,6 +177,14 @@ public class Account implements Parcelable {
|
||||
this.writeToken = writeToken;
|
||||
}
|
||||
|
||||
public boolean isNotificationsEnabled() {
|
||||
return notificationsEnabled;
|
||||
}
|
||||
|
||||
public void setNotificationsEnabled(boolean notificationsEnabled) {
|
||||
this.notificationsEnabled = notificationsEnabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
@ -191,6 +203,7 @@ public class Account implements Parcelable {
|
||||
dest.writeString(password);
|
||||
dest.writeString(token);
|
||||
dest.writeString(writeToken);
|
||||
dest.writeByte((byte) (notificationsEnabled ? 1 : 0));
|
||||
}
|
||||
|
||||
public boolean isLocal() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user