package club.clubk.ktag.apps.sharedservices

import android.os.Bundle
import androidx.annotation.XmlRes
import androidx.preference.PreferenceFragmentCompat

/**
 * Base fragment for displaying preferences from an XML resource.
 */
open class BaseSettingsFragment : PreferenceFragmentCompat() {

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        val preferencesResId = arguments?.getInt(ARG_PREFERENCES_RES_ID, 0) ?: 0
        if (preferencesResId != 0) {
            setPreferencesFromResource(preferencesResId, rootKey)
        }
    }

    companion object {
        private const val ARG_PREFERENCES_RES_ID = "preferences_res_id"

        fun newInstance(@XmlRes preferencesResId: Int): BaseSettingsFragment {
            return BaseSettingsFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_PREFERENCES_RES_ID, preferencesResId)
                }
            }
        }
    }
}
