11package com.simplemobiletools.filemanager.dialogs
22
3- import android.app.Activity
43import android.support.v7.app.AlertDialog
54import android.view.View
65import android.view.WindowManager
76import com.simplemobiletools.commons.extensions.*
87import com.simplemobiletools.filemanager.R
8+ import com.simplemobiletools.filemanager.activities.SimpleActivity
99import kotlinx.android.synthetic.main.dialog_create_new.view.*
1010import java.io.File
1111import java.io.IOException
1212
13- class CreateNewItemDialog (val activity : Activity , val path : String , val callback : () -> Unit ) {
13+ class CreateNewItemDialog (val activity : SimpleActivity , val path : String , val callback : () -> Unit ) {
1414 init {
1515 val view = activity.layoutInflater.inflate(R .layout.dialog_create_new, null )
1616
@@ -32,12 +32,16 @@ class CreateNewItemDialog(val activity: Activity, val path: String, val callback
3232 }
3333
3434 if (view.dialog_radio_group.checkedRadioButtonId == R .id.dialog_radio_directory) {
35- if (! createDirectory(file, this )) {
36- errorOccurred()
35+ createDirectory(file, this ) {
36+ if (! it) {
37+ errorOccurred()
38+ }
3739 }
3840 } else {
39- if (! createFile(file, this )) {
40- errorOccurred()
41+ createFile(file, this ) {
42+ if (! it) {
43+ errorOccurred()
44+ }
4145 }
4246 }
4347 } else {
@@ -47,39 +51,47 @@ class CreateNewItemDialog(val activity: Activity, val path: String, val callback
4751 }
4852 }
4953
50- private fun createDirectory (file : File , alertDialog : AlertDialog ): Boolean {
51- return if (activity.needsStupidWritePermissions(path)) {
52- val documentFile = activity.getFileDocument(file.absolutePath) ? : return false
53- documentFile.createDirectory(file.name)
54- success(alertDialog)
55- true
54+ private fun createDirectory (file : File , alertDialog : AlertDialog , callback : (Boolean ) -> Unit ) {
55+ if (activity.needsStupidWritePermissions(path)) {
56+ activity.handleSAFDialog(file) {
57+ val documentFile = activity.getFileDocument(file.absolutePath)
58+ if (documentFile == null ) {
59+ callback(false )
60+ return @handleSAFDialog
61+ }
62+ documentFile.createDirectory(file.name)
63+ success(alertDialog)
64+ }
5665 } else if (file.mkdirs()) {
5766 success(alertDialog)
58- true
67+ callback( true )
5968 } else
60- false
69+ callback( false )
6170 }
6271
6372 private fun errorOccurred () {
6473 activity.toast(R .string.unknown_error_occurred)
6574 }
6675
67- private fun createFile (file : File , alertDialog : AlertDialog ): Boolean {
76+ private fun createFile (file : File , alertDialog : AlertDialog , callback : ( Boolean ) -> Unit ) {
6877 try {
6978 if (activity.needsStupidWritePermissions(path)) {
70- val documentFile = activity.getFileDocument(file.absolutePath) ? : return false
71- documentFile.createFile(" " , file.name)
72- success(alertDialog)
73- return true
79+ activity.handleSAFDialog(file) {
80+ val documentFile = activity.getFileDocument(file.absolutePath)
81+ if (documentFile == null ) {
82+ callback(false )
83+ return @handleSAFDialog
84+ }
85+ documentFile.createFile(" " , file.name)
86+ success(alertDialog)
87+ }
7488 } else if (file.createNewFile()) {
7589 success(alertDialog)
76- return true
90+ callback( true )
7791 }
7892 } catch (ignored: IOException ) {
7993
8094 }
81-
82- return false
8395 }
8496
8597 private fun success (alertDialog : AlertDialog ) {
0 commit comments