Browse Source

Merge pull request #12083 from nextcloud/refactor/convert-to-kt-MultipleAccountsDialog

Convert Java to Kotlin Multiple Accounts Dialog
Andy Scherzinger 1 year ago
parent
commit
7d49856f87

+ 0 - 131
app/src/main/java/com/owncloud/android/ui/dialog/MultipleAccountsDialog.java

@@ -1,131 +0,0 @@
-/*
- *
- *  Nextcloud Android client application
- *
- *  @author Tobias Kaminsky
- *  @author Chris Narkiewicz <hello@ezaquarii.com>
- *
- *  Copyright (C) 2019 Tobias Kaminsky
- *  Copyright (C) 2019 Nextcloud GmbH
- *  Copyright (C) 2020 Chris Narkiewicz <hello@ezaquarii.com>
- *
- *  This program is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU Affero General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- *  GNU Affero General Public License for more details.
- *
- *  You should have received a copy of the GNU Affero General Public License
- *  along with this program. If not, see <https://www.gnu.org/licenses/>.
- *
- */
-
-package com.owncloud.android.ui.dialog;
-
-import android.app.Activity;
-import android.app.Dialog;
-import android.content.Context;
-import android.os.Bundle;
-import android.view.LayoutInflater;
-import android.view.View;
-
-import com.google.android.material.dialog.MaterialAlertDialogBuilder;
-import com.nextcloud.client.account.User;
-import com.nextcloud.client.account.UserAccountManager;
-import com.nextcloud.client.di.Injectable;
-import com.owncloud.android.R;
-import com.owncloud.android.databinding.MultipleAccountsBinding;
-import com.owncloud.android.ui.adapter.UserListAdapter;
-import com.owncloud.android.ui.adapter.UserListItem;
-import com.owncloud.android.utils.theme.ViewThemeUtils;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import javax.inject.Inject;
-
-import androidx.annotation.NonNull;
-import androidx.fragment.app.DialogFragment;
-import androidx.recyclerview.widget.LinearLayoutManager;
-
-public class MultipleAccountsDialog extends DialogFragment implements Injectable, UserListAdapter.ClickListener {
-
-    @Inject UserAccountManager accountManager;
-    @Inject ViewThemeUtils viewThemeUtils;
-    public boolean highlightCurrentlyActiveAccount = true;
-
-    @NonNull
-    @Override
-    public Dialog onCreateDialog(Bundle savedInstanceState) {
-        Activity activity = getActivity();
-        if (activity == null) {
-            throw new IllegalArgumentException("Activity may not be null");
-        }
-
-        // Inflate the layout for the dialog
-        LayoutInflater inflater = activity.getLayoutInflater();
-        MultipleAccountsBinding binding = MultipleAccountsBinding.inflate(inflater, null, false);
-
-        final Context parent = getActivity();
-        MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(binding.getRoot().getContext());
-
-        UserListAdapter adapter = new UserListAdapter(parent,
-                                                      accountManager,
-                                                      getAccountListItems(),
-                                                      this,
-                                                      false,
-                                                      highlightCurrentlyActiveAccount,
-                                                      false,
-                                                      viewThemeUtils);
-
-        binding.list.setHasFixedSize(true);
-        binding.list.setLayoutManager(new LinearLayoutManager(activity));
-        binding.list.setAdapter(adapter);
-
-        builder.setView(binding.getRoot()).setTitle(R.string.common_choose_account);
-
-        viewThemeUtils.dialog.colorMaterialAlertDialogBackground(binding.getRoot().getContext(), builder);
-
-        return builder.create();
-    }
-
-    /**
-     * creates the account list items list including the add-account action in case
-     * multiaccount_support is enabled.
-     *
-     * @return list of account list items
-     */
-    private List<UserListItem> getAccountListItems() {
-        List<User> users = accountManager.getAllUsers();
-        List<UserListItem> adapterUserList = new ArrayList<>(users.size());
-        for (User user : users) {
-            adapterUserList.add(new UserListItem(user));
-        }
-
-        return adapterUserList;
-    }
-
-    @Override
-    public void onOptionItemClicked(User user, View view) {
-        // By default, access account if option is clicked
-        onAccountClicked(user);
-    }
-
-    @Override
-    public void onAccountClicked(User user) {
-        final AccountChooserInterface parentActivity = (AccountChooserInterface) getActivity();
-        if (parentActivity != null) {
-            parentActivity.onAccountChosen(user);
-        }
-        dismiss();
-    }
-
-    @Override
-    public void onDestroyView() {
-        super.onDestroyView();
-    }
-}

