Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,11 @@ class CloneRepositoryFragment : BaseFragment() {
}

cloneButton.setOnClickListener {
val url = repoUrl.text.toString()
val path = localPath.text.toString()
val username = if (authCheckbox.isChecked) username.text.toString() else null
val password = if (authCheckbox.isChecked) password.text.toString() else null

viewModel.cloneRepository(url, path, username, password)
cloneRepo()
}

retryButton.setOnClickListener {
cloneRepo()
}

exitButton.setOnClickListener {
Expand All @@ -110,6 +109,15 @@ class CloneRepositoryFragment : BaseFragment() {
}
}

private fun FragmentCloneRepositoryBinding.cloneRepo() {
val url = repoUrl.text.toString()
val path = localPath.text.toString()
val mUsername = if (authCheckbox.isChecked) username.text.toString() else null
val mPassword = if (authCheckbox.isChecked) password.text.toString() else null

viewModel.cloneRepository(url, path, mUsername, mPassword)
}

private fun observeViewModel() {
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
Expand Down Expand Up @@ -140,14 +148,17 @@ class CloneRepositoryFragment : BaseFragment() {
when (state) {
is CloneRepoUiState.Idle -> {
cloneButton.isEnabled = state.isCloneButtonEnabled
retryButton.visibility = View.GONE
statusText.text = ""
}
is CloneRepoUiState.Cloning -> {
cloneButton.isEnabled = false
retryButton.visibility = View.GONE
statusText.text = getString(R.string.cloning_repo)
}
is CloneRepoUiState.Error -> {
cloneButton.isEnabled = true
retryButton.visibility = View.VISIBLE
val statusMessage = state.errorResId?.let { getString(it) } ?: state.errorMessage
statusText.text = statusMessage
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.application
import androidx.lifecycle.viewModelScope
import com.itsaky.androidide.R
import com.itsaky.androidide.git.core.GitRepositoryManager
import com.itsaky.androidide.git.core.models.CloneRepoUiState
import kotlinx.coroutines.Dispatchers
Expand All @@ -16,7 +15,12 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.eclipse.jgit.lib.ProgressMonitor
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider
import org.eclipse.jgit.api.errors.TransportException
import java.net.UnknownHostException
import java.io.EOFException
import java.io.File
import com.blankj.utilcode.util.NetworkUtils
import com.itsaky.androidide.resources.R

class CloneRepositoryViewModel(application: Application) : AndroidViewModel(application) {

Expand Down Expand Up @@ -67,6 +71,17 @@ class CloneRepositoryViewModel(application: Application) : AndroidViewModel(appl
return
}

if (!NetworkUtils.isConnected()) {
_uiState.update {
CloneRepoUiState.Error(
url = url,
localPath = localPath,
errorResId = R.string.no_internet_connection
)
}
return
}

viewModelScope.launch {
var hasCloned = false
_uiState.update {
Expand Down Expand Up @@ -143,12 +158,28 @@ class CloneRepositoryViewModel(application: Application) : AndroidViewModel(appl
CloneRepoUiState.Success(localPath = localPath)
}
} catch (e: Exception) {
val errorMessage = e.message ?: application.getString(R.string.unknown_error)
// Error handling
val isNetworkError = e is TransportException && e.cause is UnknownHostException
val isConnectionDrop = e.cause is EOFException ||
e.message?.contains("Unexpected end of stream") == true ||
e.message?.contains("Software caused connection abort") == true

val errorResId = when {
isNetworkError -> R.string.no_internet_connection
isConnectionDrop -> R.string.connection_lost
else -> null
}

val errorMessage = if (errorResId == null) {
e.message ?: application.getString(R.string.unknown_error)
} else null

_uiState.update {
CloneRepoUiState.Error(
url = url,
localPath = localPath,
errorMessage = application.getString(R.string.clone_failed, errorMessage)
errorResId = errorResId,
errorMessage = errorMessage?.let { application.getString(R.string.clone_failed, it) }
)
}
} finally {
Expand Down
Loading
Loading