Skip to content

Commit

Permalink
Fix columns for grid layout for MainFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelMess committed Apr 24, 2022
1 parent 89d63cd commit 46241fc
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
import static android.os.Build.VERSION_CODES.Q;
import static com.amaze.filemanager.filesystem.ssh.SshConnectionPool.SSH_URI_PREFIX;
import static com.amaze.filemanager.ui.fragments.preference_fragments.PreferencesConstants.PREFERENCE_GRID_COLUMNS_DEFAULT;
import static com.amaze.filemanager.ui.fragments.preference_fragments.PreferencesConstants.PREFERENCE_SHOW_DIVIDERS;
import static com.amaze.filemanager.ui.fragments.preference_fragments.PreferencesConstants.PREFERENCE_SHOW_GOBACK_BUTTON;
import static com.amaze.filemanager.ui.fragments.preference_fragments.PreferencesConstants.PREFERENCE_SHOW_HIDDENFILES;
Expand Down Expand Up @@ -265,7 +266,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
mLayoutManager = new CustomScrollLinearLayoutManager(getContext());
listView.setLayoutManager(mLayoutManager);
} else {
if (mainFragmentViewModel.getColumns() == -1 || mainFragmentViewModel.getColumns() == 0)
if (mainFragmentViewModel.getColumns() == null)
mLayoutManagerGrid = new CustomScrollGridLayoutManager(getActivity(), 3);
else
mLayoutManagerGrid =
Expand Down Expand Up @@ -1617,13 +1618,14 @@ private boolean getBoolean(String key) {

@Override
public void onGlobalLayout() {
if (mainFragmentViewModel.getColumns() == 0 || mainFragmentViewModel.getColumns() == -1) {
int screen_width = listView.getWidth();
int dptopx = Utils.dpToPx(getContext(), 115);
mainFragmentViewModel.setColumns(screen_width / dptopx);
if (mainFragmentViewModel.getColumns() == 0 || mainFragmentViewModel.getColumns() == -1) {
mainFragmentViewModel.setColumns(3);
if (mainFragmentViewModel.getColumns() == null) {
int screenWidth = listView.getWidth();
int dpToPx = Utils.dpToPx(requireContext(), 115);
if(dpToPx == 0) {
// HACK to fix a crash see #3249
dpToPx = 1;
}
mainFragmentViewModel.setColumns(screenWidth / dpToPx);
if (!mainFragmentViewModel.isList()) {
mLayoutManagerGrid.setSpanCount(mainFragmentViewModel.getColumns());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import com.amaze.filemanager.database.CloudHandler
import com.amaze.filemanager.file_operations.filesystem.OpenMode
import com.amaze.filemanager.filesystem.HybridFileParcelable
import com.amaze.filemanager.ui.fragments.preference_fragments.PreferencesConstants
import com.amaze.filemanager.ui.fragments.preference_fragments.PreferencesConstants.PREFERENCE_GRID_COLUMNS
import com.amaze.filemanager.ui.fragments.preference_fragments.PreferencesConstants.PREFERENCE_GRID_COLUMNS_DEFAULT
import com.amaze.filemanager.utils.DataUtils
import java.util.*

Expand All @@ -45,7 +47,7 @@ class MainFragmentViewModel : ViewModel() {

var fileCount = 0
var folderCount: Int = 0
var columns: Int = 0
var columns: Int? = null
var smbPath: String? = null
var searchHelper = ArrayList<HybridFileParcelable>()
var no = 0
Expand Down Expand Up @@ -141,15 +143,9 @@ class MainFragmentViewModel : ViewModel() {
* Initialize column number from preference
*/
fun initColumns(sharedPreferences: SharedPreferences) {
if (columns == 0) {
sharedPreferences.getString(
PreferencesConstants
.PREFERENCE_GRID_COLUMNS,
"-1"
)?.toInt()?.run {
columns = this
}
}
val columnPreference = sharedPreferences.getString(PREFERENCE_GRID_COLUMNS, PREFERENCE_GRID_COLUMNS_DEFAULT)
Objects.requireNonNull(columnPreference)
columns = columnPreference?.toInt()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ import android.os.Bundle
import androidx.preference.Preference
import com.afollestad.materialdialogs.MaterialDialog
import com.amaze.filemanager.R
import com.amaze.filemanager.ui.fragments.preference_fragments.PreferencesConstants.PREFERENCE_GRID_COLUMNS
import com.amaze.filemanager.ui.fragments.preference_fragments.PreferencesConstants.PREFERENCE_GRID_COLUMNS_DEFAULT
import com.amaze.filemanager.ui.theme.AppTheme
import java.util.*

class AppearancePrefsFragment : BasePrefsFragment() {
override val title = R.string.appearance

private lateinit var gridColumnItems: Array<String>
/**
* The actual value saved for the preference, to see the localized strings see [R.array.columns]
*/
private val savedPreferenceValues = listOf(PREFERENCE_GRID_COLUMNS_DEFAULT, "2", "3", "4", "5", "6")
private var currentTheme = 0
private var gridColumnPref: Preference? = null

Expand All @@ -56,28 +62,38 @@ class AppearancePrefsFragment : BasePrefsFragment() {
}

private val onClickGridColumn = Preference.OnPreferenceClickListener {
val builder = MaterialDialog.Builder(activity)
builder.theme(activity.utilsProvider.appTheme.getMaterialDialogTheme(activity))
builder.title(R.string.gridcolumnno)
var current = activity
.prefs
.getString(PreferencesConstants.PREFERENCE_GRID_COLUMNS, "-1")!!
.toInt() - 1
if (current < 0) current = 0
builder
.items(R.array.columns)
.itemsCallbackSingleChoice(current) { dialog, _, which, _ ->
val editor = activity.prefs.edit()
editor.putString(
PreferencesConstants.PREFERENCE_GRID_COLUMNS,
if (which != 0) gridColumnItems[which] else "-1"
)
editor.apply()
dialog.dismiss()
updateGridColumnSummary()
true
val dialog = MaterialDialog.Builder(activity).also { builder ->
builder.theme(activity.utilsProvider.appTheme.getMaterialDialogTheme(activity))
builder.title(R.string.gridcolumnno)
val columnsPreference = activity
.prefs
.getString(PREFERENCE_GRID_COLUMNS, PREFERENCE_GRID_COLUMNS_DEFAULT)

Objects.requireNonNull(columnsPreference)
val current = when (columnsPreference) {
null -> {
PREFERENCE_GRID_COLUMNS_DEFAULT.toInt()
}
else -> {
columnsPreference.toInt() - 1
}
}
builder.build().show()

builder
.items(R.array.columns)
.itemsCallbackSingleChoice(current) { dialog, _, which, _ ->
val editor = activity.prefs.edit()
editor.putString(
PREFERENCE_GRID_COLUMNS,
savedPreferenceValues[which]
)
editor.apply()
dialog.dismiss()
updateGridColumnSummary()
true
}
}.build()
dialog.show()

true
}
Expand Down Expand Up @@ -112,23 +128,13 @@ class AppearancePrefsFragment : BasePrefsFragment() {
true
}

gridColumnItems = resources.getStringArray(R.array.columns)
gridColumnPref = findPreference(PreferencesConstants.PREFERENCE_GRID_COLUMNS)
gridColumnPref = findPreference(PREFERENCE_GRID_COLUMNS)
updateGridColumnSummary()
gridColumnPref?.onPreferenceClickListener = onClickGridColumn
}

private fun updateGridColumnSummary() {
val gridColumnItems = resources.getStringArray(R.array.columns)

activity.prefs
.getString(PreferencesConstants.PREFERENCE_GRID_COLUMNS, "-1")
?.let {
if (it == "-1") {
gridColumnPref?.summary = gridColumnItems[0]
} else {
gridColumnPref?.summary = it
}
}
val preferenceColumns = activity.prefs.getString(PREFERENCE_GRID_COLUMNS, PREFERENCE_GRID_COLUMNS_DEFAULT)
gridColumnPref?.summary = preferenceColumns
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ object PreferencesConstants {
const val PREFERENCE_COLORED_NAVIGATION = "colorednavigation"
const val PREFERENCE_SELECT_COLOR_CONFIG = "selectcolorconfig"
const val PREFERENCE_INTELLI_HIDE_TOOLBAR = "intelliHideToolbar"
const val PREFERENCE_GRID_COLUMNS = "columns"
const val PREFERENCE_GRID_COLUMNS = "columnsGrid"
const val PREFERENCE_GRID_COLUMNS_DEFAULT = "3"
const val PREFERENCE_ENABLE_MARQUEE_FILENAME = "enableMarqueeFilename"

// color_prefs.xml
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/xml/appearance_prefs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
app:summary="@string/intellihide_toolbar_summary"
app:title="@string/intellihide_toolbar_title" />
<Preference
app:key="columns"
app:key="columnsGrid"
app:title="@string/gridcolumnno" />
<com.amaze.filemanager.ui.views.preference.CheckBox
app:defaultValue="true"
Expand Down

0 comments on commit 46241fc

Please sign in to comment.