Skip to content

Commit

Permalink
feat: restore recent commits
Browse files Browse the repository at this point in the history
  • Loading branch information
ViscousPot committed Sep 15, 2024
1 parent 962eec0 commit 9980159
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 76 deletions.
32 changes: 17 additions & 15 deletions app/src/main/java/com/viscouspot/gitsync/GitSyncService.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.viscouspot.gitsync

import android.app.ActivityManager
import android.app.Service
import android.app.NotificationChannel
import android.app.NotificationManager
Expand All @@ -12,6 +13,7 @@ import android.os.IBinder
import android.os.Looper
import android.widget.Toast
import androidx.core.app.NotificationCompat
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.viscouspot.gitsync.util.GitManager
import com.viscouspot.gitsync.util.Logger.log
import com.viscouspot.gitsync.util.SettingsManager
Expand Down Expand Up @@ -204,12 +206,12 @@ class GitSyncService : Service() {
displaySyncMessage("Sync complete!")
}

// if (isForeground()) {
// withContext(Dispatchers.Main) {
// val intent = Intent("REFRESH")
// LocalBroadcastManager.getInstance(this@GitSyncService).sendBroadcast(intent)
// }
// }
if (isForeground()) {
withContext(Dispatchers.Main) {
val intent = Intent("REFRESH")
LocalBroadcastManager.getInstance(this@GitSyncService).sendBroadcast(intent)
}
}
}

job.invokeOnCompletion {
Expand All @@ -235,15 +237,15 @@ class GitSyncService : Service() {
}
}

// private fun isForeground(): Boolean {
// val manager = getSystemService(ACTIVITY_SERVICE) as ActivityManager
// val runningTaskInfo = manager.getRunningTasks(1)
// if (runningTaskInfo.isEmpty()) {
// return false
// }
// val componentInfo = runningTaskInfo[0].topActivity
// return componentInfo!!.packageName == packageName
// }
private fun isForeground(): Boolean {
val manager = getSystemService(ACTIVITY_SERVICE) as ActivityManager
val runningTaskInfo = manager.getRunningTasks(1)
if (runningTaskInfo.isEmpty()) {
return false
}
val componentInfo = runningTaskInfo[0].topActivity
return componentInfo!!.packageName == packageName
}

override fun onBind(intent: Intent?): IBinder? {
return null
Expand Down
97 changes: 55 additions & 42 deletions app/src/main/java/com/viscouspot/gitsync/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.viscouspot.gitsync

import android.Manifest
import android.accessibilityservice.AccessibilityServiceInfo
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.graphics.PorterDuff
import android.graphics.Rect
Expand All @@ -29,9 +31,13 @@ import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.button.MaterialButton
import com.viscouspot.gitsync.ui.adapter.ApplicationGridAdapter
import com.viscouspot.gitsync.ui.adapter.Commit
import com.viscouspot.gitsync.ui.adapter.RecentCommitsAdapter
import com.viscouspot.gitsync.ui.fragment.CloneRepoFragment
import com.viscouspot.gitsync.util.GitManager
import com.viscouspot.gitsync.util.Helper
Expand All @@ -41,6 +47,7 @@ import com.viscouspot.gitsync.util.rightDrawable
import java.io.File
import java.util.Locale


class MainActivity : AppCompatActivity() {
private lateinit var applicationObserverMax: ConstraintSet
private lateinit var applicationObserverMin: ConstraintSet
Expand All @@ -49,8 +56,8 @@ class MainActivity : AppCompatActivity() {
private lateinit var settingsManager: SettingsManager
private var onStoragePermissionGranted: (() -> Unit)? = null

// private lateinit var recentCommitsAdapter: RecentCommitsAdapter
// private lateinit var recentCommitsRecycler: RecyclerView
private lateinit var recentCommitsAdapter: RecentCommitsAdapter
private lateinit var recentCommitsRecycler: RecyclerView

private lateinit var forceSyncButton: MaterialButton
private lateinit var syncMessageButton: MaterialButton
Expand All @@ -64,9 +71,7 @@ class MainActivity : AppCompatActivity() {

private lateinit var viewDocs: MaterialButton

// private var refreshingAuthButton = false

// private val recentCommits: MutableList<Commit> = mutableListOf()
private val recentCommits: MutableList<Commit> = mutableListOf()

private lateinit var applicationObserverPanel: ConstraintLayout
private lateinit var applicationObserverSwitch: Switch
Expand All @@ -75,13 +80,13 @@ class MainActivity : AppCompatActivity() {
private lateinit var syncAppOpened: Switch
private lateinit var syncAppClosed: Switch

// private val broadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
// override fun onReceive(context: Context, intent: Intent) {
// if (intent.action == "REFRESH") {
// refreshRecentCommits()
// }
// }
// }
private val broadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == "REFRESH") {
refreshRecentCommits()
}
}
}

private val dirSelectionLauncher = registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) { uri ->
uri?.let {
Expand Down Expand Up @@ -160,19 +165,31 @@ class MainActivity : AppCompatActivity() {
}
}

private fun setRecyclerViewHeight(recyclerView: RecyclerView) {
val adapter = recyclerView.adapter ?: return

val viewHolder = adapter.createViewHolder(recyclerView, adapter.getItemViewType(0))
viewHolder.itemView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

val itemHeight = (viewHolder.itemView.layoutParams as ViewGroup.MarginLayoutParams).topMargin + viewHolder.itemView.measuredHeight

recyclerView.layoutParams.height = itemHeight * 3
recyclerView.requestLayout()
}

override fun onPause() {
super.onPause()

settingsManager.setGitDirPath(gitDirPath.text.toString())
}

// override fun onDestroy() {
// super.onDestroy()
//
//// try {
//// unregisterReceiver(broadcastReceiver)
//// } catch (e: Exception) { }
// }
override fun onDestroy() {
super.onDestroy()

try {
unregisterReceiver(broadcastReceiver)
} catch (e: Exception) { }
}

override fun onResume() {
super.onResume()
Expand All @@ -184,10 +201,10 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)

// val bManager = LocalBroadcastManager.getInstance(this)
// val intentFilter = IntentFilter()
// intentFilter.addAction("REFRESH")
// bManager.registerReceiver(broadcastReceiver, intentFilter)
val bManager = LocalBroadcastManager.getInstance(this)
val intentFilter = IntentFilter()
intentFilter.addAction("REFRESH")
bManager.registerReceiver(broadcastReceiver, intentFilter)

window.statusBarColor = getColor(R.color.app_bg)

Expand All @@ -196,8 +213,8 @@ class MainActivity : AppCompatActivity() {
settingsManager = SettingsManager(this)
gitManager = GitManager(this, this)

// recentCommitsRecycler = findViewById(R.id.recentCommitsRecycler)
// recentCommitsAdapter = RecentCommitsAdapter(recentCommits)
recentCommitsRecycler = findViewById(R.id.recentCommitsRecycler)
recentCommitsAdapter = RecentCommitsAdapter(recentCommits)

forceSyncButton = findViewById(R.id.forceSyncButton)
syncMessageButton = findViewById(R.id.syncMessageButton)
Expand Down Expand Up @@ -225,9 +242,9 @@ class MainActivity : AppCompatActivity() {

refreshAll()

// recentCommitsRecycler.adapter = recentCommitsAdapter
//
// setRecyclerViewHeight(recentCommitsRecycler)
recentCommitsRecycler.adapter = recentCommitsAdapter

setRecyclerViewHeight(recentCommitsRecycler)

forceSyncButton.setOnClickListener {
val forceSyncIntent = Intent(this, GitSyncService::class.java)
Expand Down Expand Up @@ -359,7 +376,7 @@ class MainActivity : AppCompatActivity() {
}

private fun refreshAll() {
// refreshRecentCommits()
refreshRecentCommits()

if (settingsManager.getSyncMessageEnabled()) {
settingsManager.setSyncMessageEnabled(false)
Expand Down Expand Up @@ -413,19 +430,15 @@ class MainActivity : AppCompatActivity() {
}
}

// private fun refreshRecentCommits() {
// val oldSize = recentCommits.size
// val newRecentCommits = gitManager.getRecentCommits(gitDirPath.text.toString()).reversed().filter { !recentCommits.map {commit -> commit.reference}.contains(it.reference) }
// if (newRecentCommits.isNotEmpty()) {
// recentCommits.addAll(newRecentCommits)
// recentCommitsAdapter.notifyItemRangeInserted(oldSize, newRecentCommits.size)
// }
//
// CoroutineScope(Dispatchers.Default).launch {
// delay(200)
// if (recentCommits.size > 0) recentCommitsRecycler.smoothScrollToPosition( recentCommits.size - 1)
// }
// }
private fun refreshRecentCommits() {
val recentCommitsReferences = recentCommits.map {commit -> commit.reference}
val newRecentCommits = gitManager.getRecentCommits(gitDirPath.text.toString()).filter { !recentCommitsReferences.contains(it.reference) }
if (newRecentCommits.isNotEmpty()) {
recentCommits.addAll(0, newRecentCommits)
recentCommitsAdapter.notifyItemRangeInserted(0, newRecentCommits.size)
recentCommitsRecycler.smoothScrollToPosition(0);
}
}

private fun refreshGitRepo() {
var repoName = ""
Expand Down
37 changes: 19 additions & 18 deletions app/src/main/res/layout/main_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@
android:paddingTop="?android:attr/actionBarSize"
android:theme="@style/Theme.MaterialComponents">

<!-- <androidx.recyclerview.widget.RecyclerView-->
<!-- android:id="@+id/recentCommitsRecycler"-->
<!-- android:layout_width="match_parent"-->
<!-- android:background="@drawable/input_bg_top"-->
<!-- android:backgroundTint="@color/card_bg"-->
<!-- android:paddingVertical="@dimen/space_sm"-->
<!-- android:paddingHorizontal="@dimen/space_sm"-->
<!-- android:layout_height="@dimen/recent_commits_height"-->
<!-- app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"-->
<!-- tools:listitem="@layout/recent_commit"-->
<!-- app:spanCount="1"-->
<!-- android:layout_marginTop="@dimen/space_md"-->
<!-- android:requiresFadingEdge="vertical"-->
<!-- app:layout_constraintTop_toTopOf="parent"-->
<!-- app:layout_constraintStart_toStartOf="parent"-->
<!-- app:layout_constraintEnd_toEndOf="parent"/>-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recentCommitsRecycler"
android:layout_width="match_parent"
android:layout_height="@dimen/recent_commits_height"
android:layout_marginTop="8dp"
android:background="@drawable/input_bg_top"
android:backgroundTint="@color/card_bg"
android:paddingHorizontal="@dimen/space_sm"
android:paddingVertical="@dimen/space_sm"
android:requiresFadingEdge="vertical"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:reverseLayout="true"
app:spanCount="1"
tools:listitem="@layout/recent_commit" />

<com.google.android.material.button.MaterialButton
android:id="@+id/forceSyncButton"
Expand All @@ -53,8 +54,8 @@
app:iconTint="@color/textPrimary"
app:layout_constraintEnd_toStartOf="@id/syncMessageButton"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
app:layout_constraintTop_toBottomOf="@id/recentCommitsRecycler"
app:layout_constraintStart_toStartOf="parent"/>

<com.google.android.material.button.MaterialButton
android:id="@+id/syncMessageButton"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
<dimen name="space_sm">8dp</dimen>
<dimen name="space_md">16dp</dimen>
<dimen name="space_lg">32dp</dimen>
<dimen name="divider_height">2dp</dimen>

<dimen name="recent_commits_height">120dp</dimen>
</resources>

0 comments on commit 9980159

Please sign in to comment.