@@ -22,6 +22,7 @@ import com.itsaky.androidide.databinding.FragmentGitBottomSheetBinding
2222import com.itsaky.androidide.fragments.git.adapter.GitFileChangeAdapter
2323import com.itsaky.androidide.git.core.GitCredentialsManager
2424import com.itsaky.androidide.git.core.models.ChangeType
25+ import com.itsaky.androidide.interfaces.IEditorHandler
2526import com.itsaky.androidide.preferences.internal.GitPreferences
2627import com.itsaky.androidide.utils.flashSuccess
2728import com.itsaky.androidide.viewmodel.BottomSheetViewModel
@@ -52,7 +53,6 @@ class GitBottomSheetFragment : Fragment(R.layout.fragment_git_bottom_sheet) {
5253 onFileClicked = { change ->
5354 when (change.type) {
5455 ChangeType .CONFLICTED -> {
55- // Open conflicted file in editor
5656 val activity = requireActivity()
5757 if (activity is EditorHandlerActivity ) {
5858 viewLifecycleOwner.lifecycleScope.launch {
@@ -66,14 +66,16 @@ class GitBottomSheetFragment : Fragment(R.layout.fragment_git_bottom_sheet) {
6666 }
6767 }
6868 else -> {
69- // Show diff in a dialog when changed file is clicked
7069 val dialog = GitDiffViewerDialog .newInstance(change.path)
7170 dialog.show(childFragmentManager, " GitDiffViewerDialog" )
7271 }
7372 }
7473 },
7574 onSelectionChanged = {
7675 validateCommitButton()
76+ },
77+ onResolveConflict = { change ->
78+ viewModel.resolveConflict(change.path)
7779 }
7880 )
7981
@@ -183,19 +185,21 @@ class GitBottomSheetFragment : Fragment(R.layout.fragment_git_bottom_sheet) {
183185 }
184186
185187 binding.commitButton.setOnClickListener {
186- val summary = binding.commitSummary.text?.toString()?.trim() ? : " "
187- val description = binding.commitDescription.text?.toString()?.trim()
188-
189- if (summary.isNotEmpty() && fileChangeAdapter.selectedFiles.isNotEmpty() && hasAuthorInfo()) {
190- viewModel.commitChanges(
191- summary = summary,
192- description = description,
193- selectedPaths = fileChangeAdapter.selectedFiles.toList()
194- ) {
195- // Clear the inputs on successful commit
196- binding.commitSummary.text?.clear()
197- binding.commitDescription.text?.clear()
198- fileChangeAdapter.selectedFiles.clear()
188+ checkUnsavedChangesAndProceed {
189+ val summary = binding.commitSummary.text?.toString()?.trim() ? : " "
190+ val description = binding.commitDescription.text?.toString()?.trim()
191+
192+ if (summary.isNotEmpty() && fileChangeAdapter.selectedFiles.isNotEmpty() && hasAuthorInfo()) {
193+ viewModel.commitChanges(
194+ summary = summary,
195+ description = description,
196+ selectedPaths = fileChangeAdapter.selectedFiles.toList()
197+ ) {
198+ // Clear the inputs on successful commit
199+ binding.commitSummary.text?.clear()
200+ binding.commitDescription.text?.clear()
201+ fileChangeAdapter.selectedFiles.clear()
202+ }
199203 }
200204 }
201205 }
@@ -303,12 +307,14 @@ class GitBottomSheetFragment : Fragment(R.layout.fragment_git_bottom_sheet) {
303307 }
304308
305309 binding.btnPull.setOnClickListener {
306- val username = credentialsManager.getUsername()
307- val token = credentialsManager.getToken()
308- if (! username.isNullOrBlank() && ! token.isNullOrBlank()) {
309- viewModel.pull(username, token)
310- } else {
311- showCredentialsDialog()
310+ checkUnsavedChangesAndProceed {
311+ val username = credentialsManager.getUsername()
312+ val token = credentialsManager.getToken()
313+ if (! username.isNullOrBlank() && ! token.isNullOrBlank()) {
314+ viewModel.pull(username, token)
315+ } else {
316+ showCredentialsDialog()
317+ }
312318 }
313319 }
314320 }
@@ -344,6 +350,25 @@ class GitBottomSheetFragment : Fragment(R.layout.fragment_git_bottom_sheet) {
344350 }
345351 }
346352
353+ private fun checkUnsavedChangesAndProceed (action : () -> Unit ) {
354+ val handler = requireActivity() as ? IEditorHandler
355+ if (handler?.areFilesModified() == true ) {
356+ MaterialAlertDialogBuilder (requireContext())
357+ .setTitle(R .string.title_files_unsaved)
358+ .setMessage(R .string.msg_save_before_git_action)
359+ .setPositiveButton(R .string.save_before_git_action) { _, _ ->
360+ handler.saveAllAsync { action() }
361+ }
362+ .setNegativeButton(R .string.no_save_before_git_action) { _, _ ->
363+ action()
364+ }
365+ .setNeutralButton(android.R .string.cancel, null )
366+ .show()
367+ } else {
368+ action()
369+ }
370+ }
371+
347372 override fun onDestroyView () {
348373 super .onDestroyView()
349374 _binding = null
0 commit comments