Skip to content

Commit

Permalink
initial commit v2
Browse files Browse the repository at this point in the history
  • Loading branch information
horaciocome1 committed Jun 5, 2021
1 parent f917282 commit 23863ef
Show file tree
Hide file tree
Showing 51 changed files with 1,191 additions and 1 deletion.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 91 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,91 @@
"# fireflow"
# fireflow
[![](https://jitpack.io/v/horaciocome1/fireflow.svg)](https://jitpack.io/#horaciocome1/fireflow) [![License](https://img.shields.io/badge/license-Apache%202-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0) [![API](https://img.shields.io/badge/API-19%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=19)

## Getting Started
Android library that aims to hide Firebase Firestore listener implementation and expose only a Kotlin Flow of specified type. ;)
Works for ColletionReferences, Queries, and DocumentReferences.

## Pre-requisites
Check the pre-requisites for Firebase Firestore and for Kotlin Flow on respective documentation.
Be familiar with Kotlin Coroutines.
Based on versions **23.0.1** and **1.4.1** of _firebase-firestore-ktx_ and _kotlinx-coroutines-play-services_ respectively

## Adding to your project
Lets start by adding a corresponding repository in your _root_ `build.gradle` file.
```gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```
The next task is to add the dependency to your _app_ `build.gradle` file.
```gradle
dependencies {
...
implementation 'com.github.horaciocome1:fireflow:0.0.1'
}
```
Now you ready to go. You might want to _**sync your project**_ first. ;)

## How to use
### without fireflow
```kotlin
val db = FirebaseFirestore.getInstance()
val ref = db.collection("posts")
ref.addSnapshotListener { snapshot, error ->
if (error != null) {
// handle
} else if (snapshot != null) {
val posts = snapshot.toObjects(Post::class.java)
// set posts to UI
}
}
```
### with fireflow
```kotlin
val db = FirebaseFirestore.getInstance()
db.collection("posts").getAsFlow<Post>().collect { posts ->
// set posts to UI
}
```
You can also read documents as flows
```kotlin
val db = FirebaseFirestore.getInstance()
db.collection("posts").document("1")
.getAsFlow<Post>().collect { post ->
// set post to UI
}
```

## Troubleshooting
There is no Java support.
For other things please open an Issue or reach me via [[email protected]](mailto:[email protected])

## Licenses
Copyright 2021 Horácio Flávio Comé Júnior

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
### Not maintained by us
Firebase Firestore is a product of Google.
Kotlin is a product of Jetbrains.

## How to contribute
We open to suggestions of any kind.
Email me, open pull requests, etc.

## Less Boilerplate Utils
If you want to see another utility please check my work on Recyclerview.
[Simple RecyclerView Listener](https://github.com/horaciocome1/simple-recyclerview-listener)
[Simple RecyclerView Adapter](https://github.com/horaciocome1/simple-recyclerview-adapter)
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
44 changes: 44 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
}

android {
compileSdkVersion 30

defaultConfig {
applicationId "io.github.horaciocome1.firestore_flow"
minSdkVersion 19
targetSdkVersion 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}

dependencies {
implementation 'androidx.core:core-ktx:1.5.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.google.firebase:firebase-firestore-ktx:23.0.1'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.4.1"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.horaciocome1.firestore_flow

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("io.github.horaciocome1.firestore_flow", appContext.packageName)
}
}
21 changes: 21 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.github.horaciocome1.firestore_flow">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Firestoreflow">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.github.horaciocome1.firestore_flow

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.Query
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.collect

class MainActivity : AppCompatActivity() {

class Post

class User

class Topic

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}

// common way
fun getPostsA() {
val db = FirebaseFirestore.getInstance()
val ref = db.collection("posts")
ref.addSnapshotListener { snapshot, error ->
if (error != null) {
// handle
} else if (snapshot != null) {
val posts = snapshot.toObjects(Post::class.java)
// set posts to UI
}
}
}

// with fireflow
@ExperimentalCoroutinesApi
suspend fun getPostsB() {
val db = FirebaseFirestore.getInstance()
db.collection("posts").getAsFlow<Post>().collect { posts ->
// set posts to UI
}
}

// with fireflow document
@ExperimentalCoroutinesApi
suspend fun getPost() {
val db = FirebaseFirestore.getInstance()
db.collection("posts")
.document("1")
.getAsFlow<Post>().collect { post ->
// set post to UI
}
}

}
Loading

0 comments on commit 23863ef

Please sign in to comment.