@@ -10,6 +10,7 @@ import io.reactivex.rxjava3.subjects.Subject
1010import kotlinx.coroutines.Dispatchers
1111import kotlinx.coroutines.asExecutor
1212import java.io.File
13+ import java.io.IOException
1314import java.util.concurrent.Executor
1415import java.util.concurrent.atomic.AtomicReference
1516
@@ -27,11 +28,11 @@ object Settings : Stateable<Settings.SettingsData> {
2728 AtomicReference (null )
2829 }
2930
30- private val defaultDownloadDir by lazy {
31+ val defaultDownloadDir: String by lazy {
3132 File (Environment .getExternalStoragePublicDirectory(Environment .DIRECTORY_DOWNLOADS ), " tFileTransfer" ).canonicalPath
3233 }
3334
34- private const val defaultConnectionSize = 4
35+ private const val defaultConnectionSize = 6
3536
3637
3738 const val minConnectionSize = 1
@@ -41,8 +42,19 @@ object Settings : Stateable<Settings.SettingsData> {
4142 val sp = context.getSharedPreferences(SP_FILE_NAME , Context .MODE_PRIVATE )
4243 this .sp.set(sp)
4344 val s = updateState {
45+ val storedDownloadDir = sp.getString(DOWNLOAD_DIR_KEY , defaultDownloadDir)?.let {
46+ if (isDirWriteable(it)) {
47+ it
48+ } else {
49+ sp.edit().let { et ->
50+ et.putString(DOWNLOAD_DIR_KEY , defaultDownloadDir)
51+ et.apply ()
52+ }
53+ defaultDownloadDir
54+ }
55+ }
4456 SettingsData (
45- downloadDir = sp.getString( DOWNLOAD_DIR_KEY , defaultDownloadDir) ? : defaultDownloadDir,
57+ downloadDir = storedDownloadDir ? : defaultDownloadDir,
4658 shareMyDir = sp.getBoolean(SHARE_MY_DIR_KEY , true ),
4759 transferFileMaxConnection = sp.getInt(MAX_CONNECTION_KEY , defaultConnectionSize),
4860 )
@@ -59,29 +71,39 @@ object Settings : Stateable<Settings.SettingsData> {
5971 fun transferFileMaxConnection (): Single <Int > = stateStore.firstOrError()
6072 .map { fixTransferFileConnectionSize(it.transferFileMaxConnection) }
6173
62- fun updateDownloadDir (dir : String ) = updateState { s ->
63- sp.get()?.edit()?.let {
64- it.putString(DOWNLOAD_DIR_KEY , dir)
65- it.apply ()
74+ fun updateDownloadDir (dir : String ): Single <Unit > {
75+ return if (isDirWriteable(dir)) {
76+ updateState { s ->
77+ if (dir != s.downloadDir) {
78+ sp.get()?.edit()?.let {
79+ it.putString(DOWNLOAD_DIR_KEY , dir)
80+ it.apply ()
81+ }
82+ s.copy(downloadDir = dir)
83+ } else {
84+ s
85+ }
86+ }.map { }
87+ } else {
88+ Single .error(IOException (" $dir is not writeable dir." ))
6689 }
67- s.copy(downloadDir = dir)
6890 }
6991
70- fun updateShareDir (shareDir : Boolean ) = updateState { s ->
92+ fun updateShareDir (shareDir : Boolean ): Single < Unit > = updateState { s ->
7193 sp.get()?.edit()?.let {
7294 it.putBoolean(SHARE_MY_DIR_KEY , shareDir)
7395 it.apply ()
7496 }
7597 s.copy(shareMyDir = shareDir)
76- }
98+ }.map { }
7799
78- fun updateTransferFileMaxConnection (maxConnection : Int ) = updateState { s ->
100+ fun updateTransferFileMaxConnection (maxConnection : Int ): Single < Unit > = updateState { s ->
79101 sp.get()?.edit()?.let {
80102 it.putInt(MAX_CONNECTION_KEY , maxConnection)
81103 it.apply ()
82104 }
83105 s.copy(transferFileMaxConnection = maxConnection)
84- }
106+ }.map { }
85107
86108 fun fixTransferFileConnectionSize (needToFix : Int ): Int {
87109 return if (needToFix in minConnectionSize .. maxConnectionSize) {
@@ -91,6 +113,11 @@ object Settings : Stateable<Settings.SettingsData> {
91113 }
92114 }
93115
116+ fun isDirWriteable (dir : String ): Boolean {
117+ val f = File (dir)
118+ return f.isDirectory && f.canWrite()
119+ }
120+
94121 data class SettingsData (
95122 val downloadDir : String = defaultDownloadDir,
96123 val shareMyDir : Boolean = true ,
0 commit comments