@@ -23,12 +23,13 @@ import com.tans.tfiletransporter.file.isRootFileTree
2323import com.tans.tfiletransporter.file.newLocalSubTree
2424import com.tans.tfiletransporter.ui.DataBindingAdapter
2525import com.tans.tfiletransporter.ui.activity.BaseActivity
26+ import com.tans.tfiletransporter.ui.activity.commomdialog.TextInputDialog
2627import com.tans.tfiletransporter.ui.activity.commomdialog.loadingDialog
2728import com.tans.tfiletransporter.ui.activity.commomdialog.showNoOptionalDialog
29+ import com.tans.tfiletransporter.ui.activity.commomdialog.showTextInputDialog
2830import com.tans.tfiletransporter.utils.dp2px
2931import com.tans.tfiletransporter.utils.firstVisibleItemPosition
3032import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
31- import io.reactivex.rxjava3.core.Single
3233import io.reactivex.rxjava3.kotlin.withLatestFrom
3334import io.reactivex.rxjava3.schedulers.Schedulers
3435import kotlinx.coroutines.Dispatchers
@@ -90,8 +91,42 @@ class FolderSelectActivity : BaseActivity<FolderSelectActivityBinding, FolderSel
9091
9192 binding.toolBar.menu.findItem(R .id.create_new_folder).clicks()
9293 .flatMapSingle {
93- // TODO: create new folder.
94- Single .just(Unit )
94+ rxSingle(Dispatchers .Main ) {
95+ val inputResult = this @FolderSelectActivity.showTextInputDialog(getString(R .string.folder_select_create_new_folder_hint))
96+ if (inputResult is TextInputDialog .Companion .Result .Success ) {
97+ val tree = bindState().firstOrError().await().fileTree
98+ if (tree.isRootFileTree() || ! Settings .isDirWriteable(tree.path)) {
99+ this @FolderSelectActivity.showNoOptionalDialog(
100+ title = getString(R .string.folder_select_create_folder_error),
101+ message = getString(R .string.folder_select_error_body, " Can't create new folder." )
102+ ).await()
103+ } else {
104+ val createFolderResult = withContext(Dispatchers .IO ) {
105+ try {
106+ val f = File (tree.path, inputResult.text)
107+ f.mkdirs()
108+ } catch (e: Throwable ) {
109+ e.printStackTrace()
110+ false
111+ }
112+ }
113+ if (createFolderResult) {
114+ updateState { oldState ->
115+ val oldTree = oldState.fileTree
116+ val parentTree = oldTree.parentTree
117+ val dirLeaf = parentTree?.dirLeafs?.find { it.path == oldTree.path }
118+ if (parentTree != null && dirLeaf != null ) {
119+ oldState.copy(
120+ fileTree = parentTree.newLocalSubTree(dirLeaf)
121+ )
122+ } else {
123+ oldState
124+ }
125+ }.await()
126+ }
127+ }
128+ }
129+ }
95130 }
96131 .bindLife()
97132
@@ -172,7 +207,7 @@ class FolderSelectActivity : BaseActivity<FolderSelectActivityBinding, FolderSel
172207 if (tree.isRootFileTree()) {
173208 this @FolderSelectActivity.showNoOptionalDialog(
174209 title = getString(R .string.folder_select_error_title),
175- message = getString(R .string.folder_select_error_body, " Root fold can't be selected." )
210+ message = getString(R .string.folder_select_error_body, " Root folder can't be selected." )
176211 ).await()
177212 } else {
178213 if (withContext(Dispatchers .IO ) { Settings .isDirWriteable(tree.path) }) {
0 commit comments