Skip to content

Commit

Permalink
Merge pull request #42 from StepicOrg/release/1.9
Browse files Browse the repository at this point in the history
Release/1.9
  • Loading branch information
KirillMakarov committed Jun 5, 2016
2 parents ba2004b + 9701412 commit 95907f7
Show file tree
Hide file tree
Showing 90 changed files with 2,097 additions and 113 deletions.
10 changes: 6 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.neenbedankt.android-apt' //needed for source code generation

android {
Expand All @@ -11,8 +12,8 @@ android {
applicationId "org.stepic.droid"
minSdkVersion 14
targetSdkVersion 23
versionCode 54
versionName "1.8.3"
versionCode 56
versionName "1.9.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// Enabling multidex support.
multiDexEnabled true
Expand Down Expand Up @@ -97,7 +98,8 @@ dependencies {
compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'

compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.facebook.fresco:fresco:0.9.0'
compile 'com.facebook.fresco:fresco:0.10.0'
compile 'com.facebook.fresco:animated-gif:0.10.0'

compile 'joda-time:joda-time:2.8'

Expand All @@ -117,7 +119,7 @@ dependencies {

// Required -- JUnit 4 framework
testCompile 'junit:junit:4.12'
// Optional -- Mockito frameworkвв ьфермшуц фы вузутвутсн
// Optional -- Mockito framework
testCompile 'org.mockito:mockito-core:1.10.19'

debugCompile 'com.squareup.leakcanary:leakcanary-android:1.4-beta2' // or 1.3.1
Expand Down
29 changes: 26 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
android:largeHeap="true"
android:supportsRtl="false"
android:theme="@style/AppTheme"
tools:replace = "android:allowBackup"
tools:replace="android:allowBackup"
>

<activity
Expand Down Expand Up @@ -72,7 +72,7 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".view.activities.MainFeedActivity"/>

</activity>во скольк
</activity>

<activity
android:name=".view.activities.UnitsActivity"
Expand All @@ -82,7 +82,8 @@
<activity
android:name=".view.activities.StepsActivity"
android:hardwareAccelerated="false"
android:label="@string/steps_title"/>
android:label="@string/steps_title"
android:launchMode="singleTop"/>

<receiver
android:name="org.stepic.droid.receivers.DownloadCompleteReceiver"
Expand Down Expand Up @@ -212,5 +213,27 @@
<action android:name="notification_cancelled"/>
</intent-filter>
</receiver>

<activity
android:name=".view.activities.CommentsActivity"
android:label="@string/comments_title"
android:launchMode="singleTop"
android:parentActivityName=".view.activities.StepsActivity">
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".view.activities.StepsActivity"/>
</activity>

<activity
android:name=".view.activities.NewCommentActivity"
android:label="@string/new_comment_title"
android:windowSoftInputMode="adjustResize|stateHidden"
android:parentActivityName=".view.activities.CommentsActivity">
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".view.activities.CommentsActivity"/>
</activity>
</application>
</manifest>
231 changes: 231 additions & 0 deletions app/src/main/java/org/stepic/droid/core/CommentManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
package org.stepic.droid.core

import com.squareup.otto.Bus

import org.stepic.droid.base.MainApplication
import org.stepic.droid.events.comments.CommentsLoadedSuccessfullyEvent
import org.stepic.droid.events.comments.InternetConnectionProblemInCommentsEvent
import org.stepic.droid.model.CommentAdapterItem
import org.stepic.droid.model.User
import org.stepic.droid.model.comments.Comment
import org.stepic.droid.model.comments.DiscussionProxy
import org.stepic.droid.web.CommentsResponse
import org.stepic.droid.web.IApi
import retrofit.Callback
import retrofit.Response
import retrofit.Retrofit
import java.util.*

import javax.inject.Inject

class CommentManager {

@Inject
lateinit var bus: Bus

@Inject
lateinit var api: IApi

private val maxOfParentInQuery = 10 // server supports 20, but we can change it
private val maxOfRepliesInQuery = 20 // we can't change it
private var sumOfCachedParent: Int = 0;
private var discussionProxy: DiscussionProxy? = null
private val parentCommentToSumOfCachedReplies: MutableMap<Long, Int> = HashMap()
private val cachedCommentsSetMap: MutableMap<Long, Comment> = HashMap()
private val cachedCommentsList: MutableList<Comment> = ArrayList()
private val userSetMap: MutableMap<Int, User> = HashMap() //userId -> User
private val replyToPositionInParentMap: MutableMap<Long, Int> = HashMap()
private val parentIdToPositionInDiscussionMap: MutableMap<Long, Int> = HashMap()
private val repliesIdIsLoading: MutableSet<Long> = HashSet()
private val commentIdIsLoading: MutableSet<Long> = HashSet() //can be reply or comment (with 0 replies) for load more comments).

@Inject
constructor() {
MainApplication.component().inject(this)
}

fun loadComments() {
val orderOfComments = discussionProxy?.discussions
orderOfComments?.let {
val sizeNeedLoad = Math.min((sumOfCachedParent + maxOfParentInQuery), orderOfComments.size)
if (sizeNeedLoad == sumOfCachedParent || sizeNeedLoad == 0) {
// we don't need to load comments
bus.post(CommentsLoadedSuccessfullyEvent()) // notify UI
return
}

val idsForLoading = it.subList(sumOfCachedParent, sizeNeedLoad).toLongArray()
loadCommentsByIds(idsForLoading)
}
}

fun loadExtraReplies(oneOfReplyComment: Comment) {
val parentCommentId = oneOfReplyComment.parent
val parentComment = cachedCommentsSetMap[parentCommentId]
if (parentComment != null && parentComment.replies != null) {
val countOfCachedReplies: Int = parentCommentToSumOfCachedReplies[parentCommentId] ?: return

val sizeNeedLoad = Math.min(parentComment.replies.size, countOfCachedReplies + maxOfRepliesInQuery)
if (sizeNeedLoad == countOfCachedReplies || sizeNeedLoad == 0) {
bus.post(CommentsLoadedSuccessfullyEvent()) // notify UI
return
}

val idsForLoading = parentComment.replies.subList(countOfCachedReplies, sizeNeedLoad).toLongArray()
loadCommentsByIds(idsForLoading, fromReply = true)
}
}

fun loadCommentsByIds(idsForLoading: LongArray, fromReply: Boolean = false) {
api.getCommentsByIds(idsForLoading).enqueue(object : Callback<CommentsResponse> {
override fun onResponse(response: Response<CommentsResponse>?, retrofit: Retrofit?) {

if (response != null && response.isSuccess) {
val stepicResponse = response.body()
if (stepicResponse != null) {
stepicResponse.comments
?.forEach {
if (it.id != null) {
val previousValue: Comment? = cachedCommentsSetMap.put(it.id, it)
val parentId: Long? = it.parent
if (parentId != null && previousValue == null) {
//first time
var numberOfCachedBefore: Int = parentCommentToSumOfCachedReplies[parentId] ?: 0
numberOfCachedBefore++
parentCommentToSumOfCachedReplies[parentId] = numberOfCachedBefore
}
}
}
sumOfCachedParent = cachedCommentsSetMap.filter { it.value.parent == null }.size

cachedCommentsList.clear()
var i = 0
var j = 0
while (i < sumOfCachedParent) {
val parentCommentId = discussionProxy?.discussions?.get(j)
j++

val parentComment = cachedCommentsSetMap[parentCommentId] ?: break
cachedCommentsList.add(parentComment)
i++
if (parentComment.replies != null && !parentComment.replies.isEmpty()) {
var childIndex = 0
val cachedRepliesNumber = parentCommentToSumOfCachedReplies.get(parentComment.id) ?: 0
while (childIndex < cachedRepliesNumber) {
val childComment = cachedCommentsSetMap [parentComment.replies[childIndex]] ?: break
replyToPositionInParentMap.put(childComment.id!!, childIndex)
cachedCommentsList.add(childComment)
childIndex++
}
}
}

stepicResponse.users
?.forEach {
if (it.id !in userSetMap) {
userSetMap.put(it.id, it)
}
}
//commentIdIsLoading = commentIdIsLoading.filterNot { cachedCommentsSetMap.containsKey(it) }.toHashSet()
if (fromReply) {
repliesIdIsLoading.clear()
} else {
commentIdIsLoading.clear()
}
bus.post(CommentsLoadedSuccessfullyEvent()) // notify UI
} else {
bus.post(InternetConnectionProblemInCommentsEvent(discussionProxy!!.id))
}
} else {
bus.post(InternetConnectionProblemInCommentsEvent(discussionProxy!!.id))
}
}

override fun onFailure(t: Throwable?) {
bus.post(InternetConnectionProblemInCommentsEvent(discussionProxy!!.id))
}
})
}

fun getSize() = cachedCommentsList.size

fun getItemWithNeedUpdatingInfoByPosition(position: Int): CommentAdapterItem {
val comment: Comment = cachedCommentsList[position]
return getCommentAndNeedUpdateBase(comment)
}

private fun getCommentAndNeedUpdateBase(comment: Comment): CommentAdapterItem {
var needUpdate = false
val parentComment: Comment? = cachedCommentsSetMap[comment.parent] //comment.parent can be null

if (parentComment == null) {
//comment is parent comment
val positionInDiscussion = parentIdToPositionInDiscussionMap[comment.id]!!
if (discussionProxy!!.discussions.size > sumOfCachedParent && (positionInDiscussion + 1) == sumOfCachedParent) {
needUpdate = true
}

} else {
//comment is reply
val pos: Int = replyToPositionInParentMap[comment.id]!!
val numberOfCached = parentCommentToSumOfCachedReplies[parentComment.id]
if ((pos + 1) == numberOfCached && parentComment.reply_count ?: 0 > numberOfCached) {
needUpdate = true
}
}

val isLoading = repliesIdIsLoading.contains(comment.id)
val isParentLoading = commentIdIsLoading.contains(comment.id)
return CommentAdapterItem(isNeedUpdating = needUpdate, isLoading = isLoading, comment = comment, isParentLoading = isParentLoading)
}

fun addToLoading(commentId: Long) {
repliesIdIsLoading.add(commentId)
}

fun getUserById(userId: Int) = userSetMap[userId]

fun isNeedUpdateParentInReply(commentReply: Comment): Boolean {
val positionInParent = replyToPositionInParentMap[commentReply.id]
if (discussionProxy!!.discussions.size > sumOfCachedParent) {
//need update parent:
//and it is last cached reply?
val positionInParent = replyToPositionInParentMap[commentReply.id]
val numberOfCached = parentCommentToSumOfCachedReplies[commentReply.parent]
val posInDisscussion = parentIdToPositionInDiscussionMap[commentReply.parent] ?: return false
if (numberOfCached != null && positionInParent != null && (positionInParent + 1) == numberOfCached && (posInDisscussion + 1) == sumOfCachedParent) {
return true
}
}
return false
}

fun setDiscussionProxy(dP: DiscussionProxy) {
discussionProxy = dP
//todo: remove dp from manager, make only list in discussion proxy based on sorting and discussion id!
var i = 0
while (i < dP.discussions.size) {
parentIdToPositionInDiscussionMap.put(dP.discussions[i], i)
i++
}
}

fun addCommentIdWhereLoadMoreClicked(commentId: Long) {
commentIdIsLoading.add(commentId)
}

fun isEmpty() = cachedCommentsList.isEmpty()

fun reset() {
sumOfCachedParent = 0
}

fun isCommentCached(commentId: Long?) : Boolean {
if (commentId == null) {
return false
} else {
return cachedCommentsSetMap.containsKey(commentId)
}
}

}
6 changes: 6 additions & 0 deletions app/src/main/java/org/stepic/droid/core/IScreenManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.support.v7.app.AppCompatActivity;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.stepic.droid.model.Course;
import org.stepic.droid.model.Lesson;
import org.stepic.droid.model.Section;
Expand All @@ -24,6 +25,10 @@ public interface IScreenManager {

void showCourseDescription(Fragment sourceActivity, @NotNull Course course);

void openComments(Context context, String discussionProxyId, long stepId);

void openNewCommentForm(Activity sourceActivity, Long target, @Nullable Long parent);

void showSections(Context sourceActivity, @NotNull Course course);

void showUnitsForSection(Context sourceActivity, @NotNull Section section);
Expand All @@ -49,4 +54,5 @@ public interface IScreenManager {
void showVideo(Activity sourceActivity, String source);

void showSettings(Activity sourceActivity);

}
2 changes: 1 addition & 1 deletion app/src/main/java/org/stepic/droid/core/LoginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private void failLogin(Throwable t) {
if (t instanceof ProtocolException) {
Toast.makeText(mContext, R.string.failLogin, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(mContext, R.string.failLoginConnectionProblems, Toast.LENGTH_LONG).show();
Toast.makeText(mContext, R.string.connectionProblems, Toast.LENGTH_LONG).show();
}
}
}
Expand Down
Loading

0 comments on commit 95907f7

Please sign in to comment.