-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Backup option to the bottom sheet
- Loading branch information
Showing
11 changed files
with
291 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
app/src/main/java/com/tughi/aggregator/activities/backup/BackupActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.tughi.aggregator.activities.backup | ||
|
||
import android.content.ComponentName | ||
import android.content.Intent | ||
import android.content.ServiceConnection | ||
import android.os.Bundle | ||
import android.os.IBinder | ||
import android.view.MenuItem | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.Button | ||
import android.widget.ProgressBar | ||
import androidx.lifecycle.MutableLiveData | ||
import com.tughi.aggregator.AppActivity | ||
import com.tughi.aggregator.R | ||
import com.tughi.aggregator.services.BackupService | ||
import kotlin.math.roundToInt | ||
|
||
class BackupActivity : AppActivity() { | ||
private var service: BackupService? = null | ||
private val serviceConnection = object : ServiceConnection { | ||
override fun onServiceConnected(name: ComponentName?, binder: IBinder?) { | ||
service = (binder as BackupService.LocalBinder).getService().also { service -> | ||
service.status.observe(this@BackupActivity) { status -> | ||
serviceStatus.value = status | ||
} | ||
} | ||
} | ||
|
||
override fun onServiceDisconnected(name: ComponentName?) { | ||
service = null | ||
} | ||
} | ||
|
||
private val serviceStatus = MutableLiveData<BackupService.Status>() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
supportActionBar?.apply { | ||
setDisplayHomeAsUpEnabled(true) | ||
setHomeAsUpIndicator(R.drawable.action_back) | ||
} | ||
|
||
setContentView(R.layout.backup_activity) | ||
|
||
val actionsWrapper = findViewById<ViewGroup>(R.id.actions_wrapper) | ||
val backupButton = actionsWrapper.findViewById<Button>(R.id.backup) | ||
backupButton.setOnClickListener { | ||
startService(Intent(this, BackupService::class.java)) | ||
} | ||
|
||
val progressWrapper = findViewById<ViewGroup>(R.id.progress_wrapper) | ||
val progressBar = progressWrapper.findViewById<ProgressBar>(R.id.progress) | ||
val cancelButton = progressWrapper.findViewById<Button>(R.id.cancel) | ||
cancelButton.setOnClickListener { | ||
service?.cancel() | ||
} | ||
|
||
serviceStatus.observe(this) { status -> | ||
if (status.busy) { | ||
actionsWrapper.visibility = View.GONE | ||
progressWrapper.visibility = View.VISIBLE | ||
progressBar.isIndeterminate = false | ||
progressBar.progress = (status.progress * 100).roundToInt() | ||
} else { | ||
actionsWrapper.visibility = View.VISIBLE | ||
progressWrapper.visibility = View.GONE | ||
progressBar.isIndeterminate = true | ||
} | ||
} | ||
} | ||
|
||
override fun onResume() { | ||
super.onResume() | ||
|
||
Intent(this, BackupService::class.java).also { intent -> | ||
bindService(intent, serviceConnection, BIND_AUTO_CREATE) | ||
} | ||
} | ||
|
||
override fun onPause() { | ||
unbindService(serviceConnection) | ||
|
||
super.onPause() | ||
} | ||
|
||
override fun onOptionsItemSelected(item: MenuItem): Boolean { | ||
when (item.itemId) { | ||
android.R.id.home -> { | ||
finish() | ||
} | ||
else -> { | ||
return super.onOptionsItemSelected(item) | ||
} | ||
} | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
app/src/main/java/com/tughi/aggregator/services/BackupService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.tughi.aggregator.services | ||
|
||
import android.app.Service | ||
import android.content.Intent | ||
import android.os.Binder | ||
import android.os.IBinder | ||
import android.util.Log | ||
import androidx.lifecycle.MutableLiveData | ||
import com.tughi.aggregator.contentScope | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
|
||
class BackupService : Service() { | ||
private val binder = LocalBinder() | ||
|
||
private var currentJob: Job? = null | ||
|
||
val status = MutableLiveData(Status(false)) | ||
|
||
override fun onBind(intent: Intent?): IBinder { | ||
return binder | ||
} | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
|
||
Log.i(BackupService::class.java.simpleName, "onCreate") | ||
} | ||
|
||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | ||
val job = contentScope.launch { | ||
backup() | ||
} | ||
|
||
job.invokeOnCompletion { | ||
currentJob = null | ||
status.postValue(Status(false)) | ||
} | ||
|
||
currentJob = job | ||
|
||
return START_NOT_STICKY | ||
} | ||
|
||
private suspend fun backup() { | ||
val duration = 13000 | ||
val steps = 100L | ||
for (step in 1..steps) { | ||
status.postValue(Status(true, step / steps.toFloat())) | ||
delay(duration / steps) | ||
} | ||
} | ||
|
||
fun cancel() { | ||
currentJob?.cancel() | ||
} | ||
|
||
inner class LocalBinder : Binder() { | ||
fun getService() = this@BackupService | ||
} | ||
|
||
data class Status(val busy: Boolean, val progress: Float = 0f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:tint="?colorControlNormal" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24"> | ||
<path | ||
android:fillColor="#FF000000" | ||
android:pathData="M9,3L5,6.99h3L8,14h2L10,6.99h3L9,3zM16,17.01L16,10h-2v7.01h-3L15,21l4,-3.99h-3z" /> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:layout_margin="16dp" | ||
android:orientation="vertical"> | ||
|
||
<TextView | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:text="TODO: Explain why is this feature su great!" | ||
android:textAppearance="?textAppearanceBody1" /> | ||
|
||
<LinearLayout | ||
android:id="@+id/actions_wrapper" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical"> | ||
|
||
<Button | ||
android:id="@+id/backup" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="16dp" | ||
android:text="Backup" /> | ||
|
||
<Button | ||
android:id="@+id/restore" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="16dp" | ||
android:text="Restore" /> | ||
|
||
</LinearLayout> | ||
|
||
<LinearLayout | ||
android:id="@+id/progress_wrapper" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical"> | ||
|
||
<TextView | ||
android:id="@+id/status" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="16dp" | ||
android:text="In progress ..." | ||
android:textAppearance="?textAppearanceCaption" /> | ||
|
||
<ProgressBar | ||
android:id="@+id/progress" | ||
style="?android:attr/progressBarStyleHorizontal" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="8dp" | ||
android:indeterminate="true" /> | ||
|
||
<Button | ||
android:id="@+id/cancel" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="16dp" | ||
android:text="Cancel" /> | ||
|
||
</LinearLayout> | ||
|
||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
- Changed OPML support | ||
* allows selection of already aggregated feeds for import | ||
* implemented export feature | ||
|
||
* TODO: Added feature to dump data for loading it on a different device | ||
* TODO: Remove automatic backup (replaced by dump feature) | ||
* TODO: The app clears state of affected feeds when the default clean-up mode is changed | ||
|
||
* TODO: Fix app-launch updates by using an OnBackPressedCallback to detect when the user leaves the MainActivity |