Code Monkey home page Code Monkey logo

fbremotedb's Introduction

FbRemoteDb

Simple access to Firebase Realtime Database.

Everything is inside a Bucket, there you can store your items. This is for simplifying the access to your data.

You can create a Bucket with a name, a password and a list of BucketItem. You can join a Bucket with a name, a password.

1. How to use in code

2. How to setup project

3. Limitations

4. Troubleshooting

5. Last words

6. License

How to use in code

Define a class for the content of the Buckets, which extends BucketItem and provides an empty constructor.

data class MyData(override val id: String, val secretCodes: List<String>) : BucketItem {
    constructor() : this("", emptyList())
}

Now you can get the repository and use it, e.g. in a ViewModel.

class AViewModel : ViewModel() {
    private val fbRepo by lazy {
        FbRemoteDb(MyData::class.java).apply {
            version = "1" // Optional
        }
    }

    fun createBuckets() {
        viewModelScope.launch {
            // TODO: Here do, what you need:  fbRepo. 
        }

    }
}

The signatures are like this. Everything throws a NoUidException if there is no with FirebaseAuth logged in user. Additionally, it is your responsibility to ensure that the correct user is logged in.

@Throws(NoUidException::class)
suspend fun deleteBucketForUser(name: String): Boolean

@Throws(NoUidException::class)
suspend fun fetchBucketsList(): List<Bucket<T>>

@Throws(NoUidException::class)
suspend fun fetchBucket(name: String): Bucket<T>?

@Throws(NoUidException::class)
suspend fun updateBucket(bucket: Bucket<T>): Boolean

@Throws(NoUidException::class)
suspend fun createBucket(name: String, password: String, bucketList: List<T>): Boolean

@Throws(NoUidException::class)
suspend fun joinPublicBucket(name: String, password: String): Boolean

@Throws(NoUidException::class)
suspend fun getBucketAsFlow(name: String): Flow<Bucket<T>>?

@Throws(NoUidException::class)
suspend fun getTAsFlow(name: String, id: String): Flow<T>?

How to setup project

  1. Add following to settings.gradle:
pluginManagement {
    plugins {
        id 'com.google.gms.google-services' version '4.3.10'
    }
}
dependencyResolutionManagement {
    repositories {
        maven { url "https://jitpack.io" }
    }
}
  1. Add following to app/build.gradle:
plugins {
    id 'com.google.gms.google-services'
}
dependencies {
    implementation 'com.github.betafx:FbRemoteDb:{latest_version}'
}
  1. Register your app on https://console.firebase.google.com. Download google-services.json into project.

  2. Create a new Realtime Database on https://console.firebase.google.com. Change the rules to this:

{
  "rules": {
    ".read": "false",
    ".write": "false",
    "user": {
      ".read": "false",
      ".write": "false",
      "$user_id": {
        ".write": "$user_id === auth.uid",
        ".read": "$user_id === auth.uid"
      }
    },
    "buckets": {
      ".read": "false",
      ".write": "false",
      "$bucket": {
        ".read": "false",
        ".write": "false",
        "$version": {
          ".read": "false",
          ".write": "false",
          "$pw": {
            ".read": "auth.uid != null",
            ".write": "auth.uid != null"
          }
        }
      }
    }
  }
}

Limitations

  • Do not use this for sensitive data, seriously.
  • No login mechanism, the right user has to be logged in or nothing will work.
  • No server-side validation of data.
  • No encryption of the data stored on the Firebase-Server. Data in database is visible for you. Implement your custom encryption and store the key on the device.
  • Do not edit the database online on https://console.firebase.google.com, I don't know what will happen. Maybe your app will crash.

Troubleshooting

java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.app.myapplication. Make sure to call FirebaseApp.initializeApp(Context) first.

You forgot to add id 'com.google.gms.google-services' in app/build.gradle.

Execution failed for task ':app:processDebugGoogleServices'.
> File google-services.json is missing. The Google Services Plugin cannot function without it.

See step 3 in "How to setup project"

Last words

I was annoyed to move the same code over again, when using it in my own projects, so I decided to move it here. I am happy if you contribute, fork, clone or whatever to this repo, also opening issues is really appreciated. Feel free to contact my and happy coding :)

License

Copyright 2021 betafx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

fbremotedb's People

Contributors

betafx avatar

Stargazers

Sebastian Schäf avatar

Watchers

 avatar

fbremotedb's Issues

Change using of classname as identifier

The trick for linking butcketItems agains classes will not work with obfuscation, needs to be changed.
See String.hashed()-extension: clazz.canonicalName?.filter { it.isLetter() }

Create private buckets

Private buckets must not have a password and can be saved in the user db. Migrate to public buckets with adding a password should also be possible.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.