Skip to content

Commit

Permalink
feat: move merge operation into background
Browse files Browse the repository at this point in the history
  • Loading branch information
ViscousPot committed Nov 8, 2024
1 parent ce4e330 commit a2486a6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 43 deletions.
45 changes: 45 additions & 0 deletions app/src/main/java/com/viscouspot/gitsync/GitSyncService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class GitSyncService : Service() {
companion object {
const val NOTIFICATION_CHANNEL_ID = "git_sync_service_channel"
const val NOTIFICATION_CHANNEL_NAME = "Git Sync Service"
const val MERGE = "MERGE"
const val FORCE_SYNC = "FORCE_SYNC"
const val APPLICATION_SYNC = "APPLICATION_SYNC"
const val INTENT_SYNC = "INTENT_SYNC"
Expand All @@ -55,6 +56,10 @@ class GitSyncService : Service() {
}

when (intent.action) {
MERGE -> {
log(LogType.ToServiceCommand, "Merge")
merge()
}
FORCE_SYNC -> {
log(LogType.ToServiceCommand, "Force Sync")
debouncedSync(forced = true)
Expand Down Expand Up @@ -240,6 +245,46 @@ class GitSyncService : Service() {
}
}

private fun merge() {
CoroutineScope(Dispatchers.Default).launch {
val authCredentials = settingsManager.getGitAuthCredentials()
if (settingsManager.getGitDirUri() == null || authCredentials.first == "" || authCredentials.second == "") return@launch

val pushResult = gitManager.uploadChanges(
settingsManager.getGitDirUri()!!,
settingsManager.getSyncMessage(),
authCredentials.first,
authCredentials.second
) {
Handler(Looper.getMainLooper()).post {
Toast.makeText(
applicationContext,
getString(R.string.resolving_merge),
Toast.LENGTH_SHORT
)
.show()
}
}

when (pushResult) {
null -> {
log(LogType.Sync, "Merge Failed")
return@launch
}

true -> log(LogType.Sync, "Merge Complete")
false -> log(LogType.Sync, "Merge Not Required")
}

debouncedSync(forced = true)

if (isForeground()) {
val intent = Intent(MainActivity.MERGE_COMPLETE)
LocalBroadcastManager.getInstance(this@GitSyncService).sendBroadcast(intent)
}
}
}

private fun displaySyncMessage(msg: String) {
if (settingsManager.getSyncMessageEnabled()) {
Handler(Looper.getMainLooper()).post {
Expand Down
50 changes: 8 additions & 42 deletions app/src/main/java/com/viscouspot/gitsync/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,17 @@ class MainActivity : AppCompatActivity() {

companion object {
const val REFRESH = "REFRESH"
const val MERGE_COMPLETE = "MERGE_COMPLETE"
}

private val broadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
REFRESH -> refreshRecentCommits()
MERGE_COMPLETE -> {
mergeConflictDialog?.dismiss()
refreshRecentCommits()
}
}
}
}
Expand Down Expand Up @@ -225,10 +230,6 @@ class MainActivity : AppCompatActivity() {
}

refreshAll()

if (gitManager.getConflicting(settingsManager.getGitDirUri()).isNotEmpty()) {
openMergeConflictDialog()
}
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -646,44 +647,9 @@ class MainActivity : AppCompatActivity() {
merge.setTextColor(getColor(R.color.textSecondary))
abortMerge.visibility = View.GONE

CoroutineScope(Dispatchers.Default).launch {
val authCredentials = settingsManager.getGitAuthCredentials()
if (settingsManager.getGitDirUri() == null || authCredentials.first == "" || authCredentials.second == "") return@launch

val pushResult = gitManager.uploadChanges(
settingsManager.getGitDirUri()!!,
settingsManager.getSyncMessage(),
authCredentials.first,
authCredentials.second
) {
Handler(Looper.getMainLooper()).post {
Toast.makeText(
applicationContext,
"Resolving merge...",
Toast.LENGTH_SHORT
)
.show()
}
}

when (pushResult) {
null -> {
log(LogType.Sync, "Merge Failed")
return@launch
}

true -> log(LogType.Sync, "Merge Complete")
false -> log(LogType.Sync, "Merge Not Required")
}

refreshRecentCommits()

val forceSyncIntent = Intent(this@MainActivity, GitSyncService::class.java)
forceSyncIntent.setAction(GitSyncService.FORCE_SYNC)
startService(forceSyncIntent)

mergeConflictDialog?.dismiss()
}
val forceSyncIntent = Intent(this@MainActivity, GitSyncService::class.java)
forceSyncIntent.setAction(GitSyncService.MERGE)
startService(forceSyncIntent)
}

abortMerge.setOnClickListener {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@
<string name="merge_conflict_item_title">MERGE CONFLICT</string>
<string name="merge_conflict_item_message">There is a merge conflict! Tap to resolve</string>
<string name="merge">Merge</string>
<string name="merging">Merging...</string>
<string name="merging">Merging…</string>
<string name="resolving_merge">Resolving merge…</string>

<string name="conflict_start"><![CDATA[<<<<<<<]]></string>
<string name="conflict_separator">=======</string>
Expand Down

0 comments on commit a2486a6

Please sign in to comment.