Skip to content

Commit

Permalink
Merge pull request #308 from fga-gpp-mds/feature/267
Browse files Browse the repository at this point in the history
Feature/267
  • Loading branch information
miguelpimentel authored May 21, 2018
2 parents e6c1b80 + 9fae353 commit 08b1e84
Show file tree
Hide file tree
Showing 44 changed files with 1,922 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class CommentsPresenter : CommentsPresentationLogic {
* Function that formatted the new comment wrote by user to send to View
* @param response
*/

override fun presentNewComment(response: CommentsModel.PublishCommentRequest.Response) {

val newComment = response.newComment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package com.nexte.nexte.Entities.Challenge

import java.util.*

object ChallengeMocker {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.nexte.nexte.Entities.Comment

import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
import java.util.*

open class Comment(@PrimaryKey var id: String? = null,
/**
* Class to define actions and details that involves comments which will be
* requested on server.
* @param id contains the id of the comment
* @param userId contains the id from the user which is commenting
* @param comment contains the comment
* @param date contains the date of the comment
* */
open class Comment(var id: String? = null,
var userId: String? = null,
var comment: String? = null,
var date: Date? = null): RealmObject() {
var date: Date? = null){

enum class ServerRequest(val request: Map<String, String>) {
COMMENTS(hashMapOf("route" to "comments", "method" to "get")),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.nexte.nexte.Entities.Comment

/**
* Interface to allow Comment Entity to assume different values according
* to Realm
*/
interface CommentAdapter {
fun getAll(): List<Comment>
fun get(identifier: String): Comment?
fun updateOrInsert(challenge: Comment): Comment?
fun delete(identifier: String): Comment?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.nexte.nexte.Entities.Comment
import io.realm.Realm
import io.realm.kotlin.where

/**
* Class to allow adaptations of data and sending to Realm.
* */
class CommentAdapterRealm : CommentAdapter {

var realm: Realm = Realm.getDefaultInstance()

override fun get(identifier: String): Comment? {
val commentRealm = realm.where<CommentRealm>().equalTo("id", identifier).findFirst()
return convertCommentRealmToComment(commentRealm!!)
}

override fun getAll(): List<Comment> {
val commentRealmResults = realm.where<CommentRealm>().findAll()
return convertListCommentRealmToCommentList(commentRealmResults)

}

override fun updateOrInsert(comment: Comment): Comment? {
convertCommentToCommentRealm(comment)?.let {
realm.beginTransaction()
realm.insertOrUpdate(it)
realm.commitTransaction()
return comment
}

return null
}

override fun delete(identifier: String): Comment? {

return null
}

private fun convertCommentRealmToComment(commentRealm: CommentRealm) : Comment {

val commentId = commentRealm.id

val userId = commentRealm.userId

val comment = commentRealm.comment

val date = commentRealm.date

val comments = Comment(id = commentId, userId = userId, comment = comment, date = date)
return comments
}

private fun convertListCommentRealmToCommentList(commentRealm: List<CommentRealm>) : List<Comment> {
val comments: MutableList<Comment> = mutableListOf()
for(commentRealm in commentRealm) {
convertCommentRealmToComment(commentRealm)?.let {
comments.add(it)
}
}

return comments.toList()
}

private fun convertCommentToCommentRealm(comment: Comment): CommentRealm {

val id = comment.id

val userId = comment.userId

val comments = comment.comment

val date = comment.date

val commentRealm = CommentRealm(id = id, userId = userId, comment = comments, date = date)
return commentRealm
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,57 @@
package com.nexte.nexte.Entities.Comment

class CommentManager {
fun createInitialMocker() {
/**
* Class define according to scene requests which kind of response will be displayed:
* data from remote repository or from data storage.
* @param commentAdapter hold information from CommentAdapterRealm
* */
class CommentManager(private val commentAdapter: CommentAdapter = CommentAdapterRealm()) {
fun get(identifier: String): Comment? {
return commentAdapter.get(identifier)
}

fun getAll(): List<Comment> {
return commentAdapter.getAll()
}

fun update(comment: Comment): Comment? {
return commentAdapter.updateOrInsert(comment)
}

fun updateMany(comments: List<Comment>): List<Comment> {
val newComments: MutableList<Comment> = mutableListOf()
for (comment in comments) {
val updatedComment = commentAdapter.updateOrInsert(comment)
updatedComment?.let {
newComments.add(it)
}
}
return newComments.toList()
}

fun delete(identifier: String): Comment? {
return commentAdapter.delete(identifier)
}

fun createInitialMocker(): List<Comment> {

val commentsInMocker = CommentMocker.allComments
val insertedComments: MutableList<Comment> = mutableListOf()

for (comment in commentsInMocker) {

val insertedComment = commentAdapter.updateOrInsert(comment)
insertedComment?.let {
insertedComments.add(it)
}
}
return insertedComments.toList()
}

fun createCommentsIds(amount: Int): List<String>{
return CommentMocker.generateComments(amount = amount)
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package com.nexte.nexte.Entities.Comment
import com.nexte.nexte.Entities.User.User
import com.nexte.nexte.Entities.User.UserAdapter
import com.nexte.nexte.Entities.User.UserManager
import java.util.*


/**
* Singleton responsible for mock comment data
*/
object CommentMocker{

/**
* @property commentTextMocks contains a list of mock comments
*/
private val commentTextMocks = listOf("Que jogo sensacional! " +
"Muita coisa ruim aconteceu mas eu sei que muita coisa ainda " +
"vai acontecer e por isso torço muito para que muita coisa" +
" continue acontecendo porque no final é tudo o que importa," +
" não é mesmo? Eu sei meu amigo." +
" As coisas mudam e tudo tem que mudar. A vida é assim!!!",
"Tá na hora da revanche!" +
" As coisas mudam e tudo tem que mudar. A vida é assim!!!",
"Você é muito pato",
"Arregou! Torço muito para que muita coisa continue" +
" acontecendo porque no final é tudo o que importa, não é mesmo?" +
" Eu sei meu amigo. As coisas mudam" +
" e tudo tem que mudar. A vida é assim!!!",
"Eu Não imaginava esse placar. ",
"Eu também acho isso. Eu sei meu amigo." +
" As coisas mudam e tudo tem que mudar. A vida é assim!!!",
"Mais seriedade",
"Não vou mais jogar contigo.",
"Cala a boca!")
var commentsId = 0
var userAdapter: UserAdapter? = null
var commentAdapter: CommentAdapter? = null
var allComments = mutableListOf<Comment>()

/**
* Function to generate random numbers
*/
private fun randomNumber(from: Int, to: Int): Int{
val random = Random()
println("BOUND: " + (to - from))

val bound = to - from

if (bound > 0){
return random.nextInt(bound) + from
}else{
return from
}
}

/**
* Function to generate random dates for all comments
*/
private fun generateRandomDate(): Date {
val year = randomNumber(2013, 2018)
val dayOfYear = randomNumber(1, 30)
val monthOfYear = randomNumber(1, 12)
val date = Date(year, monthOfYear, dayOfYear)
return date
}

/**
* Function to generate comments Ids
*/
private fun getCommentIds(amount: Int): List<String>{

val commentsIds = mutableListOf<String>()
for(counter in 0..amount - 1){
commentsIds.add(commentsId++.toString())
}

return commentsIds.toList()
}

/**
* Function to generate users Ids
*/
private fun getUsersIds(usersAmount: Int) : List<String> {
var users = listOf<User>()

if(userAdapter == null){
users = UserManager().getAll()
}else{
users = UserManager(userAdapter = userAdapter!!).getAll()
}

val usersIds = mutableListOf<String>()

println(users.size)
println(usersAmount)
for (counter in 0..usersAmount - 1){
println(counter)
usersIds.add(users[counter].id)
}

return usersIds
}

/**
* Function to generate a date for a comment
*/
private fun getDates(amount: Int): List<Date>{
val dates = mutableListOf<Date>()

for (counter in 0..amount - 1){
dates.add(generateRandomDate())
}

return dates
}

/**
* Function to get content of a comment
*/
private fun getComments(amount: Int): List<String>{
val comments = mutableListOf<String>()

for(counter in 0..amount - 1){
comments.add(commentTextMocks[counter])
}

return comments
}

/**
* Function to generate random data for comments which contains ids,
* userIds, dates and a comment from a user
*/
fun generateComments(amount: Int): List<String>{
val ids = getCommentIds(amount)
val userIds = getUsersIds(amount)
val dates = getDates(amount)
val comments = getComments(amount)

for (counter in 0..amount - 1){
val comment = Comment(ids[counter], userIds[counter], comments[counter], dates[counter])
allComments.add(comment)
}

return ids
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.nexte.nexte.Entities.Comment
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
import java.util.*

/**
* Class to define global data for the whole application.
* @param id contains the id of the comment
* @param userId contains the id from the user which is commenting
* @param comment contains the comment
* @param date contains the date of the comment
* */
open class CommentRealm(@PrimaryKey var id: String? = null,
var userId: String? = null,
var comment: String? = null,
var date: Date? = null): RealmObject() {

enum class ServerRequest(val request: Map<String, String>) {
COMMENTS(hashMapOf("route" to "comments", "method" to "get")),
ADD_COMMENT(hashMapOf("route" to "comment", "method" to "post")),
DELETE_COMMENT(hashMapOf("route" to "comment", "method" to "delete")),
REPORT_COMMENT(hashMapOf("route" to "report-comment", "method" to "post"))
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.nexte.nexte.Entities.Like

import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
import java.util.*

open class Like(@PrimaryKey var id: String? = null,
open class Like(var id: String? = null,
var userId: String? = null,
var date: Date? = null): RealmObject() {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.nexte.nexte.Entities.Like

interface LikeAdapter {
fun getAll(): List<Like>
fun get(identifier: String): Like?
fun updateOrInsert(like: Like): Like?
fun delete(identifier: String): Like?
}
Loading

0 comments on commit 08b1e84

Please sign in to comment.