From 8cc9c274167b6a39e1ff4589567202677ffbcbe4 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 13 Nov 2016 21:43:56 +0100 Subject: [PATCH] convert ChangeSortingDialog to kotlin --- .../gallery/activities/MainActivity.java | 16 +-- .../gallery/activities/MediaActivity.java | 16 +-- .../gallery/dialogs/ChangeSorting.java | 99 ------------------- .../gallery/dialogs/ChangeSortingDialog.kt | 87 ++++++++++++++++ ..._sorting.xml => dialog_change_sorting.xml} | 0 5 files changed, 103 insertions(+), 115 deletions(-) delete mode 100644 app/src/main/java/com/simplemobiletools/gallery/dialogs/ChangeSorting.java create mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeSortingDialog.kt rename app/src/main/res/layout/{change_sorting.xml => dialog_change_sorting.xml} (100%) diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java index c17e6a563..a40848ecd 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java @@ -32,7 +32,7 @@ import com.simplemobiletools.gallery.R; import com.simplemobiletools.gallery.Utils; import com.simplemobiletools.gallery.adapters.DirectoryAdapter; import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask; -import com.simplemobiletools.gallery.dialogs.ChangeSorting; +import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog; import com.simplemobiletools.gallery.dialogs.CopyDialog; import com.simplemobiletools.gallery.dialogs.RenameDirectoryDialog; import com.simplemobiletools.gallery.models.Directory; @@ -51,7 +51,7 @@ import butterknife.ButterKnife; public class MainActivity extends SimpleActivity implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, GridView.OnTouchListener, - SwipeRefreshLayout.OnRefreshListener, ChangeSorting.ChangeDialogListener, GetDirectoriesAsynctask.GetDirectoriesListener { + SwipeRefreshLayout.OnRefreshListener, GetDirectoriesAsynctask.GetDirectoriesListener { @BindView(R.id.directories_grid) GridView mGridView; @BindView(R.id.directories_holder) SwipeRefreshLayout mSwipeRefreshLayout; @@ -180,7 +180,12 @@ public class MainActivity extends SimpleActivity } private void showSortingDialog() { - new ChangeSorting(this, true); + new ChangeSortingDialog(this, true, new ChangeSortingDialog.OnChangeSortingListener() { + @Override + public void sortingChanged() { + getDirectories(); + } + }); } private void prepareForDeleting() { @@ -585,11 +590,6 @@ public class MainActivity extends SimpleActivity mSwipeRefreshLayout.setRefreshing(false); } - @Override - public void sortingDialogClosed() { - getDirectories(); - } - @Override public void gotDirectories(@NotNull ArrayList dirs) { mIsGettingDirs = false; diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java index 19aac4f37..32c2f1538 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java @@ -35,7 +35,7 @@ import com.simplemobiletools.gallery.Constants; import com.simplemobiletools.gallery.R; import com.simplemobiletools.gallery.Utils; import com.simplemobiletools.gallery.adapters.MediaAdapter; -import com.simplemobiletools.gallery.dialogs.ChangeSorting; +import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog; import com.simplemobiletools.gallery.dialogs.CopyDialog; import com.simplemobiletools.gallery.models.Medium; @@ -51,7 +51,7 @@ import butterknife.ButterKnife; public class MediaActivity extends SimpleActivity implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, GridView.OnTouchListener, - SwipeRefreshLayout.OnRefreshListener, ChangeSorting.ChangeDialogListener { + SwipeRefreshLayout.OnRefreshListener { private static final String TAG = MediaActivity.class.getSimpleName(); @BindView(R.id.media_grid) GridView mGridView; @BindView(R.id.media_holder) SwipeRefreshLayout mSwipeRefreshLayout; @@ -168,7 +168,12 @@ public class MediaActivity extends SimpleActivity } private void showSortingDialog() { - new ChangeSorting(this, false); + new ChangeSortingDialog(this, false, new ChangeSortingDialog.OnChangeSortingListener() { + @Override + public void sortingChanged() { + initializeGallery(); + } + }); } private void hideDirectory() { @@ -549,9 +554,4 @@ public class MediaActivity extends SimpleActivity initializeGallery(); mSwipeRefreshLayout.setRefreshing(false); } - - @Override - public void sortingDialogClosed() { - initializeGallery(); - } } diff --git a/app/src/main/java/com/simplemobiletools/gallery/dialogs/ChangeSorting.java b/app/src/main/java/com/simplemobiletools/gallery/dialogs/ChangeSorting.java deleted file mode 100644 index 83cf701b8..000000000 --- a/app/src/main/java/com/simplemobiletools/gallery/dialogs/ChangeSorting.java +++ /dev/null @@ -1,99 +0,0 @@ -package com.simplemobiletools.gallery.dialogs; - -import android.app.Activity; -import android.content.DialogInterface; -import android.support.v7.app.AlertDialog; -import android.view.View; -import android.widget.RadioButton; -import android.widget.RadioGroup; - -import com.simplemobiletools.gallery.Config; -import com.simplemobiletools.gallery.Constants; -import com.simplemobiletools.gallery.R; - -public class ChangeSorting extends AlertDialog.Builder implements DialogInterface.OnClickListener { - private static Config mConfig; - private static ChangeDialogListener mListener; - private static View mHolder; - - private static int mCurrSorting; - private static boolean mIsDirectorySorting; - - public ChangeSorting(Activity act, boolean isDirectorySorting) { - super(act.getApplicationContext()); - - mIsDirectorySorting = isDirectorySorting; - mListener = (ChangeDialogListener) act; - mConfig = Config.Companion.newInstance(getContext()); - mHolder = act.getLayoutInflater().inflate(R.layout.change_sorting, null); - - final AlertDialog.Builder builder = new AlertDialog.Builder(act); - builder.setTitle(act.getResources().getString(R.string.sort_by)); - builder.setView(mHolder); - - mCurrSorting = (mIsDirectorySorting ? mConfig.getDirectorySorting() : mConfig.getSorting()); - setupSortRadio(); - setupOrderRadio(); - - builder.setPositiveButton(R.string.ok, this); - builder.setNegativeButton(R.string.cancel, null); - builder.show(); - } - - private void setupSortRadio() { - final RadioGroup sortingRadio = (RadioGroup) mHolder.findViewById(R.id.dialog_radio_sorting); - RadioButton sortBtn = (RadioButton) sortingRadio.findViewById(R.id.dialog_radio_name); - if ((mCurrSorting & Constants.SORT_BY_DATE) != 0) { - sortBtn = (RadioButton) sortingRadio.findViewById(R.id.dialog_radio_date); - } else if ((mCurrSorting & Constants.SORT_BY_SIZE) != 0) { - sortBtn = (RadioButton) sortingRadio.findViewById(R.id.dialog_radio_size); - } - sortBtn.setChecked(true); - } - - private void setupOrderRadio() { - final RadioGroup orderRadio = (RadioGroup) mHolder.findViewById(R.id.dialog_radio_order); - RadioButton orderBtn = (RadioButton) orderRadio.findViewById(R.id.dialog_radio_ascending); - if ((mCurrSorting & Constants.SORT_DESCENDING) != 0) { - orderBtn = (RadioButton) orderRadio.findViewById(R.id.dialog_radio_descending); - } - orderBtn.setChecked(true); - } - - @Override - public void onClick(DialogInterface dialog, int which) { - - final RadioGroup sortingRadio = (RadioGroup) mHolder.findViewById(R.id.dialog_radio_sorting); - int sorting = Constants.SORT_BY_NAME; - switch (sortingRadio.getCheckedRadioButtonId()) { - case R.id.dialog_radio_date: - sorting = Constants.SORT_BY_DATE; - break; - case R.id.dialog_radio_size: - sorting = Constants.SORT_BY_SIZE; - break; - default: - break; - } - - final RadioGroup orderRadio = (RadioGroup) mHolder.findViewById(R.id.dialog_radio_order); - if (orderRadio.getCheckedRadioButtonId() == R.id.dialog_radio_descending) { - sorting |= Constants.SORT_DESCENDING; - } - - if (mIsDirectorySorting) { - if (mConfig.getDirectorySorting() != sorting) { - mConfig.setDirectorySorting(sorting); - } - } else { - if (mConfig.getSorting() != sorting) { - mConfig.setSorting(sorting); - } - } - mListener.sortingDialogClosed(); - } - - public interface ChangeDialogListener { - void sortingDialogClosed(); - } -} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeSortingDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeSortingDialog.kt new file mode 100644 index 000000000..70e505f0f --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeSortingDialog.kt @@ -0,0 +1,87 @@ +package com.simplemobiletools.gallery.dialogs + +import android.app.Activity +import android.content.DialogInterface +import android.support.v7.app.AlertDialog +import android.view.LayoutInflater +import android.view.View +import com.simplemobiletools.gallery.Config +import com.simplemobiletools.gallery.Constants +import com.simplemobiletools.gallery.R +import kotlinx.android.synthetic.main.dialog_change_sorting.view.* + +class ChangeSortingDialog(val activity: Activity, val isDirectorySorting: Boolean, val listener: OnChangeSortingListener) : DialogInterface.OnClickListener { + companion object { + private var currSorting = 0 + + lateinit var config: Config + lateinit var view: View + } + + init { + config = Config.newInstance(activity) + view = LayoutInflater.from(activity).inflate(R.layout.dialog_change_sorting, null) + + AlertDialog.Builder(activity) + .setTitle(activity.resources.getString(R.string.sort_by)) + .setView(view) + .setPositiveButton(R.string.ok, this) + .setNegativeButton(R.string.cancel, null) + .create() + .show() + + currSorting = if (isDirectorySorting) config.directorySorting else config.sorting + setupSortRadio() + setupOrderRadio() + } + + private fun setupSortRadio() { + val sortingRadio = view.dialog_radio_sorting + var sortBtn = sortingRadio.dialog_radio_name + + if (currSorting and Constants.SORT_BY_DATE != 0) { + sortBtn = sortingRadio.dialog_radio_date + } else if (currSorting and Constants.SORT_BY_SIZE != 0) { + sortBtn = sortingRadio.dialog_radio_size + } + sortBtn.isChecked = true + } + + private fun setupOrderRadio() { + val orderRadio = view.dialog_radio_order + var orderBtn = orderRadio.dialog_radio_ascending + + if (currSorting and Constants.SORT_DESCENDING != 0) { + orderBtn = orderRadio.dialog_radio_descending + } + orderBtn.isChecked = true + } + + override fun onClick(dialog: DialogInterface, which: Int) { + val sortingRadio = view.dialog_radio_sorting + var sorting = when (sortingRadio.checkedRadioButtonId) { + R.id.dialog_radio_name -> Constants.SORT_BY_NAME + R.id.dialog_radio_date -> Constants.SORT_BY_DATE + else -> Constants.SORT_BY_SIZE + } + + if (view.dialog_radio_order.checkedRadioButtonId == R.id.dialog_radio_descending) { + sorting = sorting or Constants.SORT_DESCENDING + } + + if (isDirectorySorting) { + if (config.directorySorting != sorting) { + config.directorySorting = sorting + } + } else { + if (config.sorting != sorting) { + config.sorting = sorting + } + } + listener.sortingChanged() + } + + interface OnChangeSortingListener { + fun sortingChanged() + } +} diff --git a/app/src/main/res/layout/change_sorting.xml b/app/src/main/res/layout/dialog_change_sorting.xml similarity index 100% rename from app/src/main/res/layout/change_sorting.xml rename to app/src/main/res/layout/dialog_change_sorting.xml