mirror of
				https://github.com/KokaKiwi/BarInsta
				synced 2025-10-31 11:35:34 +00:00 
			
		
		
		
	Convert PostsLayoutPreferencesDialogFragment to kotlin
This commit is contained in:
		
							parent
							
								
									e3bf083b52
								
							
						
					
					
						commit
						c06deee84e
					
				| @ -1,205 +0,0 @@ | ||||
| package awais.instagrabber.dialogs; | ||||
| 
 | ||||
| import android.app.Dialog; | ||||
| import android.content.Context; | ||||
| import android.os.Bundle; | ||||
| import android.view.LayoutInflater; | ||||
| import android.view.View; | ||||
| 
 | ||||
| import androidx.annotation.NonNull; | ||||
| import androidx.annotation.Nullable; | ||||
| import androidx.fragment.app.DialogFragment; | ||||
| 
 | ||||
| import com.google.android.material.dialog.MaterialAlertDialogBuilder; | ||||
| 
 | ||||
| import awais.instagrabber.R; | ||||
| import awais.instagrabber.databinding.DialogPostLayoutPreferencesBinding; | ||||
| import awais.instagrabber.models.PostsLayoutPreferences; | ||||
| 
 | ||||
| import static awais.instagrabber.utils.Utils.settingsHelper; | ||||
| 
 | ||||
