Skip to content

Instantly share code, notes, and snippets.

View molidev8's full-sized avatar

Miguel Olivera molidev8

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / workerJob.kt
Created August 4, 2022 09:43
Specifiying the worker job
class BackupWorker(
context: Context,
workerParams: WorkerParameters
) : CoroutineWorker(context, workerParams) {
private val backup: BackupUserData = BackupUserData(applicationContext)
override suspend fun doWork(): Result {
return if (backup.doBackup()) Result.success()
else Result.failure()
@molidev8
molidev8 / workerLaunch.kt
Created August 4, 2022 09:03
Launching the periodic worker
BackupWorkerManager(requireContext()).launchWorker(interval)