Skip to content

Commit f7961cf

Browse files
committed
Remember all torrent adding parameters, not just download directory
1 parent 1112c90 commit f7961cf

File tree

11 files changed

+138
-52
lines changed

11 files changed

+138
-52
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
### Changed
99
- RPC client is rewritten to use OkHttp library instead of Qt
10+
- All torrent adding parameters are now remembered together with download directory
1011

1112
## [2.9.1] - 2023-03-26
1213
### Fixed

app/src/main/kotlin/org/equeim/tremotesf/ui/Settings.kt

Lines changed: 93 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import kotlinx.coroutines.sync.withLock
2727
import kotlinx.coroutines.withContext
2828
import org.equeim.tremotesf.R
2929
import org.equeim.tremotesf.TremotesfApplication
30+
import org.equeim.tremotesf.torrentfile.rpc.requests.torrentproperties.TorrentLimits
3031
import org.equeim.tremotesf.ui.Settings.Property
3132
import org.equeim.tremotesf.ui.torrentslistfragment.TorrentsListFragmentViewModel
3233
import timber.log.Timber
@@ -106,31 +107,58 @@ object Settings {
106107
putString(colorTheme.key, newValue)
107108
}
108109
}
110+
context.getString(R.string.deprecated_prefs_remember_download_directory_key)
111+
.let { deprecatedRememberDownloadDirectoryKey ->
112+
if (preferences.contains(deprecatedRememberDownloadDirectoryKey)) {
113+
preferences.edit {
114+
putBoolean(
115+
rememberAddTorrentParameters.key,
116+
preferences.getBoolean(deprecatedRememberDownloadDirectoryKey, false)
117+
)
118+
remove(deprecatedRememberDownloadDirectoryKey)
119+
}
120+
}
121+
}
109122
migrated = true
110123
}
111124
}
112125
}
113126

127+
private class EnumPrefsMapper<T : Enum<T>>(
128+
private val enumClass: Class<T>,
129+
@StringRes private val keyResId: Int,
130+
@StringRes private val defaultValueResId: Int,
131+
val enumToPrefsValue: (T) -> String,
132+
) {
133+
private val enumValues = requireNotNull(enumClass.enumConstants)
134+
135+
fun prefsValueToEnum(prefsValue: String): T {
136+
enumValues.find { enumToPrefsValue(it) == prefsValue }?.let { return it }
137+
val key = context.getString(keyResId)
138+
Timber.e("Unknown prefs value $prefsValue for key $key and enum $enumClass")
139+
val defaultPrefsValue = context.getString(defaultValueResId)
140+
return enumValues.find { enumToPrefsValue(it) == defaultPrefsValue }
141+
?: throw IllegalStateException("Did not find value of enum $enumClass for default prefs value $defaultPrefsValue and key $key")
142+
}
143+
}
144+
145+
private inline fun <reified T : Enum<T>> EnumPrefsMapper(
146+
@StringRes keyResId: Int,
147+
@StringRes defaultValueResId: Int,
148+
noinline enumToPrefsValue: (T) -> String,
149+
): EnumPrefsMapper<T> =
150+
EnumPrefsMapper(T::class.java, keyResId, defaultValueResId, enumToPrefsValue)
151+
114152
private interface MappedPrefsEnum {
115153
val prefsValue: String
116-
117-
companion object {
118-
inline fun <reified T> fromPrefsValueProvider(
119-
@StringRes keyResId: Int,
120-
@StringRes defaultValueResId: Int,
121-
): (String) -> T where T : MappedPrefsEnum, T : Enum<T> {
122-
val values = enumValues<T>()
123-
return { prefsValue ->
124-
values.find { it.prefsValue == prefsValue } ?: run {
125-
Timber.e("Unknown value $prefsValue for key ${context.getString(keyResId)}")
126-
val defaultPrefsValue = context.getString(defaultValueResId)
127-
values.single { it.prefsValue == defaultPrefsValue }
128-
}
129-
}
130-
}
131-
}
132154
}
133155

