From e24b5b8bfe304f97bac161243de52855f08fe92e Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Sat, 18 Jul 2020 08:02:57 -0500 Subject: [PATCH] Convert AddAccountDialog to Kotlin --- .../java/org/tasks/sync/AddAccountDialog.java | 87 ------------------- .../java/org/tasks/sync/AddAccountDialog.kt | 73 ++++++++++++++++ 2 files changed, 73 insertions(+), 87 deletions(-) delete mode 100644 app/src/main/java/org/tasks/sync/AddAccountDialog.java create mode 100644 app/src/main/java/org/tasks/sync/AddAccountDialog.kt diff --git a/app/src/main/java/org/tasks/sync/AddAccountDialog.java b/app/src/main/java/org/tasks/sync/AddAccountDialog.java deleted file mode 100644 index 5c8ac597c..000000000 --- a/app/src/main/java/org/tasks/sync/AddAccountDialog.java +++ /dev/null @@ -1,87 +0,0 @@ -package org.tasks.sync; - -import android.app.Activity; -import android.content.Intent; -import android.content.res.TypedArray; -import android.net.Uri; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ArrayAdapter; -import android.widget.ImageView; -import android.widget.TextView; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import com.todoroo.astrid.gtasks.auth.GtasksLoginActivity; -import org.tasks.R; -import org.tasks.caldav.CaldavAccountSettingsActivity; -import org.tasks.dialogs.DialogBuilder; -import org.tasks.etesync.EteSyncAccountSettingsActivity; -import org.tasks.preferences.fragments.SynchronizationKt; -import org.tasks.themes.DrawableUtil; - -public class AddAccountDialog { - public static void showAddAccountDialog(Activity activity, DialogBuilder dialogBuilder) { - String[] services = activity.getResources().getStringArray(R.array.synchronization_services); - String[] descriptions = - activity.getResources().getStringArray(R.array.synchronization_services_description); - TypedArray typedArray = - activity.getResources().obtainTypedArray(R.array.synchronization_services_icons); - int[] icons = new int[typedArray.length()]; - for (int i = 0; i < icons.length; i++) { - icons[i] = typedArray.getResourceId(i, 0); - } - typedArray.recycle(); - ArrayAdapter adapter = - new ArrayAdapter( - activity, R.layout.simple_list_item_2_themed, R.id.text1, services) { - @NonNull - @Override - public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { - View view = super.getView(position, convertView, parent); - view.findViewById(R.id.text1).setText(services[position]); - view.findViewById(R.id.text2).setText(descriptions[position]); - ImageView icon = view.findViewById(R.id.image_view); - icon.setImageDrawable(DrawableUtil.getWrapped(getContext(), icons[position])); - if (position == 1) { - icon.getDrawable().setTint(getContext().getColor(R.color.icon_tint)); - } - return view; - } - }; - - dialogBuilder - .newDialog() - .setTitle(R.string.choose_synchronization_service) - .setSingleChoiceItems( - adapter, - -1, - (dialog, which) -> { - switch (which) { - case 0: - activity.startActivityForResult( - new Intent(activity, GtasksLoginActivity.class), - SynchronizationKt.REQUEST_GOOGLE_TASKS); - break; - case 1: - activity.startActivityForResult( - new Intent(activity, CaldavAccountSettingsActivity.class), - SynchronizationKt.REQUEST_CALDAV_SETTINGS); - break; - case 2: - activity.startActivityForResult( - new Intent(activity, EteSyncAccountSettingsActivity.class), - SynchronizationKt.REQUEST_CALDAV_SETTINGS); - break; - } - dialog.dismiss(); - }) - .setNeutralButton( - R.string.help, - (dialog, which) -> - activity.startActivity( - new Intent( - Intent.ACTION_VIEW, Uri.parse(activity.getString(R.string.help_url_sync))))) - .setNegativeButton(android.R.string.cancel, null) - .show(); - } -} diff --git a/app/src/main/java/org/tasks/sync/AddAccountDialog.kt b/app/src/main/java/org/tasks/sync/AddAccountDialog.kt new file mode 100644 index 000000000..f83066a49 --- /dev/null +++ b/app/src/main/java/org/tasks/sync/AddAccountDialog.kt @@ -0,0 +1,73 @@ +package org.tasks.sync + +import android.app.Activity +import android.content.DialogInterface +import android.content.Intent +import android.net.Uri +import android.view.View +import android.view.ViewGroup +import android.widget.ArrayAdapter +import android.widget.ImageView +import android.widget.TextView +import com.todoroo.astrid.gtasks.auth.GtasksLoginActivity +import org.tasks.R +import org.tasks.caldav.CaldavAccountSettingsActivity +import org.tasks.dialogs.DialogBuilder +import org.tasks.etesync.EteSyncAccountSettingsActivity +import org.tasks.preferences.fragments.REQUEST_CALDAV_SETTINGS +import org.tasks.preferences.fragments.REQUEST_GOOGLE_TASKS +import org.tasks.themes.DrawableUtil + +object AddAccountDialog { + fun showAddAccountDialog(activity: Activity, dialogBuilder: DialogBuilder) { + val services = activity.resources.getStringArray(R.array.synchronization_services) + val descriptions = activity.resources.getStringArray(R.array.synchronization_services_description) + val typedArray = activity.resources.obtainTypedArray(R.array.synchronization_services_icons) + val icons = IntArray(typedArray.length()) + for (i in icons.indices) { + icons[i] = typedArray.getResourceId(i, 0) + } + typedArray.recycle() + val adapter: ArrayAdapter = object : ArrayAdapter( + activity, R.layout.simple_list_item_2_themed, R.id.text1, services) { + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + val view = super.getView(position, convertView, parent) + view.findViewById(R.id.text1).text = services[position] + view.findViewById(R.id.text2).text = descriptions[position] + val icon = view.findViewById(R.id.image_view) + icon.setImageDrawable(DrawableUtil.getWrapped(context, icons[position])) + if (position == 1) { + icon.drawable.setTint(context.getColor(R.color.icon_tint)) + } + return view + } + } + dialogBuilder + .newDialog() + .setTitle(R.string.choose_synchronization_service) + .setSingleChoiceItems( + adapter, + -1 + ) { dialog: DialogInterface, which: Int -> + when (which) { + 0 -> activity.startActivityForResult( + Intent(activity, GtasksLoginActivity::class.java), + REQUEST_GOOGLE_TASKS) + 1 -> activity.startActivityForResult( + Intent(activity, CaldavAccountSettingsActivity::class.java), + REQUEST_CALDAV_SETTINGS) + 2 -> activity.startActivityForResult( + Intent(activity, EteSyncAccountSettingsActivity::class.java), + REQUEST_CALDAV_SETTINGS) + } + dialog.dismiss() + } + .setNeutralButton(R.string.help) { _, _ -> + activity.startActivity( + Intent( + Intent.ACTION_VIEW, Uri.parse(activity.getString(R.string.help_url_sync)))) + } + .setNegativeButton(android.R.string.cancel, null) + .show() + } +} \ No newline at end of file