This file contains hidden or 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
fun initDropboxClient() { | |
val prefs = getSharedPreferences("recipe-vault", MODE_PRIVATE) | |
val serializedCredential = prefs.getString("credential", null) | |
if (serializedCredential == null) { | |
val credential = Auth.getDbxCredential() | |
if (credential != null) { | |
prefs.edit().putString("credential", credential.toString()).apply() |
This file contains hidden or 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
private val config: DbxRequestConfig = DbxRequestConfig.newBuilder("recipe-vault") | |
.withHttpRequestor(OkHttp3Requestor(OkHttp3Requestor.defaultOkHttpClient())) | |
.build() | |
private var client: DbxClientV2? = null | |
/** | |
* Launches the browser for the user to log in to Dropbox and get the credentials | |
*/ | |
fun startOAuth2Authentication() { | |
Auth.startOAuth2PKCE( |
This file contains hidden or 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
/** | |
* Uploads a file into the user's Dropbox | |
* @param input A [FileInputStream] to the file that is going to be uploaded | |
* @return true in case the upload went well, false otherwise | |
*/ | |
fun uploadFile(input: FileInputStream): Boolean { | |
return try { | |
client?.files()?.uploadBuilder("/recipe-vault-backup.zip") | |
?.withMode(WriteMode.OVERWRITE) | |
?.uploadAndFinish(input) |
This file contains hidden or 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
/** | |
* Initiates the backup process | |
* @return true in case the upload went well, false otherwise | |
*/ | |
fun doBackup(): Boolean = try { | |
LocalRecipeDatabase.getInstance(context).close() | |
val output = FileOutputStream(backupDir?.path + "/recipe-vault-backup.zip") | |
zipFiles(output, context.getDatabasePath(DATABASE_NAME), photosDir) | |
val zipFile = FileInputStream(backupDir?.listFiles()?.find { it.name == "recipe-vault-backup.zip" }) | |
uploadToDropbox(zipFile) |
This file contains hidden or 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
/** | |
* Compress all the files for the backup into a .zip file | |
* @param output A [FileOutputStream] where the zip file is going to be saved | |
* @param input The files that are going to be compressed into the .zip file | |
*/ | |
private fun zipFiles(output: FileOutputStream, vararg input: File?) { | |
val zipOutput = ZipOutputStream(BufferedOutputStream(output)) | |
val addZipEntry = { file: File -> | |
val entry = ZipEntry(file.name) |
This file contains hidden or 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
private val auth: FirebaseAuth = FirebaseAuth.getInstance() | |
// AuthState can be read to know if the user is logged in | |
val authState = object : LiveData<FirebaseUser>() { | |
private val authStateListener = FirebaseAuth.AuthStateListener { firebaseAuth -> | |
value = firebaseAuth.currentUser | |
} | |
override fun onActive() { | |
auth.addAuthStateListener(authStateListener) | |
} |
This file contains hidden or 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
private val auth: FirebaseAuth = FirebaseAuth.getInstance() | |
suspend fun getUser(): User? { | |
var user: UserFromFirebase? | |
try { | |
val documentSnapshot = db.collection(USERS).document(auth.uid).get().await() | |
user = documentSnapshot.toObject() | |
Log.d(FIRESTORE, "DocumentSnapshot data: ${documentSnapshot.data}") | |
} catch (e: Exception) { | |
user = null |
This file contains hidden or 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
<androidx.appcompat.widget.SwitchCompat | |
android:id="@+id/customThumbSmallSwitch" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:thumb="@drawable/thumb_small" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@id/customTrackSwitch" | |
app:track="@drawable/track" /> |
This file contains hidden or 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
<?xml version="1.0" encoding="utf-8"?> | |
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item android:state_checked="true"> | |
<shape android:shape="rectangle"> | |
<corners android:radius="20dp" /> | |
<solid android:color="@color/blue_800" /> | |
<size android:width="24dp" android:height="24dp" /> | |
<stroke android:width="4dp" android:color="@android:color/transparent" /> | |
</shape> | |
</item> |
This file contains hidden or 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
<?xml version="1.0" encoding="utf-8"?> | |
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item android:state_checked="true"> | |
<shape android:shape="rectangle"> | |
<solid android:color="@color/blue_300" /> | |
<corners android:radius="40dp" /> | |
<size android:width="48dp" android:height="24dp" /> | |
</shape> | |
</item> | |
<item android:state_checked="false"> |