+ 108 - 0
app/src/main/java/com/owncloud/android/ui/dialog/MultipleAccountsDialog.kt

@@ -0,0 +1,108 @@
+/*
+ *
+ *  Nextcloud Android client application
+ *
+ *  @author Tobias Kaminsky
+ *  @author Chris Narkiewicz <hello@ezaquarii.com>
+ *
+ *  Copyright (C) 2019 Tobias Kaminsky
+ *  Copyright (C) 2019 Nextcloud GmbH
+ *  Copyright (C) 2020 Chris Narkiewicz <hello@ezaquarii.com>
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU Affero General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *  GNU Affero General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Affero General Public License
+ *  along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+package com.owncloud.android.ui.dialog
+
+import android.app.Dialog
+import android.os.Bundle
+import android.view.View
+import androidx.fragment.app.DialogFragment
+import androidx.recyclerview.widget.LinearLayoutManager
+import com.google.android.material.dialog.MaterialAlertDialogBuilder
+import com.nextcloud.client.account.User
+import com.nextcloud.client.account.UserAccountManager
+import com.nextcloud.client.di.Injectable
+import com.owncloud.android.R
+import com.owncloud.android.databinding.MultipleAccountsBinding
+import com.owncloud.android.ui.adapter.UserListAdapter
+import com.owncloud.android.ui.adapter.UserListItem
+import com.owncloud.android.utils.theme.ViewThemeUtils
+import javax.inject.Inject
+
+class MultipleAccountsDialog : DialogFragment(), Injectable, UserListAdapter.ClickListener {
+    @JvmField
+    @Inject
+    var accountManager: UserAccountManager? = null
+
+    @JvmField
+    @Inject
+    var viewThemeUtils: ViewThemeUtils? = null
+    var highlightCurrentlyActiveAccount = true
+
+    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
+        val inflater = requireActivity().layoutInflater
+        val binding = MultipleAccountsBinding.inflate(inflater, null, false)
+
+        val builder = MaterialAlertDialogBuilder(binding.root.context)
+        val adapter = UserListAdapter(
+            requireActivity(),
+            accountManager,
+            accountListItems,
+            this,
+            false,
+            highlightCurrentlyActiveAccount,
+            false,
+            viewThemeUtils
+        )
+
+        binding.list.setHasFixedSize(true)
+        binding.list.layoutManager = LinearLayoutManager(requireActivity())
+        binding.list.adapter = adapter
+
+        builder.setView(binding.root).setTitle(R.string.common_choose_account)
+
+        viewThemeUtils?.dialog?.colorMaterialAlertDialogBackground(requireContext(), builder)
+
+        return builder.create()
+    }
+
+    private val accountListItems: List<UserListItem>
+        /**
+         * creates the account list items list including the add-account action in case
+         * multiaccount_support is enabled.
+         *
+         * @return list of account list items
+         */
+        get() {
+            val users = accountManager?.allUsers ?: listOf()
+
+            val adapterUserList: MutableList<UserListItem> = ArrayList(users.size)
+            for (user in users) {
+                adapterUserList.add(UserListItem(user))
+            }
+            return adapterUserList
+        }
+
+    override fun onOptionItemClicked(user: User, view: View) {
+        // By default, access account if option is clicked
+        onAccountClicked(user)
+    }
+
+    override fun onAccountClicked(user: User) {
+        val parentActivity = activity as AccountChooserInterface?
+        parentActivity?.onAccountChosen(user)
+        dismiss()
+    }
+}