Skip to content

Commit

Permalink
feat: display sync message immediately instead of after completion
Browse files Browse the repository at this point in the history
  • Loading branch information
ViscousPot committed Jul 5, 2024
1 parent 14a1a93 commit 725bf1d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
27 changes: 18 additions & 9 deletions app/src/main/java/com/viscouspot/gitsync/GitSyncService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,17 @@ class GitSyncService : Service() {
return@launch
}

var synced = false

log(applicationContext, "Sync", "Start Pull Repo")
val pullResult = gitManager.pullRepository(
gitDirPath,
authCredentials.first,
authCredentials.second
)
) {
synced = true
displaySyncMessage()
}

when (pullResult) {
null -> {
Expand All @@ -221,7 +226,11 @@ class GitSyncService : Service() {
gitDirPath,
authCredentials.first,
authCredentials.second
)
) {
if (!synced) {
displaySyncMessage()
}
}

when (pushResult) {
null -> {
Expand All @@ -247,13 +256,6 @@ class GitSyncService : Service() {
LocalBroadcastManager.getInstance(this@GitSyncService).sendBroadcast(intent)
}
}

if (settingsManager.getSyncMessageEnabled()) {
withContext(Dispatchers.Main) {
Toast.makeText(applicationContext, "Files synced!", Toast.LENGTH_SHORT)
.show()
}
}
}

currentSyncJob?.invokeOnCompletion {
Expand All @@ -263,6 +265,13 @@ class GitSyncService : Service() {
}
}

private fun displaySyncMessage() {
if (settingsManager.getSyncMessageEnabled()) {
Toast.makeText(applicationContext, "Files synced!", Toast.LENGTH_SHORT)
.show()
}
}

private fun isForeground(): Boolean {
val manager = getSystemService(ACTIVITY_SERVICE) as ActivityManager
val runningTaskInfo = manager.getRunningTasks(1)
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/com/viscouspot/gitsync/util/GitManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class GitManager(private val context: Context, private val activity: AppCompatAc
}


fun pullRepository(storageDir: String, username: String, token: String): Boolean? {
fun pullRepository(storageDir: String, username: String, token: String, onSync: () -> Unit): Boolean? {
try {
var returnResult: Boolean? = false
log(context, "PullFromRepo", "Getting local directory")
Expand All @@ -175,6 +175,7 @@ class GitManager(private val context: Context, private val activity: AppCompatAc

if (!fetchResult.trackingRefUpdates.isEmpty()) {
log(context, "PullFromRepo", "Pulling changes")
onSync.invoke()
val result = git.pull()
.setCredentialsProvider(cp)
.setRemote("origin")
Expand All @@ -196,7 +197,7 @@ class GitManager(private val context: Context, private val activity: AppCompatAc
return null
}

fun pushAllToRepository(repoUrl: String, storageDir: String, username: String, token: String): Boolean? {
fun pushAllToRepository(repoUrl: String, storageDir: String, username: String, token: String, onSync: () -> Unit): Boolean? {
try {
var returnResult = false
log(context, "PushToRepo", "Getting local directory")
Expand All @@ -207,7 +208,7 @@ class GitManager(private val context: Context, private val activity: AppCompatAc
logStatus(git)
val status = git.status().call()
if (status.uncommittedChanges.isNotEmpty() || status.untracked.isNotEmpty()) {

onSync.invoke()
log(context, "PushToRepo", "Adding Files to Stage")
git.add().addFilepattern(".").call();
git.add().addFilepattern(".").setUpdate(true).call();
Expand Down

0 comments on commit 725bf1d

Please sign in to comment.