Skip to content

Commit

Permalink
Merge pull request #47 from horaciocome1/bug-fixes-and-improvements-p…
Browse files Browse the repository at this point in the history
…art-VI

Bug fixes and improvements part vi
  • Loading branch information
horaciocome1 authored Aug 9, 2019
2 parents 486d79e + 0c2a271 commit 495c93a
Show file tree
Hide file tree
Showing 36 changed files with 626 additions and 251 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ android {
applicationId "io.github.horaciocome1.reaque"
minSdkVersion 16
targetSdkVersion 29
versionCode 12
versionName "1.3.0-rc-21"
versionCode 13
versionName "1.4.0-rc-10"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
multiDexEnabled true
Expand Down
64 changes: 50 additions & 14 deletions app/src/main/java/io/github/horaciocome1/reaque/data/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.github.horaciocome1.reaque.data

import io.github.horaciocome1.reaque.data.bookmarks.BookmarksService
import io.github.horaciocome1.reaque.data.configurations.ConfigurationsService
import io.github.horaciocome1.reaque.data.feed.FeedService
import io.github.horaciocome1.reaque.data.posts.PostsService
import io.github.horaciocome1.reaque.data.ratings.RatingsService
Expand All @@ -28,26 +29,61 @@ import io.github.horaciocome1.reaque.data.users.UsersService

class Database private constructor() {

var bookmarksService = BookmarksService()
var feedsService = FeedService()
var postsService = PostsService()
var ratingsService = RatingsService()
var readingsService = ReadingsService()
var sharesService = SharesService()
var storageService = StorageService()
var subscriptionsService = SubscriptionsService()
var topicsService = TopicsService()
var usersService = UsersService()
val bookmarksService: BookmarksService by lazy {
BookmarksService()
}

val feedsService: FeedService by lazy {
FeedService()
}

val postsService: PostsService by lazy {
PostsService()
}

val ratingsService: RatingsService by lazy {
RatingsService()
}

val readingsService: ReadingsService by lazy {
ReadingsService()
}

val sharesService: SharesService by lazy {
SharesService()
}

val storageService: StorageService by lazy {
StorageService()
}

val subscriptionsService: SubscriptionsService by lazy {
SubscriptionsService()
}

val topicsService: TopicsService by lazy {
TopicsService()
}

val usersService: UsersService by lazy {
UsersService()
}

val configurationsService: ConfigurationsService by lazy {
ConfigurationsService()
}

companion object {

@Volatile
private var instance: Database? = null

fun getInstance() = instance
?: synchronized(this) {
instance ?: Database().also { instance = it }
}
fun getInstance() = instance ?: synchronized(this) {
instance ?: Database()
.also {
instance = it
}
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package io.github.horaciocome1.reaque.data.bookmarks
import com.google.android.gms.tasks.Task
import io.github.horaciocome1.reaque.data.posts.Post

class BookmarksRepository private constructor(private val service: BookmarksService) : BookmarksInterface {
class BookmarksRepository private constructor(
private val service: BookmarksService
) : BookmarksInterface {

override fun bookmark(post: Post, onCompleteListener: (Task<Void?>?) -> Unit) =
service.bookmark(post, onCompleteListener)
Expand All @@ -22,10 +24,12 @@ class BookmarksRepository private constructor(private val service: BookmarksServ
@Volatile
private var instance: BookmarksRepository? = null

fun getInstance(service: BookmarksService) = instance
?: synchronized(this) {
instance ?: BookmarksRepository(service).also { instance = it }
}
fun getInstance(service: BookmarksService) = instance ?: synchronized(this) {
instance ?: BookmarksRepository(service)
.also {
instance = it
}
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ import io.github.horaciocome1.reaque.util.posts

class BookmarksService : BookmarksInterface {

private val db: FirebaseFirestore by lazy { FirebaseFirestore.getInstance() }
private val db: FirebaseFirestore by lazy {
FirebaseFirestore.getInstance()
}

private val auth: FirebaseAuth by lazy { FirebaseAuth.getInstance() }
private val auth: FirebaseAuth by lazy {
FirebaseAuth.getInstance()
}

private val increment: Map<String, FieldValue> by lazy {
val increment = FieldValue.increment(1)
Expand Down Expand Up @@ -74,9 +78,11 @@ class BookmarksService : BookmarksInterface {
posts.value?.let { list ->
if (list.isEmpty())
auth.addSimpleAuthStateListener { user ->
val ref = db.collection("users/${user.uid}/bookmarks")
ref.orderBy("score", Query.Direction.DESCENDING).limit(100)
.get().addOnSuccessListener {
db.collection("users/${user.uid}/bookmarks")
.orderBy("score", Query.Direction.DESCENDING)
.limit(100)
.get()
.addOnSuccessListener {
posts.value = it.posts
}
}
Expand All @@ -88,24 +94,22 @@ class BookmarksService : BookmarksInterface {
isBookmarked.value = false
if (post.id.isNotBlank())
auth.addSimpleAuthStateListener { user ->
val ref = db.document("users/${user.uid}/bookmarks/${post.id}")
ref.addSimpleSnapshotListener {
isBookmarked.value = it["title"] != null
}
db.document("users/${user.uid}/bookmarks/${post.id}")
.addSimpleSnapshotListener {
isBookmarked.value = it["title"] != null
}
}
return isBookmarked
}

override fun hasBookmarks(): LiveData<Boolean> {
hasBookmarks.value = false
auth.addSimpleAuthStateListener { user ->
val ref = db.document("users/${user.uid}")
ref.addSimpleSnapshotListener {
val bookmarks = it["bookmarks"]
hasBookmarks.value = if (bookmarks != null)
bookmarks.toString().toInt() > 0
else
false
db.document("users/${user.uid}")
.addSimpleSnapshotListener { snapshot ->
snapshot["bookmarks"]?.let {
hasBookmarks.value = it.toString().toInt() > 0
}
}
}
return hasBookmarks
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.horaciocome1.reaque.data.configurations

import androidx.lifecycle.LiveData

interface ConfigurationsInterface {

fun isUpdateAvailable(): LiveData<Boolean>

fun getLatestVersionName(): LiveData<String>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.github.horaciocome1.reaque.data.configurations

class ConfigurationsRepository private constructor(
private val service: ConfigurationsService
) : ConfigurationsInterface {

override fun isUpdateAvailable() = service.isUpdateAvailable()

override fun getLatestVersionName() = service.getLatestVersionName()

companion object {

@Volatile
private var instance: ConfigurationsRepository? = null

fun getInstance(service: ConfigurationsService) = instance ?: synchronized(this) {
instance ?: ConfigurationsRepository(service)
.also {
instance = it
}
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.github.horaciocome1.reaque.data.configurations

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.firebase.firestore.FirebaseFirestore
import io.github.horaciocome1.reaque.BuildConfig
import io.github.horaciocome1.reaque.util.addSafeSnapshotListener

class ConfigurationsService : ConfigurationsInterface {

private val versionCode: Int by lazy {
BuildConfig.VERSION_CODE
}

private val db: FirebaseFirestore by lazy {
FirebaseFirestore.getInstance()
}

private val isUpdateAvailable: MutableLiveData<Boolean> by lazy {
MutableLiveData<Boolean>()
.apply {
value = false
}
}

private val latestVersionName: MutableLiveData<String> by lazy {
MutableLiveData<String>()
.apply {
value = ""
}
}

override fun isUpdateAvailable(): LiveData<Boolean> {
isUpdateAvailable.value = false
db.document("configurations/default")
.addSafeSnapshotListener { snapshot ->
snapshot["version_code"]?.let {
isUpdateAvailable.value = versionCode < it.toString().toInt()
}
}
return isUpdateAvailable
}

override fun getLatestVersionName(): LiveData<String> {
db.document("configurations/default")
.addSafeSnapshotListener { snapshot ->
snapshot["version_name"]?.let {
latestVersionName.value = it.toString()
}
}
return latestVersionName
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.horaciocome1.reaque.data.feed

class FeedRepository private constructor(private val service: FeedService) : FeedInterface {
class FeedRepository private constructor(
private val service: FeedService
) : FeedInterface {

override fun get() = service.get()

Expand All @@ -9,10 +11,12 @@ class FeedRepository private constructor(private val service: FeedService) : Fee
@Volatile
private var instance: FeedRepository? = null

fun getInstance(service: FeedService) = instance
?: synchronized(this) {
instance ?: FeedRepository(service).also { instance = it }
}
fun getInstance(service: FeedService) = instance ?: synchronized(this) {
instance ?: FeedRepository(service)
.also {
instance = it
}
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import io.github.horaciocome1.reaque.util.posts

class FeedService : FeedInterface {

private val db: FirebaseFirestore by lazy { FirebaseFirestore.getInstance() }
private val db: FirebaseFirestore by lazy {
FirebaseFirestore.getInstance()
}

private val auth: FirebaseAuth by lazy { FirebaseAuth.getInstance() }
private val auth: FirebaseAuth by lazy {
FirebaseAuth.getInstance()
}

private var _posts = mutableListOf<Post>()

Expand All @@ -24,9 +28,11 @@ class FeedService : FeedInterface {
override fun get(): LiveData<List<Post>> {
if (_posts.isEmpty())
auth.addSimpleAuthStateListener { user ->
val ref = db.collection("users/${user.uid}/feed")
ref.orderBy("score", Query.Direction.DESCENDING).limit(100)
.get().addOnSuccessListener {
db.collection("users/${user.uid}/feed")
.orderBy("score", Query.Direction.DESCENDING)
.limit(100)
.get()
.addOnSuccessListener {
_posts = it.posts
posts.value = _posts
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import com.google.android.gms.tasks.Task
import io.github.horaciocome1.reaque.data.topics.Topic
import io.github.horaciocome1.reaque.data.users.User

class PostsRepository private constructor(private val service: PostsService) : PostsInterface {
class PostsRepository private constructor(
private val service: PostsService
) : PostsInterface {

override fun create(post: Post, onCompleteListener: (Task<Void?>?) -> Unit) =
service.create(post, onCompleteListener)
Expand All @@ -22,10 +24,12 @@ class PostsRepository private constructor(private val service: PostsService) : P
@Volatile
private var instance: PostsRepository? = null

fun getInstance(service: PostsService) = instance
?: synchronized(this) {
instance ?: PostsRepository(service).also { instance = it }
}
fun getInstance(service: PostsService) = instance ?: synchronized(this) {
instance ?: PostsRepository(service)
.also {
instance = it
}
}

}

Expand Down
Loading

0 comments on commit 495c93a

Please sign in to comment.