|
| 1 | +package io.github.vikulin.opengammakit.view |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.os.Bundle |
| 5 | +import android.view.* |
| 6 | +import android.widget.* |
| 7 | +import androidx.activity.result.contract.ActivityResultContracts |
| 8 | +import androidx.documentfile.provider.DocumentFile |
| 9 | +import androidx.fragment.app.DialogFragment |
| 10 | +import io.github.vikulin.opengammakit.R |
| 11 | +import io.github.vikulin.opengammakit.model.OpenGammaKitData |
| 12 | +import java.text.SimpleDateFormat |
| 13 | +import java.util.Date |
| 14 | +import java.util.Locale |
| 15 | + |
| 16 | +class SaveSpectrumDataIntoFileDialogFragment : DialogFragment() { |
| 17 | + |
| 18 | + private lateinit var fileNameEditText: TextView |
| 19 | + private lateinit var selectedLocationText: TextView |
| 20 | + |
| 21 | + companion object { |
| 22 | + private const val SPECTRUM_DATA = "spectrum_data_to_save" |
| 23 | + |
| 24 | + fun newInstance(spectrumData: OpenGammaKitData): SaveSpectrumDataIntoFileDialogFragment { |
| 25 | + val fragment = SaveSpectrumDataIntoFileDialogFragment() |
| 26 | + val args = Bundle() |
| 27 | + args.putSerializable(SPECTRUM_DATA, spectrumData) |
| 28 | + fragment.arguments = args |
| 29 | + return fragment |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + interface SaveSpectrumDataIntoFileListener { |
| 34 | + fun onSaveToFile(fileName: String, locationUri: String, spectrumData: OpenGammaKitData) |
| 35 | + } |
| 36 | + |
| 37 | + private var saveSpectrumListener: SaveSpectrumDataIntoFileListener? = null |
| 38 | + |
| 39 | + override fun onAttach(context: Context) { |
| 40 | + super.onAttach(context) |
| 41 | + saveSpectrumListener = when { |
| 42 | + parentFragment is SaveSpectrumDataIntoFileListener -> parentFragment as SaveSpectrumDataIntoFileListener |
| 43 | + context is SaveSpectrumDataIntoFileListener -> context |
| 44 | + else -> throw IllegalStateException("Host must implement SaveSpectrumListener") |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + override fun onDetach() { |
| 49 | + super.onDetach() |
| 50 | + saveSpectrumListener = null |
| 51 | + } |
| 52 | + |
| 53 | + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { |
| 54 | + val view = inflater.inflate(R.layout.dialog_spectrum_file_save, container, false) |
| 55 | + |
| 56 | + fileNameEditText = view.findViewById<EditText>(R.id.fileNameEditText) |
| 57 | + val selectLocationButton = view.findViewById<ImageButton>(R.id.selectLocationButton) |
| 58 | + selectedLocationText = view.findViewById<TextView>(R.id.selectedLocationText) |
| 59 | + val saveButton = view.findViewById<Button>(R.id.saveButton) |
| 60 | + val formatter = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()) |
| 61 | + val timestamp = formatter.format(Date()) |
| 62 | + val fileName = "spectrum_$timestamp.json" |
| 63 | + fileNameEditText.setText(fileName) |
| 64 | + selectedLocationText.text = "Documents/OpenGammaKit/" |
| 65 | + selectLocationButton.setOnClickListener { |
| 66 | + val name = fileNameEditText.text.toString().trim() |
| 67 | + if (name.isEmpty()) { |
| 68 | + Toast.makeText(context, "Enter a file name first", Toast.LENGTH_SHORT).show() |
| 69 | + } else { |
| 70 | + createDocumentLauncher.launch(name) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + val spectrumData = arguments?.getSerializable(SPECTRUM_DATA) as? OpenGammaKitData |
| 75 | + ?: return view |
| 76 | + saveButton.setOnClickListener { |
| 77 | + val fileName = fileNameEditText.text.toString().trim() |
| 78 | + val uri = selectedLocationText.text |
| 79 | + |
| 80 | + when { |
| 81 | + fileName.isEmpty() -> { |
| 82 | + Toast.makeText(context, "Please enter a file name", Toast.LENGTH_SHORT).show() |
| 83 | + } |
| 84 | + uri == null -> { |
| 85 | + Toast.makeText(context, "Please select a save location", Toast.LENGTH_SHORT).show() |
| 86 | + } |
| 87 | + else -> { |
| 88 | + saveSpectrumListener?.onSaveToFile(fileName, uri.toString(), spectrumData) |
| 89 | + dismiss() |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return view |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + private val createDocumentLauncher = |
| 99 | + registerForActivityResult(ActivityResultContracts.CreateDocument("application/json")) { uri -> |
| 100 | + if (uri != null) { |
| 101 | + // Get URI path and remove file name + extension if present |
| 102 | + val rawPath = uri.path ?: uri.toString() |
| 103 | + val cleanedPath = rawPath |
| 104 | + .substringBeforeLast('/') |
| 105 | + .substringAfterLast(':') |
| 106 | + val fileName = rawPath.substringAfterLast('/') |
| 107 | + fileNameEditText.text = fileName |
| 108 | + selectedLocationText.text = cleanedPath |
| 109 | + |
| 110 | + val docFile = DocumentFile.fromSingleUri(requireContext(), uri) |
| 111 | + if (docFile?.delete() != true) { |
| 112 | + Toast.makeText(requireContext(), "Delete not supported", Toast.LENGTH_SHORT).show() |
| 113 | + } |
| 114 | + } else { |
| 115 | + Toast.makeText(requireContext(), "File creation canceled", Toast.LENGTH_SHORT).show() |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments