Skip to content

Commit 123ca52

Browse files
committed
refactor
1 parent de7a7b3 commit 123ca52

5 files changed

Lines changed: 50 additions & 35 deletions

File tree

app/src/main/java/de/felixnuesse/disky/MainActivity.kt

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,15 @@ class MainActivity : AppCompatActivity(), ChangeFolderCallback, ScanCompleteCall
124124
binding.dropdown.onItemClickListener = OnItemClickListener { parent, view, position, id ->
125125
selectedStorage = storageList[position]
126126
binding.removableStorageWarning.visibility = View.GONE
127-
triggerDataUpdate()
127+
refreshData()
128128
}
129129
if (storageList.size == 1) {
130130
binding.storageSelector.visibility = View.GONE
131131
}
132132

133-
registerReciever()
133+
registerReceiver()
134134
if (isIntroComplete) {
135-
triggerDataUpdate()
135+
refreshData()
136136
BackgroundWorker.schedule(this)
137137
}
138138

@@ -177,7 +177,7 @@ class MainActivity : AppCompatActivity(), ChangeFolderCallback, ScanCompleteCall
177177
return true
178178
}
179179
if (id == R.id.action_reload) {
180-
requestDataRefresh()
180+
refreshData(true)
181181
return true
182182
}
183183
return super.onOptionsItemSelected(item)
@@ -192,8 +192,6 @@ class MainActivity : AppCompatActivity(), ChangeFolderCallback, ScanCompleteCall
192192

193193
override fun scanComplete(result: StorageResult) {
194194

195-
// todo: remove this logging:
196-
result.scannedVolume
197195
Log.e(
198196
"POST_SCAN",
199197
"${result.scannedVolume} ${result.free.div(result.total)} ${result.used}"
@@ -232,15 +230,15 @@ class MainActivity : AppCompatActivity(), ChangeFolderCallback, ScanCompleteCall
232230

233231
/** UI METHODS **/
234232

235-
fun registerReciever() {
233+
fun registerReceiver() {
236234
val receiver = object : BroadcastReceiver() {
237235
override fun onReceive(context: Context?, intent: Intent) {
238236
if (intent.action == SCAN_ABORTED) {
239237
binding.progressLabel.text = getString(R.string.scan_was_aborted)
240238
return
241239
}
242240
if (intent.action == SCAN_REFRESH_REQUESTED) {
243-
requestDataRefresh()
241+
refreshData(true)
244242
}
245243
if (intent.action == SCAN_PROGRESSED) {
246244
val progress = intent.getIntExtra(SCAN_PROGRESSED, 0)
@@ -249,6 +247,7 @@ class MainActivity : AppCompatActivity(), ChangeFolderCallback, ScanCompleteCall
249247
binding.progressLabel.text = "$progress%"
250248
}
251249
if (intent.action == SCAN_COMPLETE) {
250+
binding.progressLabel.text = "0%" // reset, so that on a re-scan, it shows 0 first.
252251
Log.e(
253252
tag(),
254253
"OLD: Scanning and processing took: ${System.currentTimeMillis() - lastScanStarted}ms"
@@ -263,8 +262,29 @@ class MainActivity : AppCompatActivity(), ChangeFolderCallback, ScanCompleteCall
263262
LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter)
264263
}
265264

266-
fun triggerDataUpdate() {
265+
fun refreshData(considerContext: Boolean = false) {
267266
Log.e(tag(), "trigger update!")
267+
268+
var currentTypeNotApplicable = false
269+
270+
if(considerContext) {
271+
272+
val type = when (currentElement?.storageType) {
273+
StorageType.APP -> {
274+
currentTypeNotApplicable = true
275+
R.string.reload_blocked_because_inapps
276+
}
277+
else -> {
278+
R.string.reload
279+
}
280+
}
281+
282+
Toast.makeText(this, type, Toast.LENGTH_SHORT).show()
283+
}
284+
if(currentTypeNotApplicable) {
285+
return
286+
}
287+
268288
runOnUiThread {
269289
binding.folders.visibility = View.INVISIBLE
270290
binding.overview.visibility = View.INVISIBLE
@@ -303,7 +323,6 @@ class MainActivity : AppCompatActivity(), ChangeFolderCallback, ScanCompleteCall
303323
*/
304324
}
305325

306-
307326
fun handleBack(): Boolean {
308327
return if (currentElement != rootElement) {
309328
currentElement?.parent?.let { showFolder(it) }
@@ -408,18 +427,4 @@ class MainActivity : AppCompatActivity(), ChangeFolderCallback, ScanCompleteCall
408427
val recyclerViewAdapter = RecyclerViewAdapter(this, children, this)
409428
recyclerView.adapter = recyclerViewAdapter
410429
}
411-
412-
private fun requestDataRefresh() {
413-
when (currentElement?.storageType) {
414-
StorageType.APP -> {
415-
Toast.makeText(this, R.string.reload_blocked_because_inapps, Toast.LENGTH_SHORT)
416-
.show()
417-
}
418-
419-
else -> {
420-
Toast.makeText(this, R.string.reload, Toast.LENGTH_SHORT).show()
421-
triggerDataUpdate()
422-
}
423-
}
424-
}
425430
}

app/src/main/java/de/felixnuesse/disky/background/ScanService.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ class ScanService: Service() {
2222
private lateinit var notificationUtils: NotificationUtils
2323
private var serviceRunId = 0L
2424

25-
private lateinit var updates: UpdateCallback
26-
2725
private var startedScan: Long = 0
2826

2927
companion object {
@@ -42,7 +40,6 @@ class ScanService: Service() {
4240

4341
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
4442
notificationUtils = NotificationUtils(applicationContext)
45-
updates = UpdateCallback(applicationContext)
4643

4744
val message = getString(R.string.foreground_service_notification_starting_message)
4845
val notification = notificationUtils.getNotification(message, message).build()
@@ -73,7 +70,9 @@ class ScanService: Service() {
7370
}
7471

7572
try {
76-
val result = Scanner(context, updates).scan(storageToScan, subfolder)
73+
val callback = UpdateCallback(applicationContext)
74+
75+
val result = Scanner(context, callback).scan(storageToScan, subfolder)
7776
Log.e(tag(), "Scanning took: ${System.currentTimeMillis()-now}ms ${wasStopped(thisServiceRunId)}")
7877
if(wasStopped(thisServiceRunId)) {
7978
val resultIntent = Intent(SCAN_ABORTED)

app/src/main/java/de/felixnuesse/disky/background/UpdateCallback.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class UpdateCallback(private var mContext: Context): ScannerCallback {
1818
private var foundLeafLastAction = System.currentTimeMillis()
1919
private var currentlyScanningLastAction = System.currentTimeMillis()
2020

21+
override fun setMaxSize(totalSpace: Long) {
22+
maxSize = totalSpace
23+
}
2124

2225
override fun currentlyScanning(item: String) {
2326

@@ -39,25 +42,29 @@ class UpdateCallback(private var mContext: Context): ScannerCallback {
3942
}
4043

4144
override fun foundLeaf(size: Long) {
42-
4345
processedSize += size
4446
val perc = ((processedSize.div(maxSize.toFloat()))*100).toInt()
4547

46-
4748
val now = System.currentTimeMillis()
4849
if(perc != 100 && (now - foundLeafLastAction) < 100) {
4950
// Log.e(tag(), "foundLeaf, skip since last update is less than 100ms")
5051
return
5152
}
52-
53+
foundLeafLastAction = System.currentTimeMillis()
54+
System.err.println(toString())
5355

5456
if(perc != lastReportedPercentage) {
5557
lastReportedPercentage = perc
5658
val progress = Intent(SCAN_PROGRESSED)
5759
progress.putExtra(SCAN_PROGRESSED, perc)
5860
LocalBroadcastManager.getInstance(mContext).sendBroadcast(progress)
59-
foundLeafLastAction = System.currentTimeMillis()
6061
}
6162

6263
}
64+
65+
override fun toString(): String {
66+
return "UpdateCallback(processedSize=$processedSize, lastReportedPercentage=$lastReportedPercentage, maxSize=$maxSize)"
67+
}
68+
69+
6370
}

app/src/main/java/de/felixnuesse/disky/scanner/Scanner.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ class Scanner(private var mContext: Context, private var callback: ScannerCallba
2121

2222
private var fsScanner: FsScanner? = null
2323
private var fullyMulticoreFsScanner: FullyMulticoreFsScanner? = null
24-
25-
private var maxSize = 0L
2624
private var stopped = false
2725

2826

@@ -35,7 +33,9 @@ class Scanner(private var mContext: Context, private var callback: ScannerCallba
3533
val selectedStorage = findStorageByNameOrUUID(storage)
3634
val rootElement: StoragePrototype?
3735

38-
maxSize = getTotalSpace(storageStatsManager, selectedStorage)
36+
// todo: If we re-scan a sub-path, this is wrong. We should use the parent-folder-size, but
37+
// this is hard to do since we don't know the size.
38+
callback?.setMaxSize(getTotalSpace(storageStatsManager, selectedStorage))
3939
val subfolder = subpath?.replace(selectedStorage.directory!!.absolutePath+"/", "")?: ""
4040

4141
fsScanner = FsScanner(callback)

app/src/main/java/de/felixnuesse/disky/scanner/ScannerCallback.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package de.felixnuesse.disky.scanner
22

33
interface ScannerCallback {
4+
5+
6+
fun setMaxSize(totalSpace: Long)
7+
48
fun currentlyScanning(item: String)
59

610
fun foundLeaf(size: Long)

0 commit comments

Comments
 (0)