156+
private inline fun <reified T> EnumPrefsMapper(
157+
@StringRes keyResId: Int,
158+
@StringRes defaultValueResId: Int,
159+
): EnumPrefsMapper<T> where T : MappedPrefsEnum, T : Enum<T> =
160+
EnumPrefsMapper(T::class.java, keyResId, defaultValueResId, MappedPrefsEnum::prefsValue)
161+
134162
enum class ColorTheme(
135163
@StringRes prefsValueResId: Int,
136164
@StyleRes val activityThemeResId: Int = 0,
@@ -140,20 +168,15 @@ object Settings {
140168
Teal(R.string.prefs_color_theme_value_teal, R.style.AppTheme_Teal);
141169

142170
override val prefsValue = context.getString(prefsValueResId)
143-
144-
companion object {
145-
val fromPrefsValue = MappedPrefsEnum.fromPrefsValueProvider<ColorTheme>(
146-
R.string.prefs_color_theme_key,
147-
R.string.prefs_color_theme_default_value
148-
)
149-
}
150171
}
151172

173+
private val colorThemeMapper =
174+
EnumPrefsMapper<ColorTheme>(R.string.prefs_color_theme_key, R.string.prefs_color_theme_default_value)
152175
val colorTheme: MutableProperty<ColorTheme> = mutableProperty<String>(
153176
R.string.prefs_color_theme_key,
154177
R.string.prefs_color_theme_default_value
155178
).map(
156-
transformGetter = ColorTheme.fromPrefsValue,
179+
transformGetter = colorThemeMapper::prefsValueToEnum,
157180
transformSetter = { it.prefsValue }
158181
)
159182

@@ -171,20 +194,17 @@ object Settings {
171194
Off(R.string.prefs_dark_theme_mode_value_off, AppCompatDelegate.MODE_NIGHT_NO);
172195

173196
override val prefsValue = context.getString(prefsValueResId)
174-
175-
companion object {
176-
val fromPrefsValue = MappedPrefsEnum.fromPrefsValueProvider<DarkThemeMode>(
177-
R.string.prefs_dark_theme_mode_key,
178-
R.string.prefs_dark_theme_mode_default_value
179-
)
180-
}
181197
}
182198

199+
private val darkThemeModeMapper = EnumPrefsMapper<DarkThemeMode>(
200+
R.string.prefs_dark_theme_mode_key,
201+
R.string.prefs_dark_theme_mode_default_value
202+
)
183203
val darkThemeMode: Property<DarkThemeMode> =
184204
property<String>(
185205
R.string.prefs_dark_theme_mode_key,
186206
R.string.prefs_dark_theme_mode_default_value
187-
).map(DarkThemeMode.fromPrefsValue)
207+
).map(darkThemeModeMapper::prefsValueToEnum)
188208

