ITADN

Fix recommended for the PlayerHelper.kt file

#8483OpenLycaon-Project 创建于 2026-05-28
enhancement
L
Lycaon-Projectcommented
### Describe your suggested feature Hello, I'm creating this issue to help improve the playerhelper.kt file by reducing the number of warnings and to share my fix, which I've tested using my debugger. Here are the changes: Merged `edit(commit = true)` into `editNow()` to avoid duplication. Replaced `.forEach { ... }.map { ... }` with more readable and idiomatic structures. Removal of duplicate calls to getString, getLong, etc. getIgnorableNotificationChannels() filters out empty strings to prevent bugs. Use of data classes for migrations for greater clarity. Helper functions for rounding (roundToNearestQuarter) directly attached to Float. ### Other details Call centralization edit(commit = true) before : fun putString(key: String, value: String) { settings.edit(commit = true) { putString(key, value) } } fun putBoolean(key: String, value: Boolean) { settings.edit(commit = true) { putBoolean(key, value) } } // idem pour putInt, putLong, putStringSet, remove after : private inline fun SharedPreferences.editNow(action: SharedPreferences.Editor.() -> Unit) = edit(commit = true, action = action) fun putString(key: String, value: String) = settings.editNow { putString(key, value) } fun putBoolean(key: String, value: Boolean) = settings.editNow { putBoolean(key, value) } // idem pour putInt, putLong, putStringSet, remove Simplifying migration before : PreferenceMigration(5, 6) { val currentSpeed = (settings.getString(PreferenceKeys.PLAYBACK_SPEED, null) ?: return@PreferenceMigration).replace("F", "").toFloat() // round to the nearest .25 playback speed val speed = (currentSpeed * 4f).roundToInt() / 4f putString(PreferenceKeys.PLAYBACK_SPEED, speed.toString()) } after : PreferenceMigration(5, 6) { settings.getString(PreferenceKeys.PLAYBACK_SPEED, null)?.let { speedStr -> val speed = speedStr.replace("F", "").toFloat().roundToNearestQuarter() putString(PreferenceKeys.PLAYBACK_SPEED, speed.toString()) } } // Helper ajouté private fun Float.roundToNearestQuarter() = (this * 4).roundToInt() / 4f Advantage: more readable, avoids unnecessary return@PreferenceMigration, encapsulated rounding. Optimizing Ignorable Notifications before : fun getIgnorableNotificationChannels(): List<String> { return getString(PreferenceKeys.IGNORED_NOTIFICATION_CHANNELS, "").split(",") } fun isChannelNotificationIgnorable(channelId: String): Boolean { return getIgnorableNotificationChannels().any { it == channelId } } fun toggleIgnorableNotificationChannel(channelId: String) { val ignorableChannels = getIgnorableNotificationChannels().toMutableList() if (ignorableChannels.contains(channelId)) { ignorableChannels.remove(channelId) } else { ignorableChannels.add(channelId) } settings.edit { val channelsString = ignorableChannels.joinToString(",") putString(PreferenceKeys.IGNORED_NOTIFICATION_CHANNELS, channelsString) } } after : fun getIgnorableNotificationChannels() = getString(PreferenceKeys.IGNORED_NOTIFICATION_CHANNELS, "").split(",").filter { it.isNotEmpty() } fun isChannelNotificationIgnorable(channelId: String) = getIgnorableNotificationChannels().contains(channelId) fun toggleIgnorableNotificationChannel(channelId: String) { val channels = getIgnorableNotificationChannels().toMutableSet() if (!channels.add(channelId)) channels.remove(channelId) putString(PreferenceKeys.IGNORED_NOTIFICATION_CHANNELS, channels.joinToString(",")) } Advantage: Filters out empty strings. Uses a MutableSet to prevent duplicates and simplify adding and removing elements. Generating the SponsorBlock UserID before : var uuid = getString(PreferenceKeys.SB_USER_ID, "") if (uuid.isEmpty()) { uuid = (0 until 30).map { USER_ID_CHARS.random() }.joinToString("") putString(PreferenceKeys.SB_USER_ID, uuid) } return uuid after : var uuid = getString(PreferenceKeys.SB_USER_ID, "") if (uuid.isEmpty()) { uuid = (1..30).map { USER_ID_CHARS.random() }.joinToString("") putString(PreferenceKeys.SB_USER_ID, uuid) } return uuid Advantage: more natural (1..30 instead of 0 until 30), same result. Simplifying the migrate() function before : var currentPrefVersion = getInt(PreferenceKeys.PREFERENCE_VERSION, 0) while (currentPrefVersion < MIGRATIONS.count()) { val next = currentPrefVersion + 1 val migration = MIGRATIONS.find { it.fromVersion == currentPrefVersion && it.toVersion == next } Log.i(TAG, "Performing migration from $currentPrefVersion to $next") migration?.onMigration?.invoke() currentPrefVersion++ putInt(PreferenceKeys.PREFERENCE_VERSION, currentPrefVersion) } after : var version = getInt(PreferenceKeys.PREFERENCE_VERSION, 0) while (version < MIGRATIONS.size) { MIGRATIONS.find { it.fromVersion == version }?.onMigration?.invoke() version++ putInt(PreferenceKeys.PREFERENCE_VERSION, version) Log.i(TAG, "Migrated preferences to version $version") } Advantage: more readable, “next” removed, `count()` replaced with `size`. Reduces the number of Android Studio warnings from 6 to 2 in the repository before making changes, based on the suggestions Overall benefits of the changes: Replaced `forEach { ... }.map { ... }` with plain `forEach` where `map` was unnecessary. Added internal utility functions to reduce repetitive code (`roundToNearestQuarter`, `editNow`). Removed unnecessary `!!` operators where `getString(..., “”)` already guarantees a `String`. ### Acknowledgements - [x] I have searched the existing issues and this is a new ticket, **NOT** a duplicate or related to another open issue. - [x] I have written a short but informative title. - [x] I will fill out all of the requested information in this form.
0 条评论