Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Welcome Activity] Add button to restore backup and skip instance selection #4127

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,50 @@ package com.github.libretube.ui.activities
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.view.isGone
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.get
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.databinding.ActivityWelcomeBinding
import com.github.libretube.helpers.BackupHelper
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.ui.adapters.InstancesAdapter
import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.ui.models.WelcomeModel
import com.github.libretube.ui.preferences.BackupRestoreSettings
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class WelcomeActivity : BaseActivity() {
private lateinit var binding: ActivityWelcomeBinding
private var viewModel: WelcomeModel? = null

private val restoreFilePicker =
registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
if (uri == null) return@registerForActivityResult
CoroutineScope(Dispatchers.IO).launch {
BackupHelper.restoreAdvancedBackup(this@WelcomeActivity, uri)

// only skip the welcome activity if the restored backup contains an instance
if (PreferenceHelper.getString(PreferenceKeys.FETCH_INSTANCE, "").isNotEmpty()) {
withContext(Dispatchers.Main) { startMainActivity() }
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = ViewModelProvider(this).get()

binding = ActivityWelcomeBinding.inflate(layoutInflater)
setContentView(binding.root)

// ALl the binding values are optional due to two different possible layouts (normal, landscape)
viewModel!!.instances.observe(this) { instances ->
binding.instancesRecycler?.layoutManager = LinearLayoutManager(this@WelcomeActivity)
binding.instancesRecycler?.adapter = InstancesAdapter(instances, viewModel!!) { index ->
Expand All @@ -42,12 +63,20 @@ class WelcomeActivity : BaseActivity() {
val selectedInstance =
viewModel!!.instances.value!![viewModel!!.selectedInstanceIndex.value!!]
PreferenceHelper.putString(PreferenceKeys.FETCH_INSTANCE, selectedInstance.apiUrl)
val mainActivityIntent = Intent(this@WelcomeActivity, MainActivity::class.java)
startActivity(mainActivityIntent)
finish()
startMainActivity()
} else {
Toast.makeText(this, R.string.choose_instance, Toast.LENGTH_LONG).show()
}
}

binding.restore?.setOnClickListener {
restoreFilePicker.launch(BackupRestoreSettings.JSON)
}
}

private fun startMainActivity() {
val mainActivityIntent = Intent(this@WelcomeActivity, MainActivity::class.java)
startActivity(mainActivityIntent)
finish()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ class BackupRestoreSettings : BasePreferenceFragment() {
override val titleResourceId: Int = R.string.backup_restore

// backup and restore database
private val getBackupFile = registerForActivityResult(ActivityResultContracts.GetContent()) {
it?.let {
private val getBackupFile =
registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
if (uri == null) return@registerForActivityResult
CoroutineScope(Dispatchers.IO).launch {
BackupHelper.restoreAdvancedBackup(requireContext(), it)
BackupHelper.restoreAdvancedBackup(requireContext(), uri)
withContext(Dispatchers.Main) {
// could fail if fragment is already closed
runCatching {
Expand All @@ -64,12 +65,10 @@ class BackupRestoreSettings : BasePreferenceFragment() {
}
}
}
}
private val createBackupFile = registerForActivityResult(CreateDocument(JSON)) {
it?.let {
CoroutineScope(Dispatchers.IO).launch {
BackupHelper.createAdvancedBackup(requireContext(), it, backupFile)
}
private val createBackupFile = registerForActivityResult(CreateDocument(JSON)) { uri ->
if (uri == null) return@registerForActivityResult
CoroutineScope(Dispatchers.IO).launch {
BackupHelper.createAdvancedBackup(requireContext(), uri, backupFile)
}
}

Expand All @@ -78,19 +77,17 @@ class BackupRestoreSettings : BasePreferenceFragment() {
*/
private val getSubscriptionsFile = registerForActivityResult(
ActivityResultContracts.GetContent()
) {
it?.let {
lifecycleScope.launch(Dispatchers.IO) {
ImportHelper.importSubscriptions(requireActivity(), it, importFormat)
}
) { uri ->
if (uri == null) return@registerForActivityResult
lifecycleScope.launch(Dispatchers.IO) {
ImportHelper.importSubscriptions(requireActivity(), uri, importFormat)
}
}

private val createSubscriptionsFile = registerForActivityResult(CreateDocument(JSON)) {
it?.let {
lifecycleScope.launch(Dispatchers.IO) {
ImportHelper.exportSubscriptions(requireActivity(), it, importFormat)
}
private val createSubscriptionsFile = registerForActivityResult(CreateDocument(JSON)) { uri ->
if (uri == null) return@registerForActivityResult
lifecycleScope.launch(Dispatchers.IO) {
ImportHelper.exportSubscriptions(requireActivity(), uri, importFormat)
}
}

Expand Down Expand Up @@ -197,6 +194,6 @@ class BackupRestoreSettings : BasePreferenceFragment() {
}

companion object {
private const val JSON = "application/json"
const val JSON = "application/json"
}
}
29 changes: 21 additions & 8 deletions app/src/main/res/layout-land/activity_welcome.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,29 @@

</FrameLayout>

<com.google.android.material.button.MaterialButton
android:id="@id/okay"
android:layout_width="wrap_content"
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginHorizontal="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="5dp"
android:alpha="0.5"
android:text="@string/okay" />
android:layout_marginBottom="5dp">

<com.google.android.material.button.MaterialButton
android:id="@id/restore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="@string/restore" />

<com.google.android.material.button.MaterialButton
android:id="@id/okay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:alpha="0.5"
android:text="@string/okay" />

</FrameLayout>

</LinearLayout>

Expand Down
29 changes: 21 additions & 8 deletions app/src/main/res/layout/activity_welcome.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,28 @@

</FrameLayout>

<com.google.android.material.button.MaterialButton
android:id="@+id/okay"
android:layout_width="wrap_content"
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginHorizontal="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="5dp"
android:alpha="0.5"
android:text="@string/okay" />
android:layout_marginBottom="5dp">

<com.google.android.material.button.MaterialButton
android:id="@+id/restore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="@string/restore" />

<com.google.android.material.button.MaterialButton
android:id="@+id/okay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:alpha="0.5"
android:text="@string/okay" />

</FrameLayout>

</LinearLayout>