diff options
author | Stefan Niedermann <info@niedermann.it> | 2023-03-24 13:02:55 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2023-03-24 13:03:02 +0300 |
commit | 79a1a9aa5cc9faf7ef0ada5086aa36dad2f729a5 (patch) | |
tree | ac577b4e0f103a45e2af31b5eca754bbb06d4dd4 /app/src | |
parent | 35ca8886441d281385d9f742e45220e549455e95 (diff) |
fix: Fixed ViewModel scoping which caused using wrong accounts to synchronize leading to HTTP 403
Signed-off-by: Stefan Niedermann <info@niedermann.it>
Diffstat (limited to 'app/src')
7 files changed, 35 insertions, 12 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/archivedboards/ArchivedBoardsActivity.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/archivedboards/ArchivedBoardsActivity.java index b1253aa46..0a794aa91 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/archivedboards/ArchivedBoardsActivity.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/archivedboards/ArchivedBoardsActivity.java @@ -9,7 +9,6 @@ import androidx.annotation.AnyThread; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; -import androidx.lifecycle.ViewModelProvider; import it.niedermann.android.reactivelivedata.ReactiveLiveData; import it.niedermann.nextcloud.deck.DeckLog; @@ -53,7 +52,7 @@ public class ArchivedBoardsActivity extends AppCompatActivity implements Themed, setContentView(binding.getRoot()); setSupportActionBar(binding.toolbar); - archivedBoardsViewModel = new ViewModelProvider(this, new SyncViewModel.Factory(getApplication(), account)).get(ArchivedBoardsViewModel.class); + archivedBoardsViewModel = new SyncViewModel.Provider(this, getApplication(), account).get(ArchivedBoardsViewModel.class); adapter = new ArchivedBoardsAdapter(account, getSupportFragmentManager(), this::onArchive); binding.recyclerView.setAdapter(adapter); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/board/accesscontrol/AccessControlDialogFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/board/accesscontrol/AccessControlDialogFragment.java index c1b698d4c..aeb2e62c4 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/board/accesscontrol/AccessControlDialogFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/board/accesscontrol/AccessControlDialogFragment.java @@ -13,7 +13,6 @@ import androidx.annotation.ColorInt; import androidx.annotation.NonNull; import androidx.core.content.ContextCompat; import androidx.fragment.app.DialogFragment; -import androidx.lifecycle.ViewModelProvider; import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.snackbar.Snackbar; @@ -76,7 +75,7 @@ public class AccessControlDialogFragment extends DialogFragment implements Acces public Dialog onCreateDialog(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - accessControlViewModel = new ViewModelProvider(requireActivity(), new SyncViewModel.Factory(requireActivity().getApplication(), account)).get(AccessControlViewModel.class); + accessControlViewModel = new SyncViewModel.Provider(requireActivity(), requireActivity().getApplication(), account).get(AccessControlViewModel.class); final var dialogBuilder = new MaterialAlertDialogBuilder(requireContext()); binding = DialogBoardShareBinding.inflate(requireActivity().getLayoutInflater()); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/board/managelabels/ManageLabelsDialogFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/board/managelabels/ManageLabelsDialogFragment.java index 6b79d7ad2..6accebc80 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/board/managelabels/ManageLabelsDialogFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/board/managelabels/ManageLabelsDialogFragment.java @@ -11,7 +11,6 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.UiThread; import androidx.fragment.app.DialogFragment; -import androidx.lifecycle.ViewModelProvider; import com.google.android.material.dialog.MaterialAlertDialogBuilder; @@ -69,7 +68,7 @@ public class ManageLabelsDialogFragment extends ThemedDialogFragment implements public Dialog onCreateDialog(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - labelsViewModel = new ViewModelProvider(requireActivity(), new SyncViewModel.Factory(this.requireActivity().getApplication(), account)).get(LabelsViewModel.class); + labelsViewModel = new SyncViewModel.Provider(requireActivity(), requireActivity().getApplication(), account).get(LabelsViewModel.class); final var dialogBuilder = new MaterialAlertDialogBuilder(requireContext()); binding = DialogBoardManageLabelsBinding.inflate(requireActivity().getLayoutInflater()); colors = getResources().getStringArray(R.array.board_default_colors); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/NewCardDialog.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/NewCardDialog.java index c98ee4606..efe974598 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/NewCardDialog.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/NewCardDialog.java @@ -22,6 +22,7 @@ import androidx.lifecycle.ViewModelProvider; import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.nextcloud.android.common.ui.theme.utils.ColorRole; +import it.niedermann.nextcloud.deck.DeckLog; import it.niedermann.nextcloud.deck.R; import it.niedermann.nextcloud.deck.databinding.DialogNewCardBinding; import it.niedermann.nextcloud.deck.exceptions.OfflineException; @@ -65,7 +66,8 @@ public class NewCardDialog extends ThemedDialogFragment implements DialogInterfa } this.account = (Account) getArguments().getSerializable(KEY_ACCOUNT); - newCardViewModel = new ViewModelProvider(requireActivity(), new SyncViewModel.Factory(requireActivity().getApplication(), account)).get(NewCardViewModel.class); + newCardViewModel = new SyncViewModel.Provider(requireActivity(), requireActivity().getApplication(), account).get(NewCardViewModel.class); + DeckLog.log(newCardViewModel); viewModel = new ViewModelProvider(requireActivity()).get(PrepareCreateViewModel.class); } diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/comments/CardCommentsFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/comments/CardCommentsFragment.java index 254765a0a..c110d69b7 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/comments/CardCommentsFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/comments/CardCommentsFragment.java @@ -16,7 +16,6 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; -import androidx.lifecycle.ViewModelProvider; import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; @@ -63,8 +62,7 @@ public class CardCommentsFragment extends Fragment implements CommentEditedListe throw new IllegalArgumentException(KEY_ACCOUNT + " must not be null."); } - editCardViewModel = new ViewModelProvider(requireActivity()).get(EditCardViewModel.class); - commentsViewModel = new ViewModelProvider(this, new SyncViewModel.Factory(requireActivity().getApplication(), account)).get(CommentsViewModel.class); + commentsViewModel = new SyncViewModel.Provider(this, requireActivity().getApplication(), account).get(CommentsViewModel.class); } @Override diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/StackFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/StackFragment.java index 084c642c7..9ccf8818c 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/StackFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/StackFragment.java @@ -90,7 +90,7 @@ public class StackFragment extends Fragment implements Themed, DragAndDropTab<Ca public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { activity = requireActivity(); binding = FragmentStackBinding.inflate(inflater, container, false); - stackViewModel = new ViewModelProvider(requireActivity(), new SyncViewModel.Factory(this.requireActivity().getApplication(), account)).get(StackViewModel.class); + stackViewModel = new SyncViewModel.Provider(requireActivity(), requireActivity().getApplication(), account).get(StackViewModel.class); applyTheme(account.getColor()); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/viewmodel/SyncViewModel.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/viewmodel/SyncViewModel.java index 0b9bff3e1..4cfd498d2 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/viewmodel/SyncViewModel.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/viewmodel/SyncViewModel.java @@ -5,6 +5,7 @@ import android.app.Application; import androidx.annotation.NonNull; import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModelProvider; +import androidx.lifecycle.ViewModelStoreOwner; import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException; @@ -19,6 +20,11 @@ import it.niedermann.nextcloud.deck.ui.stack.StackViewModel; /** * To be used for {@link ViewModel}s which need an {@link SyncRepository} instance + * <p> + * <code> + * new SyncViewModel.Provider(requireActivity(), requireActivity().getApplication(), account).get(CustomViewModel.class) + * </code> + * </p> */ public abstract class SyncViewModel extends BaseViewModel { @@ -38,7 +44,27 @@ public abstract class SyncViewModel extends BaseViewModel { this.syncRepository = syncRepository; } - public static class Factory implements ViewModelProvider.Factory { + public static class Provider extends ViewModelProvider { + + @NonNull + private final Account account; + + public Provider(@NonNull ViewModelStoreOwner owner, @NonNull Application application, @NonNull Account account) { + super(owner, new SyncViewModel.Factory(application, account)); + this.account = account; + } + + /** + * Calls {@link #get(String, Class)} with a custom key to retrieve a {@link ViewModel} instance scoped by the {@param modelClass} for the {@link Account} provided in the constructor. + */ + @NonNull + @Override + public <T extends ViewModel> T get(@NonNull Class<T> modelClass) { + return get(modelClass.getCanonicalName() + "@" + account.getName(), modelClass); + } + } + + private static class Factory implements ViewModelProvider.Factory { private final Application application; private final Account account; |