| public class PostsLayoutPreferencesDialogFragment extends DialogFragment { | ||||
| 
 | ||||
|     private final OnApplyListener onApplyListener; | ||||
|     private final PostsLayoutPreferences.Builder preferencesBuilder; | ||||
|     private final String layoutPreferenceKey; | ||||
|     private DialogPostLayoutPreferencesBinding binding; | ||||
|     private Context context; | ||||
| 
 | ||||
|     public PostsLayoutPreferencesDialogFragment(final String layoutPreferenceKey, | ||||
|                                                 @NonNull final OnApplyListener onApplyListener) { | ||||
|         this.layoutPreferenceKey = layoutPreferenceKey; | ||||
|         final PostsLayoutPreferences preferences = PostsLayoutPreferences.fromJson(settingsHelper.getString(layoutPreferenceKey)); | ||||
|         this.preferencesBuilder = PostsLayoutPreferences.builder().mergeFrom(preferences); | ||||
|         this.onApplyListener = onApplyListener; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void onAttach(@NonNull final Context context) { | ||||
|         super.onAttach(context); | ||||
|         this.context = context; | ||||
|     } | ||||
| 
 | ||||
|     @NonNull | ||||
|     @Override | ||||
|     public Dialog onCreateDialog(@Nullable final Bundle savedInstanceState) { | ||||
|         binding = DialogPostLayoutPreferencesBinding.inflate(LayoutInflater.from(context), null, false); | ||||
|         init(); | ||||
|         return new MaterialAlertDialogBuilder(context) | ||||
|                 .setView(binding.getRoot()) | ||||
|                 .setPositiveButton(R.string.apply, (dialog, which) -> { | ||||
|                     final PostsLayoutPreferences preferences = preferencesBuilder.build(); | ||||
|                     final String json = preferences.getJson(); | ||||
|                     settingsHelper.putString(layoutPreferenceKey, json); | ||||
|                     onApplyListener.onApply(preferences); | ||||
|                 }) | ||||
|                 .create(); | ||||
|     } | ||||
| 
 | ||||
|     private void init() { | ||||
|         initLayoutToggle(); | ||||
|         if (preferencesBuilder.getType() != PostsLayoutPreferences.PostsLayoutType.LINEAR) { | ||||
|             initStaggeredOrGridOptions(); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private void initStaggeredOrGridOptions() { | ||||
|         initColCountToggle(); | ||||
|         initNamesToggle(); | ||||
|         initAvatarsToggle(); | ||||
|         initCornersToggle(); | ||||
|         initGapToggle(); | ||||
|     } | ||||
| 
 | ||||
|     private void initLayoutToggle() { | ||||
|         final int selectedLayoutId = getSelectedLayoutId(); | ||||
|         binding.layoutToggle.check(selectedLayoutId); | ||||
|         if (selectedLayoutId == R.id.layout_linear) { | ||||
|             binding.staggeredOrGridOptions.setVisibility(View.GONE); | ||||
|         } | ||||
|         binding.layoutToggle.addOnButtonCheckedListener((group, checkedId, isChecked) -> { | ||||
|             if (isChecked) { | ||||
|                 if (checkedId == R.id.layout_linear) { | ||||
|                     preferencesBuilder.setType(PostsLayoutPreferences.PostsLayoutType.LINEAR); | ||||
|                     preferencesBuilder.setColCount(1); | ||||
|                     binding.staggeredOrGridOptions.setVisibility(View.GONE); | ||||
|                 } else if (checkedId == R.id.layout_staggered) { | ||||
|                     preferencesBuilder.setType(PostsLayoutPreferences.PostsLayoutType.STAGGERED_GRID); | ||||
|                     if (preferencesBuilder.getColCount() == 1) { | ||||
|                         preferencesBuilder.setColCount(2); | ||||
|                     } | ||||
|                     binding.staggeredOrGridOptions.setVisibility(View.VISIBLE); | ||||
|                     initStaggeredOrGridOptions(); | ||||
|                 } else { | ||||
|                     preferencesBuilder.setType(PostsLayoutPreferences.PostsLayoutType.GRID); | ||||
|                     if (preferencesBuilder.getColCount() == 1) { | ||||
|                         preferencesBuilder.setColCount(2); | ||||
|                     } | ||||
|                     binding.staggeredOrGridOptions.setVisibility(View.VISIBLE); | ||||
|                     initStaggeredOrGridOptions(); | ||||
|                 } | ||||
|             } | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     private void initColCountToggle() { | ||||
|         binding.colCountToggle.check(getSelectedColCountId()); | ||||
|         binding.colCountToggle.addOnButtonCheckedListener((group, checkedId, isChecked) -> { | ||||
|             if (!isChecked) return; | ||||
|             if (checkedId == R.id.col_count_two) { | ||||
|                 preferencesBuilder.setColCount(2); | ||||
|             } else { | ||||
|                 preferencesBuilder.setColCount(3); | ||||
|             } | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     private void initAvatarsToggle() { | ||||
|         binding.showAvatarToggle.setChecked(preferencesBuilder.isAvatarVisible()); | ||||
|         binding.avatarSizeToggle.check(getSelectedAvatarSizeId()); | ||||
|         binding.showAvatarToggle.setOnCheckedChangeListener((buttonView, isChecked) -> { | ||||
|             preferencesBuilder.setAvatarVisible(isChecked); | ||||
|             binding.labelAvatarSize.setVisibility(isChecked ? View.VISIBLE : View.GONE); | ||||
|             binding.avatarSizeToggle.setVisibility(isChecked ? View.VISIBLE : View.GONE); | ||||
|         }); | ||||
|         binding.labelAvatarSize.setVisibility(preferencesBuilder.isAvatarVisible() ? View.VISIBLE : View.GONE); | ||||
|         binding.avatarSizeToggle.setVisibility(preferencesBuilder.isAvatarVisible() ? View.VISIBLE : View.GONE); | ||||
|         binding.avatarSizeToggle.addOnButtonCheckedListener((group, checkedId, isChecked) -> { | ||||
|             if (!isChecked) return; | ||||
|             if (checkedId == R.id.avatar_size_tiny) { | ||||
|                 preferencesBuilder.setProfilePicSize(PostsLayoutPreferences.ProfilePicSize.TINY); | ||||
|             } else if (checkedId == R.id.avatar_size_small) { | ||||
|                 preferencesBuilder.setProfilePicSize(PostsLayoutPreferences.ProfilePicSize.SMALL); | ||||
|             } else { | ||||
|                 preferencesBuilder.setProfilePicSize(PostsLayoutPreferences.ProfilePicSize.REGULAR); | ||||
|             } | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     private void initNamesToggle() { | ||||
|         binding.showNamesToggle.setChecked(preferencesBuilder.isNameVisible()); | ||||
|         binding.showNamesToggle.setOnCheckedChangeListener((buttonView, isChecked) -> preferencesBuilder.setNameVisible(isChecked)); | ||||
|     } | ||||
| 
 | ||||
|     private void initCornersToggle() { | ||||
|         binding.cornersToggle.check(getSelectedCornersId()); | ||||
|         binding.cornersToggle.addOnButtonCheckedListener((group, checkedId, isChecked) -> { | ||||
|             if (!isChecked) return; | ||||
|             if (checkedId == R.id.corners_round) { | ||||
|                 preferencesBuilder.setHasRoundedCorners(true); | ||||
|                 return; | ||||
|             } | ||||
|             preferencesBuilder.setHasRoundedCorners(false); | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     private void initGapToggle() { | ||||
|         binding.showGapToggle.setChecked(preferencesBuilder.getHasGap()); | ||||
|         binding.showGapToggle.setOnCheckedChangeListener((buttonView, isChecked) -> preferencesBuilder.setHasGap(isChecked)); | ||||
|     } | ||||
| 
 | ||||
|     private int getSelectedLayoutId() { | ||||
|         switch (preferencesBuilder.getType()) { | ||||
|             case STAGGERED_GRID: | ||||
|                 return R.id.layout_staggered; | ||||
|             case LINEAR: | ||||
|                 return R.id.layout_linear; | ||||
|             default: | ||||
|             case GRID: | ||||
|                 return R.id.layout_grid; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private int getSelectedColCountId() { | ||||
|         switch (preferencesBuilder.getColCount()) { | ||||
|             case 2: | ||||
|                 return R.id.col_count_two; | ||||
|             case 3: | ||||
|             default: | ||||
|                 return R.id.col_count_three; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private int getSelectedCornersId() { | ||||
|         if (preferencesBuilder.getHasRoundedCorners()) { | ||||
|             return R.id.corners_round; | ||||
|         } | ||||
|         return R.id.corners_square; | ||||
|     } | ||||
| 
 | ||||
|     private int getSelectedAvatarSizeId() { | ||||
|         switch (preferencesBuilder.getProfilePicSize()) { | ||||
|             case TINY: | ||||
|                 return R.id.avatar_size_tiny; | ||||
|             case SMALL: | ||||
|                 return R.id.avatar_size_small; | ||||
|             case REGULAR: | ||||
|             default: | ||||
|                 return R.id.avatar_size_regular; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public interface OnApplyListener { | ||||
|         void onApply(final PostsLayoutPreferences preferences); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,180 @@ | ||||
| package awais.instagrabber.dialogs | ||||
| 
 | ||||
| import android.app.Dialog | ||||
| import android.content.DialogInterface | ||||
| import android.os.Bundle | ||||
| import android.view.View | ||||
| import android.widget.CompoundButton | ||||
| import androidx.fragment.app.DialogFragment | ||||
| import awais.instagrabber.R | ||||
| import awais.instagrabber.databinding.DialogPostLayoutPreferencesBinding | ||||
| import awais.instagrabber.models.PostsLayoutPreferences | ||||
| import awais.instagrabber.models.PostsLayoutPreferences.PostsLayoutType | ||||
| import awais.instagrabber.models.PostsLayoutPreferences.ProfilePicSize | ||||
| import awais.instagrabber.utils.Utils | ||||
| import com.google.android.material.button.MaterialButtonToggleGroup | ||||
| import com.google.android.material.dialog.MaterialAlertDialogBuilder | ||||
| 
 | ||||
| class PostsLayoutPreferencesDialogFragment( | ||||
|     private val layoutPreferenceKey: String, | ||||
|     private val onApplyListener: OnApplyListener | ||||
| ) : DialogFragment() { | ||||
| 
 | ||||
|     private lateinit var binding: DialogPostLayoutPreferencesBinding | ||||
| 
 | ||||
|     private val preferencesBuilder: PostsLayoutPreferences.Builder | ||||
| 
 | ||||
|     init { | ||||
|         val preferences = PostsLayoutPreferences.fromJson(Utils.settingsHelper.getString(layoutPreferenceKey)) | ||||
|         preferencesBuilder = PostsLayoutPreferences.builder().mergeFrom(preferences) | ||||
|     } | ||||
| 
 | ||||
|     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | ||||
|         binding = DialogPostLayoutPreferencesBinding.inflate(layoutInflater, null, false) | ||||
|         init() | ||||
|         return MaterialAlertDialogBuilder(requireContext()).apply { | ||||
|             setView(binding.root) | ||||
|             setPositiveButton(R.string.apply) { _: DialogInterface?, _: Int -> | ||||
|                 val preferences = preferencesBuilder.build() | ||||
|                 val json = preferences.json | ||||
|                 Utils.settingsHelper.putString(layoutPreferenceKey, json) | ||||
|                 onApplyListener.onApply(preferences) | ||||
|             } | ||||
|         }.create() | ||||
|     } | ||||
| 
 | ||||
|     private fun init() { | ||||
|         initLayoutToggle() | ||||
|         if (preferencesBuilder.type != PostsLayoutType.LINEAR) { | ||||
|             initStaggeredOrGridOptions() | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private fun initStaggeredOrGridOptions() { | ||||
|         initColCountToggle() | ||||
|         initNamesToggle() | ||||
|         initAvatarsToggle() | ||||
|         initCornersToggle() | ||||
|         initGapToggle() | ||||
|     } | ||||
| 
 | ||||
|     private fun initLayoutToggle() { | ||||
|         val selectedLayoutId = selectedLayoutId | ||||
|         binding.layoutToggle.check(selectedLayoutId) | ||||
|         if (selectedLayoutId == R.id.layout_linear) { | ||||
|             binding.staggeredOrGridOptions.visibility = View.GONE | ||||
|         } | ||||
|         binding.layoutToggle.addOnButtonCheckedListener { _: MaterialButtonToggleGroup?, checkedId: Int, isChecked: Boolean -> | ||||
|             if (!isChecked) return@addOnButtonCheckedListener | ||||
|             when (checkedId) { | ||||
|                 R.id.layout_linear -> { | ||||
|                     preferencesBuilder.type = PostsLayoutType.LINEAR | ||||
|                     preferencesBuilder.colCount = 1 | ||||
|                     binding.staggeredOrGridOptions.visibility = View.GONE | ||||
|                 } | ||||
|                 R.id.layout_staggered -> { | ||||
|                     preferencesBuilder.type = PostsLayoutType.STAGGERED_GRID | ||||
|                     if (preferencesBuilder.colCount == 1) { | ||||
|                         preferencesBuilder.colCount = 2 | ||||
|                     } | ||||
|                     binding.staggeredOrGridOptions.visibility = View.VISIBLE | ||||
|                     initStaggeredOrGridOptions() | ||||
|                 } | ||||
|                 else -> { | ||||
|                     preferencesBuilder.type = PostsLayoutType.GRID | ||||
|                     if (preferencesBuilder.colCount == 1) { | ||||
|                         preferencesBuilder.colCount = 2 | ||||
|                     } | ||||
|                     binding.staggeredOrGridOptions.visibility = View.VISIBLE | ||||
|                     initStaggeredOrGridOptions() | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private fun initColCountToggle() { | ||||
|         binding.colCountToggle.check(selectedColCountId) | ||||
|         binding.colCountToggle.addOnButtonCheckedListener { _: MaterialButtonToggleGroup?, checkedId: Int, isChecked: Boolean -> | ||||
|             if (!isChecked) return@addOnButtonCheckedListener | ||||
|             preferencesBuilder.colCount = (if (checkedId == R.id.col_count_two) 2 else 3) | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private fun initAvatarsToggle() { | ||||
|         binding.showAvatarToggle.isChecked = preferencesBuilder.isAvatarVisible | ||||
|         binding.avatarSizeToggle.check(selectedAvatarSizeId) | ||||
|         binding.showAvatarToggle.setOnCheckedChangeListener { _: CompoundButton?, isChecked: Boolean -> | ||||
|             preferencesBuilder.isAvatarVisible = isChecked | ||||
|             binding.labelAvatarSize.visibility = if (isChecked) View.VISIBLE else View.GONE | ||||
|             binding.avatarSizeToggle.visibility = if (isChecked) View.VISIBLE else View.GONE | ||||
|         } | ||||
|         binding.labelAvatarSize.visibility = if (preferencesBuilder.isAvatarVisible) View.VISIBLE else View.GONE | ||||
|         binding.avatarSizeToggle.visibility = if (preferencesBuilder.isAvatarVisible) View.VISIBLE else View.GONE | ||||
|         binding.avatarSizeToggle.addOnButtonCheckedListener { _: MaterialButtonToggleGroup?, checkedId: Int, isChecked: Boolean -> | ||||
|             if (!isChecked) return@addOnButtonCheckedListener | ||||
|             preferencesBuilder.profilePicSize = when (checkedId) { | ||||
|                 R.id.avatar_size_tiny -> ProfilePicSize.TINY | ||||
|                 R.id.avatar_size_small -> ProfilePicSize.SMALL | ||||
|                 else -> ProfilePicSize.REGULAR | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private fun initNamesToggle() { | ||||
|         binding.showNamesToggle.isChecked = preferencesBuilder.isNameVisible | ||||
|         binding.showNamesToggle.setOnCheckedChangeListener { _: CompoundButton?, isChecked: Boolean -> | ||||
|             preferencesBuilder.isNameVisible = isChecked | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private fun initCornersToggle() { | ||||
|         binding.cornersToggle.check(selectedCornersId) | ||||
|         binding.cornersToggle.addOnButtonCheckedListener { _: MaterialButtonToggleGroup?, checkedId: Int, isChecked: Boolean -> | ||||
|             if (!isChecked) return@addOnButtonCheckedListener | ||||
|             if (checkedId == R.id.corners_round) { | ||||
|                 preferencesBuilder.hasRoundedCorners = true | ||||
|                 return@addOnButtonCheckedListener | ||||
|             } | ||||
|             preferencesBuilder.hasRoundedCorners = false | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private fun initGapToggle() { | ||||
|         binding.showGapToggle.isChecked = preferencesBuilder.hasGap | ||||
|         binding.showGapToggle.setOnCheckedChangeListener { _: CompoundButton?, isChecked: Boolean -> | ||||
|             preferencesBuilder.hasGap = isChecked | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private val selectedLayoutId: Int | ||||
|         get() = when (preferencesBuilder.type) { | ||||
|             PostsLayoutType.STAGGERED_GRID -> R.id.layout_staggered | ||||
|             PostsLayoutType.LINEAR -> R.id.layout_linear | ||||
|             PostsLayoutType.GRID -> R.id.layout_grid | ||||
|             else -> R.id.layout_grid | ||||
|         } | ||||
| 
 | ||||
|     private val selectedColCountId: Int | ||||
|         get() = when (preferencesBuilder.colCount) { | ||||
|             2 -> R.id.col_count_two | ||||
|             3 -> R.id.col_count_three | ||||
|             else -> R.id.col_count_three | ||||
|         } | ||||
| 
 | ||||
|     private val selectedCornersId: Int | ||||
|         get() = if (preferencesBuilder.hasRoundedCorners) { | ||||
|             R.id.corners_round | ||||
|         } else R.id.corners_square | ||||
| 
 | ||||
|     private val selectedAvatarSizeId: Int | ||||
|         get() = when (preferencesBuilder.profilePicSize) { | ||||
|             ProfilePicSize.TINY -> R.id.avatar_size_tiny | ||||
|             ProfilePicSize.SMALL -> R.id.avatar_size_small | ||||
|             ProfilePicSize.REGULAR -> R.id.avatar_size_regular | ||||
|             else -> R.id.avatar_size_regular | ||||
|         } | ||||
| 
 | ||||
|     fun interface OnApplyListener { | ||||
|         fun onApply(preferences: PostsLayoutPreferences) | ||||
|     } | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user