189209
val torrentCompactView: Property<Boolean> = property(
190210
R.string.prefs_torrent_compact_view_key,
@@ -247,8 +267,47 @@ object Settings {
247267
val fillTorrentLinkFromKeyboard: Property<Boolean> =
248268
property(R.string.prefs_link_from_clipboard_key, R.bool.prefs_link_from_clipboard_default_value)
249269

250-
val rememberDownloadDirectory: Property<Boolean> =
251-
property(R.string.prefs_remember_download_directory_key, R.bool.prefs_remember_download_directory_default_value)
270+
val rememberAddTorrentParameters: Property<Boolean> =
271+
property(
272+
R.string.prefs_remember_add_torrent_parameters_key,
273+
R.bool.prefs_remember_add_torrent_parameters_default_value
274+
)
275+
276+
enum class StartTorrentAfterAdding(override val prefsValue: String) : MappedPrefsEnum {
277+
Start("start"),
278+
DontStart("dont_start"),
279+
Unknown("unknown")
280+
}
281+
282+
private val startTorrentAfterAddingMapper = EnumPrefsMapper<StartTorrentAfterAdding>(
283+
R.string.prefs_last_add_torrent_start_after_adding_key,
284+
R.string.prefs_last_add_torrent_start_after_adding_default_value
285+
)
286+
val lastAddTorrentStartAfterAdding: MutableProperty<StartTorrentAfterAdding> = mutableProperty<String>(
287+
R.string.prefs_last_add_torrent_start_after_adding_key,
288+
R.string.prefs_last_add_torrent_start_after_adding_default_value
289+
).map(
290+
transformGetter = startTorrentAfterAddingMapper::prefsValueToEnum,
291+
transformSetter = startTorrentAfterAddingMapper.enumToPrefsValue
292+
)
293+
294+
private val bandwidthPriorityMapper = EnumPrefsMapper<TorrentLimits.BandwidthPriority>(
295+
R.string.prefs_last_add_torrent_priority_key,
296+
R.string.prefs_last_add_torrent_priority_default_value
297+
) {
298+
when (it) {
299+
TorrentLimits.BandwidthPriority.Low -> "low"
300+
TorrentLimits.BandwidthPriority.Normal -> "normal"
301+
TorrentLimits.BandwidthPriority.High -> "high"
302+
}
303+
}
304+
val lastAddTorrentPriority: MutableProperty<TorrentLimits.BandwidthPriority> = mutableProperty<String>(
305+
R.string.prefs_last_add_torrent_priority_key,
306+
R.string.prefs_last_add_torrent_priority_default_value
307+
).map(
308+
transformGetter = bandwidthPriorityMapper::prefsValueToEnum,
309+
transformSetter = bandwidthPriorityMapper.enumToPrefsValue
310+
)
252311

253312
val torrentsSortMode: MutableProperty<TorrentsListFragmentViewModel.SortMode> =
254313
mutableProperty<Int>(

app/src/main/kotlin/org/equeim/tremotesf/ui/addtorrent/AddTorrentFileFragment.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class AddTorrentFileFragment : AddTorrentFragment(
7070
0
7171
) {
7272
private val args: AddTorrentFileFragmentArgs by navArgs()
73-
val model: AddTorrentFileModel by viewModels<AddTorrentFileModelImpl> {
73+
val model: AddTorrentFileModelImpl by viewModels {
7474
viewModelFactory {
7575
initializer {
7676
AddTorrentFileModelImpl(
@@ -323,15 +323,14 @@ class AddTorrentFileFragment : AddTorrentFragment(
323323
if (model.shouldSetInitialRpcInputs) {
324324
val downloadingSettings = it.downloadingSettings.response
325325
downloadDirectoryLayout.downloadDirectoryEdit.setText(
326-
model.getInitialDownloadDirectory(
327-
downloadingSettings
328-
)
326+
model.getInitialDownloadDirectory(downloadingSettings)
329327
)
330-
startDownloadingCheckBox.isChecked = downloadingSettings.startAddedTorrents
328+
startDownloadingCheckBox.isChecked = model.getInitialStartAfterAdding(downloadingSettings)
331329
model.shouldSetInitialRpcInputs = false
332330
}
333331
if (model.shouldSetInitialLocalInputs) {
334-
priorityView.setText(R.string.normal_priority)
332+
val parent = requireParentFragment() as AddTorrentFileFragment
333+
priorityView.setText(parent.priorityItems[parent.priorityItemEnums.indexOf(model.getInitialPriority())])
335334
model.shouldSetInitialLocalInputs = false
336335
}
337336
}

app/src/main/kotlin/org/equeim/tremotesf/ui/addtorrent/AddTorrentLinkFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,12 @@ class AddTorrentLinkFragment : AddTorrentFragment(
203203
private suspend fun showView(downloadingSettings: DownloadingServerSettings) = with(binding) {
204204
if (model.shouldSetInitialRpcInputs) {
205205
downloadDirectoryLayout.downloadDirectoryEdit.setText(model.getInitialDownloadDirectory(downloadingSettings))
206-
startDownloadingCheckBox.isChecked = downloadingSettings.startAddedTorrents
206+
startDownloadingCheckBox.isChecked = model.getInitialStartAfterAdding(downloadingSettings)
207207
model.shouldSetInitialRpcInputs = false
208208
}
209209
if (model.shouldSetInitialLocalInputs) {
210210
torrentLinkEdit.setText(model.getInitialTorrentLink())
211-
priorityView.setText(R.string.normal_priority)
211+
priorityView.setText(priorityItems[priorityItemEnums.indexOf(model.getInitialPriority())])
212212
model.shouldSetInitialLocalInputs = false
213213
}
214214
scrollView.isVisible = true

app/src/main/kotlin/org/equeim/tremotesf/ui/addtorrent/BaseAddTorrentModel.kt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import org.equeim.tremotesf.torrentfile.rpc.requests.FileSize
1919
import org.equeim.tremotesf.torrentfile.rpc.requests.getFreeSpaceInDirectory
2020
import org.equeim.tremotesf.torrentfile.rpc.requests.serversettings.DownloadingServerSettings
2121
import org.equeim.tremotesf.torrentfile.rpc.requests.serversettings.getDownloadingServerSettings
22+
import org.equeim.tremotesf.torrentfile.rpc.requests.torrentproperties.TorrentLimits
2223
import org.equeim.tremotesf.torrentfile.rpc.stateIn
2324
import org.equeim.tremotesf.torrentfile.rpc.toNativeSeparators
2425
import org.equeim.tremotesf.ui.Settings
@@ -34,7 +35,7 @@ abstract class BaseAddTorrentModel(application: Application) : AndroidViewModel(
3435
.stateIn(GlobalRpcClient, viewModelScope)
3536

3637
suspend fun getInitialDownloadDirectory(settings: DownloadingServerSettings): String {
37-
return if (Settings.rememberDownloadDirectory.get()) {
38+
return if (Settings.rememberAddTorrentParameters.get()) {
3839
GlobalServers.serversState.value.currentServer
3940
?.lastDownloadDirectory
4041
?.takeIf { it.isNotEmpty() }
@@ -45,6 +46,24 @@ abstract class BaseAddTorrentModel(application: Application) : AndroidViewModel(
4546
}.toNativeSeparators()
4647
}
4748

49+
suspend fun getInitialStartAfterAdding(settings: DownloadingServerSettings): Boolean =
50+
if (Settings.rememberAddTorrentParameters.get()) {
51+
when (Settings.lastAddTorrentStartAfterAdding.get()) {
52+
Settings.StartTorrentAfterAdding.Start -> true
53+
Settings.StartTorrentAfterAdding.DontStart -> false
54+
Settings.StartTorrentAfterAdding.Unknown -> settings.startAddedTorrents
55+
}
56+
} else {
57+
settings.startAddedTorrents
58+
}
59+
60+
suspend fun getInitialPriority(): TorrentLimits.BandwidthPriority =
61+
if (Settings.rememberAddTorrentParameters.get()) {
62+
Settings.lastAddTorrentPriority.get()
63+
} else {
64+
TorrentLimits.BandwidthPriority.Normal
65+
}
66+
4867
suspend fun getFreeSpace(directory: String): FileSize? = try {
4968
GlobalRpcClient.getFreeSpaceInDirectory(directory)
5069
} catch (e: RpcRequestError) {

app/src/main/res/values-de-rDE/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
416416

417417
<string name="prefs_delete_files_title">Lösche Dateien auf der Festplatte, wenn der Torrent gelöscht wird</string>
418418
<string name="prefs_link_from_clipboard_title">Beim Hinzufügen eines Torrent-Links automatisch den Link aus der Zwischenablage ausfüllen</string>
419-
<string name="prefs_remember_download_directory_title">Letztes Download-Verzeichnis merken</string>
419+
<string name="prefs_remember_add_torrent_parameters_title">Letztes Download-Verzeichnis merken</string>
420420

421421
<string name="alternative_speed_limits">Alternative Geschwindigkeitslimitierungen</string>
422422

app/src/main/res/values-fr/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
419419

420420
<string name="prefs_delete_files_title">Supprimer les fichiers du disque lors de la suppression du torrent</string>
421421
<string name="prefs_link_from_clipboard_title">Remplir automatiquement le lien depuis le presse-papiers lors de l\'ajout d\'un lien torrent</string>
422-
<string name="prefs_remember_download_directory_title">Se souvenir du dernier répertoire de téléchargement</string>
422+
<string name="prefs_remember_add_torrent_parameters_title">Se souvenir du dernier répertoire de téléchargement</string>
423423

424424
<string name="alternative_speed_limits">Limites de vitesse alternatives</string>
425425

app/src/main/res/values-ru/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
446446

447447
<string name="prefs_delete_files_title">Удалять файлы на жестком диске при удалении торрента</string>
448448
<string name="prefs_link_from_clipboard_title">Автоматически копировать ссылку из буфера обмена при добавлении ссылки на торрент</string>
449-
<string name="prefs_remember_download_directory_title">Запоминать последний каталог загрузки</string>
449+
<string name="prefs_remember_add_torrent_parameters_title">Запоминать последний каталог загрузки</string>
450450

451451
<string name="alternative_speed_limits">Альтернативные ограничения скорости</string>
452452

app/src/main/res/values/prefs_constants.xml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ SPDX-License-Identifier: GPL-3.0-or-later
9595

9696
<string name="notification_permission_key" translatable="false">notificationPermission</string>
9797

98-
<string name="prefs_remember_download_directory_key" translatable="false">rememberDownloadDirectory</string>
99-
<bool name="prefs_remember_download_directory_default_value">true</bool>
98+
<string name="deprecated_prefs_remember_download_directory_key" translatable="false">rememberDownloadDirectory</string>
99+
100+
<string name="prefs_remember_add_torrent_parameters_key">rememberAddTorrentParameters</string>
101+
<bool name="prefs_remember_add_torrent_parameters_default_value">true</bool>
102+
103+
<string name="prefs_last_add_torrent_start_after_adding_key">lastAddTorrentStartAfterAdding</string>
104+
<string name="prefs_last_add_torrent_start_after_adding_default_value">unknown</string>
105+
106+
<string name="prefs_last_add_torrent_priority_key">lastAddTorrentPriority</string>
107+
<string name="prefs_last_add_torrent_priority_default_value">normal</string>
100108
</resources>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
474474

475475
<string name="prefs_delete_files_title">Delete files on the hard disk when removing torrent</string>
476476
<string name="prefs_link_from_clipboard_title">Automatically fill link from clipboard when adding torrent link</string>
477-
<string name="prefs_remember_download_directory_title">Remember last download directory</string>
477+
<string name="prefs_remember_add_torrent_parameters_title">Remember parameters of last added torrent</string>
478478

479479
<string name="alternative_speed_limits">Alternative speed limits</string>
480480

0 commit comments

Comments
 (0)