|
| 1 | +package com.androidvip.sysctlgui.ui.export |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import android.content.Intent |
| 5 | +import android.os.Bundle |
| 6 | +import android.view.LayoutInflater |
| 7 | +import android.view.View |
| 8 | +import android.view.ViewGroup |
| 9 | +import androidx.annotation.StringRes |
| 10 | +import androidx.fragment.app.Fragment |
| 11 | +import com.androidvip.sysctlgui.R |
| 12 | +import com.androidvip.sysctlgui.data.models.SettingsItem |
| 13 | +import com.androidvip.sysctlgui.databinding.FragmentExportOptionsBinding |
| 14 | +import com.androidvip.sysctlgui.helpers.OnSettingsItemClickedListener |
| 15 | +import com.androidvip.sysctlgui.toast |
| 16 | +import com.google.android.material.dialog.MaterialAlertDialogBuilder |
| 17 | +import org.koin.androidx.viewmodel.ext.android.viewModel |
| 18 | + |
| 19 | +class ExportOptionsFragment : Fragment(), OnSettingsItemClickedListener { |
| 20 | + private var _binding: FragmentExportOptionsBinding? = null |
| 21 | + private val binding get() = _binding!! |
| 22 | + private val viewModel: ExportOptionsViewModel by viewModel() |
| 23 | + |
| 24 | + override fun onCreateView( |
| 25 | + inflater: LayoutInflater, |
| 26 | + container: ViewGroup?, |
| 27 | + savedInstanceState: Bundle? |
| 28 | + ): View { |
| 29 | + _binding = FragmentExportOptionsBinding.inflate(inflater, container, false) |
| 30 | + return binding.root |
| 31 | + } |
| 32 | + |
| 33 | + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
| 34 | + super.onViewCreated(view, savedInstanceState) |
| 35 | + |
| 36 | + val adapter = ExportOptionsItemAdapter(this) |
| 37 | + binding.recyclerView.adapter = adapter |
| 38 | + adapter.submitList(viewModel.getBackOptionItems()) |
| 39 | + |
| 40 | + observeUi() |
| 41 | + } |
| 42 | + |
| 43 | + override fun onDestroyView() { |
| 44 | + super.onDestroyView() |
| 45 | + _binding = null |
| 46 | + } |
| 47 | + |
| 48 | + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { |
| 49 | + if (resultCode != Activity.RESULT_OK) return toast(R.string.error) |
| 50 | + |
| 51 | + when (requestCode) { |
| 52 | + RC_IMPORT_USER_PARAMS, |
| 53 | + RC_RESTORE_PARAMS -> { |
| 54 | + val uri = data?.data ?: return toast(R.string.import_error) |
| 55 | + val extension = uri.lastPathSegment.orEmpty() |
| 56 | + val stream = requireContext().contentResolver.openInputStream(uri) |
| 57 | + ?: return toast(R.string.import_error) |
| 58 | + viewModel.importParams(stream, extension) |
| 59 | + } |
| 60 | + |
| 61 | + RC_EXPORT_USER_PARAMS -> { |
| 62 | + val uri = data?.data ?: return toast(R.string.export_error) |
| 63 | + viewModel.exportParams(uri, requireContext(), false) |
| 64 | + } |
| 65 | + |
| 66 | + RC_BACKUP_PARAMS -> { |
| 67 | + val uri = data?.data ?: return toast(R.string.export_error) |
| 68 | + viewModel.exportParams(uri, requireContext(), true) |
| 69 | + } |
| 70 | + } |
| 71 | + super.onActivityResult(requestCode, resultCode, data) |
| 72 | + } |
| 73 | + |
| 74 | + override fun onSettingsItemClicked(item: SettingsItem, position: Int) { |
| 75 | + when (position) { |
| 76 | + 0 -> viewModel.doWhenImportUserParamsPressed() |
| 77 | + 1 -> viewModel.doWhenExportUserParamsPressed() |
| 78 | + 2 -> viewModel.doWhenBackupPressed() |
| 79 | + 3 -> viewModel.doWhenRestorePressed() |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private fun observeUi() { |
| 84 | + viewModel.viewEffect.observe(viewLifecycleOwner) { |
| 85 | + when (it) { |
| 86 | + is ExportOptionsViewEffect.ImportUserParams -> requestImportFile(RC_IMPORT_USER_PARAMS) |
| 87 | + is ExportOptionsViewEffect.ExportUserParams -> requestExportFile(RC_EXPORT_USER_PARAMS) |
| 88 | + is ExportOptionsViewEffect.RestoreRuntimeParams -> requestImportFile(RC_RESTORE_PARAMS) |
| 89 | + is ExportOptionsViewEffect.BackupRuntimeParams -> requestExportFile(RC_BACKUP_PARAMS) |
| 90 | + is ExportOptionsViewEffect.ShowImportError -> showErrorDialog(it.messageRes) |
| 91 | + is ExportOptionsViewEffect.ShowImportSuccess -> showSuccessDialog( |
| 92 | + getString(R.string.import_success_message, it.paramCount) |
| 93 | + ) |
| 94 | + is ExportOptionsViewEffect.ShowExportError -> showErrorDialog(it.messageRes) |
| 95 | + is ExportOptionsViewEffect.ShowExportSuccess -> showSuccessDialog( |
| 96 | + getString(R.string.export_success_message) |
| 97 | + ) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + viewModel.viewState.observe(viewLifecycleOwner) { |
| 102 | + binding.progress.visibility = if (it.isLoading) View.VISIBLE else View.GONE |
| 103 | + binding.loadingText.visibility = if (it.isLoading) View.VISIBLE else View.GONE |
| 104 | + binding.recyclerView.visibility = if (it.isLoading) View.GONE else View.VISIBLE |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private fun requestImportFile(requestCode: Int) { |
| 109 | + val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply { |
| 110 | + addCategory(Intent.CATEGORY_OPENABLE) |
| 111 | + type = "*/*" |
| 112 | + } |
| 113 | + startActivityForResult(intent, requestCode) |
| 114 | + } |
| 115 | + |
| 116 | + private fun requestExportFile(requestCode: Int) { |
| 117 | + val extension = if (requestCode == RC_BACKUP_PARAMS) "conf" else "json" |
| 118 | + val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply { |
| 119 | + addCategory(Intent.CATEGORY_OPENABLE) |
| 120 | + type = "*/*" |
| 121 | + putExtra(Intent.EXTRA_TITLE, "params.$extension") |
| 122 | + } |
| 123 | + startActivityForResult(intent, requestCode) |
| 124 | + } |
| 125 | + |
| 126 | + private fun showErrorDialog(@StringRes messageRes: Int) { |
| 127 | + MaterialAlertDialogBuilder(requireContext()) |
| 128 | + .setTitle(R.string.error) |
| 129 | + .setMessage(messageRes) |
| 130 | + .setIcon(R.drawable.ic_close) |
| 131 | + .create() |
| 132 | + .also { dialog -> |
| 133 | + if (isAdded) dialog.show() |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private fun showSuccessDialog(message: String) { |
| 138 | + MaterialAlertDialogBuilder(requireContext()) |
| 139 | + .setTitle(R.string.done) |
| 140 | + .setMessage(message) |
| 141 | + .setIcon(R.drawable.ic_check) |
| 142 | + .create() |
| 143 | + .also { dialog -> |
| 144 | + if (isAdded) dialog.show() |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + companion object { |
| 149 | + private const val RC_IMPORT_USER_PARAMS: Int = 1 |
| 150 | + private const val RC_EXPORT_USER_PARAMS: Int = 2 |
| 151 | + private const val RC_BACKUP_PARAMS: Int = 3 |
| 152 | + private const val RC_RESTORE_PARAMS: Int = 4 |
| 153 | + } |
| 154 | +} |
0 commit comments