Skip to content

Instantly share code, notes, and snippets.

View molidev8's full-sized avatar

Miguel Olivera molidev8

View GitHub Profile
@molidev8
molidev8 / dropboxLogin.kt
Last active August 16, 2022 16:46
Login into the Dropbox user account
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()
@molidev8
molidev8 / dropboxSetup.kt
Created August 16, 2022 16:51
Setting up the Dropbox connection
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(
@molidev8
molidev8 / dropboxData.kt
Created August 16, 2022 17:04
Managing data with the Dropbox SDK
/**
* 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)
@molidev8
molidev8 / doBackupRestore.kt
Last active August 30, 2022 10:39
Backing/restore of the SQL database
/**
* 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)
@molidev8
molidev8 / zip.kt
Last active August 30, 2022 10:45
Zip/Unzip of a file in Android
/**
* 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)
@molidev8
molidev8 / firebaseAuth.kt
Created September 17, 2022 07:42
Firebase Authentication
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)
}
@molidev8
molidev8 / firebaseFirestore.kt
Created September 17, 2022 07:59
Firebase Firestore
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
@molidev8
molidev8 / switch.xml
Created November 17, 2022 18:58
A customize Switch
<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" />
@molidev8
molidev8 / customThumb.xml
Created November 17, 2022 19:02
A custom thumb for a Switch view
<?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>
@molidev8
molidev8 / customTrack.xml
Created November 17, 2022 19:05
A custom track for a Switch view
<?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">