diff options
author | Stefan Niedermann <info@niedermann.it> | 2020-06-13 16:38:53 +0300 |
---|---|---|
committer | Niedermann IT-Dienstleistungen <stefan-niedermann@users.noreply.github.com> | 2020-06-15 21:32:47 +0300 |
commit | 0d79609c849b47da5d7bd08f94f80a3645a9a188 (patch) | |
tree | 4554b81df0952b2d68e8e07cc32831e85327a0b0 /app/src/main/java/it/niedermann/nextcloud/deck/ui/accountswitcher | |
parent | 6e5e60ac18fd84dca875edf588e383fb8474c828 (diff) |
Implement AccountSwitcherDialog and ManageAccountActivity
Diffstat (limited to 'app/src/main/java/it/niedermann/nextcloud/deck/ui/accountswitcher')
3 files changed, 213 insertions, 0 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/accountswitcher/AccountSwitcherAdapter.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/accountswitcher/AccountSwitcherAdapter.java new file mode 100644 index 000000000..5e9bb0695 --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/accountswitcher/AccountSwitcherAdapter.java @@ -0,0 +1,54 @@ +package it.niedermann.nextcloud.deck.ui.accountswitcher; + +import android.view.LayoutInflater; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.core.util.Consumer; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.ArrayList; +import java.util.List; + +import it.niedermann.nextcloud.deck.R; +import it.niedermann.nextcloud.deck.model.Account; + +public class AccountSwitcherAdapter extends RecyclerView.Adapter<AccountSwitcherViewHolder> { + + @NonNull + private final List<Account> localAccounts = new ArrayList<>(); + @NonNull + private final Consumer<Account> onAccountClick; + + public AccountSwitcherAdapter(@NonNull Consumer<Account> onAccountClick) { + this.onAccountClick = onAccountClick; + setHasStableIds(true); + } + + @Override + public long getItemId(int position) { + return localAccounts.get(position).getId(); + } + + @NonNull + @Override + public AccountSwitcherViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + return new AccountSwitcherViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_account_choose, parent, false)); + } + + @Override + public void onBindViewHolder(@NonNull AccountSwitcherViewHolder holder, int position) { + holder.bind(localAccounts.get(position), onAccountClick); + } + + @Override + public int getItemCount() { + return localAccounts.size(); + } + + public void setLocalAccounts(@NonNull List<Account> localAccounts) { + this.localAccounts.clear(); + this.localAccounts.addAll(localAccounts); + notifyDataSetChanged(); + } +} diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/accountswitcher/AccountSwitcherDialog.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/accountswitcher/AccountSwitcherDialog.java new file mode 100644 index 000000000..ee59a54d8 --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/accountswitcher/AccountSwitcherDialog.java @@ -0,0 +1,123 @@ +package it.niedermann.nextcloud.deck.ui.accountswitcher; + +import android.app.Dialog; +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; +import androidx.fragment.app.DialogFragment; +import androidx.lifecycle.ViewModelProvider; + +import com.bumptech.glide.Glide; +import com.bumptech.glide.request.RequestOptions; +import com.nextcloud.android.sso.AccountImporter; +import com.nextcloud.android.sso.exceptions.AndroidGetAccountsPermissionNotGranted; +import com.nextcloud.android.sso.exceptions.NextcloudFilesAppNotInstalledException; + +import it.niedermann.nextcloud.deck.R; +import it.niedermann.nextcloud.deck.databinding.DialogAccountSwitcherBinding; +import it.niedermann.nextcloud.deck.model.Account; +import it.niedermann.nextcloud.deck.persistence.sync.SyncManager; +import it.niedermann.nextcloud.deck.ui.MainViewModel; +import it.niedermann.nextcloud.deck.ui.manageaccounts.ManageAccountsActivity; +import it.niedermann.nextcloud.deck.util.ExceptionUtil; + +import static it.niedermann.nextcloud.deck.ui.MainActivity.ACTIVITY_MANAGE_ACCOUNTS; + +public class AccountSwitcherDialog extends DialogFragment { + + private static final String KEY_CURRENT_ACCOUNT_ID = "current_account_id"; + + private AccountSwitcherAdapter adapter; + private SyncManager syncManager; + private DialogAccountSwitcherBinding binding; + private MainViewModel viewModel; + private long currentAccountId; + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + + viewModel = new ViewModelProvider(requireActivity()).get(MainViewModel.class); + final Bundle args = getArguments(); + + if (args == null || !args.containsKey(KEY_CURRENT_ACCOUNT_ID)) { + throw new IllegalArgumentException("Please provide at least " + KEY_CURRENT_ACCOUNT_ID); + } else { + this.currentAccountId = args.getLong(KEY_CURRENT_ACCOUNT_ID); + } + + syncManager = new SyncManager(requireActivity()); + } + + @NonNull + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + binding = DialogAccountSwitcherBinding.inflate(requireActivity().getLayoutInflater()); + + + binding.accountItemLabel.setText(viewModel.getCurrentAccount().getName()); + Glide.with(requireContext()) + .load(viewModel.getCurrentAccount().getUrl() + "/index.php/avatar/" + Uri.encode(viewModel.getCurrentAccount().getUserName()) + "/64") + .error(R.drawable.ic_person_grey600_24dp) + .apply(RequestOptions.circleCropTransform()) + .into(binding.currentAccountItemAvatar); + binding.accountLayout.setOnClickListener((v) -> dismiss()); + + adapter = new AccountSwitcherAdapter((localAccount -> { + viewModel.setCurrentAccount(localAccount, localAccount.getServerDeckVersionAsObject().isSupported(requireContext())); + dismiss(); + })); + binding.accountsList.setAdapter(adapter); + + binding.addAccount.setOnClickListener((v) -> { + try { + AccountImporter.pickNewAccount(this); + } catch (NextcloudFilesAppNotInstalledException e) { + ExceptionUtil.handleNextcloudFilesAppNotInstalledException(requireContext(), e); + } catch (AndroidGetAccountsPermissionNotGranted e) { + AccountImporter.requestAndroidAccountPermissionsAndPickAccount(requireActivity()); + } + dismiss(); + }); + + binding.manageAccounts.setOnClickListener((v) -> { + requireActivity().startActivityForResult(new Intent(requireContext(), ManageAccountsActivity.class), ACTIVITY_MANAGE_ACCOUNTS); + dismiss(); + }); + + return new AlertDialog.Builder(requireContext()) + .setView(binding.getRoot()) + .create(); + } + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + syncManager.readAccounts().observe(getViewLifecycleOwner(), (localAccounts) -> { + for (Account localAccount : localAccounts) { + if (localAccount.getId() == viewModel.getCurrentAccount().getId()) { + localAccounts.remove(localAccount); + break; + } + } + adapter.setLocalAccounts(localAccounts); + }); + } + + public static DialogFragment newInstance(long currentAccountId) { + DialogFragment dialog = new AccountSwitcherDialog(); + + Bundle args = new Bundle(); + args.putLong(KEY_CURRENT_ACCOUNT_ID, currentAccountId); + dialog.setArguments(args); + + return dialog; + } + +} diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/accountswitcher/AccountSwitcherViewHolder.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/accountswitcher/AccountSwitcherViewHolder.java new file mode 100644 index 000000000..b3f6e5117 --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/accountswitcher/AccountSwitcherViewHolder.java @@ -0,0 +1,36 @@ +package it.niedermann.nextcloud.deck.ui.accountswitcher; + +import android.net.Uri; +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.core.util.Consumer; +import androidx.recyclerview.widget.RecyclerView; + +import com.bumptech.glide.Glide; +import com.bumptech.glide.request.RequestOptions; + +import it.niedermann.nextcloud.deck.R; +import it.niedermann.nextcloud.deck.databinding.ItemAccountChooseBinding; +import it.niedermann.nextcloud.deck.model.Account; + +public class AccountSwitcherViewHolder extends RecyclerView.ViewHolder { + + ItemAccountChooseBinding binding; + + public AccountSwitcherViewHolder(@NonNull View itemView) { + super(itemView); + binding = ItemAccountChooseBinding.bind(itemView); + } + + public void bind(@NonNull Account account, @NonNull Consumer<Account> onAccountClick) { + binding.accountItemLabel.setText(account.getUserName()); + Glide.with(itemView.getContext()) + .load(account.getUrl() + "/index.php/avatar/" + Uri.encode(account.getUserName()) + "/64") + .error(R.drawable.ic_person_grey600_24dp) + .apply(RequestOptions.circleCropTransform()) + .into(binding.accountItemAvatar); + itemView.setOnClickListener((v) -> onAccountClick.accept(account)); + binding.delete.setVisibility(View.GONE); + } +} |