Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NT-1956: Thread view and navigation (#1262)
- Loading branch information
Showing
9 changed files
with
255 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
app/src/main/java/com/kickstarter/ui/activities/ThreadActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.kickstarter.ui.activities | ||
|
||
import android.os.Bundle | ||
import com.kickstarter.databinding.ActivityThreadLayoutBinding | ||
import com.kickstarter.libs.BaseActivity | ||
import com.kickstarter.libs.KSString | ||
import com.kickstarter.libs.qualifiers.RequiresActivityViewModel | ||
import com.kickstarter.libs.utils.DateTimeUtils | ||
import com.kickstarter.models.Comment | ||
import com.kickstarter.ui.extensions.hideKeyboard | ||
import com.kickstarter.viewmodels.ThreadViewModel | ||
import rx.android.schedulers.AndroidSchedulers | ||
|
||
@RequiresActivityViewModel(ThreadViewModel.ViewModel::class) | ||
class ThreadActivity : BaseActivity<ThreadViewModel.ViewModel>() { | ||
|
||
private lateinit var binding: ActivityThreadLayoutBinding | ||
private lateinit var ksString: KSString | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
binding = ActivityThreadLayoutBinding.inflate(layoutInflater) | ||
setContentView(binding.root) | ||
ksString = environment().ksString() | ||
|
||
this.viewModel.getRootComment() | ||
.compose(bindToLifecycle()) | ||
.observeOn(AndroidSchedulers.mainThread()) | ||
.subscribe { comment -> | ||
configureRootCommentView(comment) | ||
} | ||
|
||
this.viewModel.shouldFocusOnCompose() | ||
.compose(bindToLifecycle()) | ||
.observeOn(AndroidSchedulers.mainThread()) | ||
.subscribe { shouldOpenKeyboard -> | ||
// TODO: Once compose view is integrated we can set focus and open the keyboard | ||
} | ||
} | ||
|
||
override fun onStop() { | ||
super.onStop() | ||
hideKeyboard() | ||
} | ||
|
||
private fun configureRootCommentView(comment: Comment) { | ||
binding.commentsCardView.setCommentUserName(comment.author().name()) | ||
binding.commentsCardView.setCommentBody(comment.body()) | ||
binding.commentsCardView.setCommentPostTime(DateTimeUtils.relative(this, ksString, comment.createdAt())) | ||
binding.commentsCardView.setCommentUserName(comment.author().name()) | ||
binding.commentsCardView.setAvatarUrl(comment.author().avatar().medium()) | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
app/src/main/java/com/kickstarter/viewmodels/ThreadViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.kickstarter.viewmodels | ||
|
||
import androidx.annotation.NonNull | ||
import com.kickstarter.libs.ActivityViewModel | ||
import com.kickstarter.libs.CurrentUserType | ||
import com.kickstarter.libs.Environment | ||
import com.kickstarter.libs.utils.ObjectUtils | ||
import com.kickstarter.models.Comment | ||
import com.kickstarter.services.ApolloClientType | ||
import com.kickstarter.ui.IntentKey | ||
import com.kickstarter.ui.activities.ThreadActivity | ||
import rx.Observable | ||
import rx.subjects.BehaviorSubject | ||
|
||
interface ThreadViewModel { | ||
|
||
interface Inputs | ||
interface Outputs { | ||
/** The anchored root comment */ | ||
fun getRootComment(): Observable<Comment> | ||
|
||
/** Will tell to the compose view if should open the keyboard */ | ||
fun shouldFocusOnCompose(): Observable<Boolean> | ||
} | ||
|
||
class ViewModel(@NonNull val environment: Environment) : ActivityViewModel<ThreadActivity>(environment), Inputs, Outputs { | ||
private val apolloClient: ApolloClientType = environment.apolloClient() | ||
private val currentUser: CurrentUserType = environment.currentUser() | ||
|
||
private val rootComment = BehaviorSubject.create<Comment>() | ||
private val focusOnCompose = BehaviorSubject.create<Boolean>() | ||
val inputs = this | ||
val outputs = this | ||
|
||
init { | ||
intent() | ||
.map { it.getParcelableExtra(IntentKey.COMMENT) as Comment? } | ||
.distinctUntilChanged() | ||
.filter { ObjectUtils.isNotNull(it) } | ||
.map { requireNotNull(it) } | ||
.compose(bindToLifecycle()) | ||
.subscribe(this.rootComment) | ||
|
||
intent() | ||
.map { it.getBooleanExtra(IntentKey.REPLY_EXPAND, false) } | ||
.distinctUntilChanged() | ||
.compose(bindToLifecycle()) | ||
.subscribe(this.focusOnCompose) | ||
} | ||
|
||
override fun getRootComment(): Observable<Comment> = this.rootComment | ||
override fun shouldFocusOnCompose(): Observable<Boolean> = this.focusOnCompose | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.coordinatorlayout.widget.CoordinatorLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:id="@+id/root" | ||
tools:context="com.kickstarter.ui.activities.ThreadActivity"> | ||
|
||
<com.google.android.material.appbar.AppBarLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
app:layout_constraintStart_toStartOf="parent"> | ||
|
||
<com.kickstarter.ui.toolbars.KSToolbar | ||
style="@style/Toolbar" | ||
app:contentInsetLeft="0dp" | ||
app:contentInsetStart="0dp"> | ||
|
||
<RelativeLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:gravity="center_vertical" | ||
android:orientation="horizontal"> | ||
|
||
<com.kickstarter.ui.views.IconButton | ||
android:id="@+id/back_button" | ||
style="@style/ToolbarIconBackButton" /> | ||
|
||
<TextView | ||
style="@style/ToolbarTitle" | ||
android:text="@string/fpo_replies" /> | ||
</RelativeLayout> | ||
</com.kickstarter.ui.toolbars.KSToolbar> | ||
</com.google.android.material.appbar.AppBarLayout> | ||
|
||
<androidx.constraintlayout.widget.ConstraintLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:focusable="true" | ||
tools:context="com.kickstarter.ui.activities.ThreadActivity" | ||
app:layout_behavior="@string/appbar_scrolling_view_behavior" | ||
android:orientation="vertical"> | ||
|
||
<com.kickstarter.ui.views.CommentCard | ||
android:id="@+id/comments_card_view" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
</androidx.coordinatorlayout.widget.CoordinatorLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
app/src/test/java/com/kickstarter/viewmodels/ThreadViewModelTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.kickstarter.viewmodels | ||
|
||
import android.content.Intent | ||
import com.kickstarter.KSRobolectricTestCase | ||
import com.kickstarter.libs.Environment | ||
import com.kickstarter.mock.factories.AvatarFactory | ||
import com.kickstarter.mock.factories.CommentFactory | ||
import com.kickstarter.models.Comment | ||
import com.kickstarter.ui.IntentKey | ||
import org.junit.Test | ||
import rx.observers.TestSubscriber | ||
|
||
class ThreadViewModelTest : KSRobolectricTestCase() { | ||
|
||
private lateinit var vm: ThreadViewModel.ViewModel | ||
private val getComment = TestSubscriber<Comment>() | ||
private val focusCompose = TestSubscriber<Boolean>() | ||
|
||
private fun setUpEnvironment() { | ||
setUpEnvironment(environment()) | ||
} | ||
|
||
private fun setUpEnvironment(environment: Environment) { | ||
this.vm = ThreadViewModel.ViewModel(environment) | ||
this.vm.getRootComment().subscribe(getComment) | ||
this.vm.shouldFocusOnCompose().subscribe(focusCompose) | ||
} | ||
|
||
@Test | ||
fun testGetRootComment() { | ||
setUpEnvironment() | ||
|
||
val comment = CommentFactory.comment(avatar = AvatarFactory.avatar()) | ||
|
||
this.vm.intent(Intent().putExtra(IntentKey.COMMENT, comment)) | ||
getComment.assertValue(comment) | ||
|
||
this.vm.intent(Intent().putExtra("Some other Key", comment)) | ||
getComment.assertValue(comment) | ||
} | ||
|
||
@Test | ||
fun testShouldFocusCompose() { | ||
setUpEnvironment() | ||
|
||
this.vm.intent(Intent().putExtra(IntentKey.REPLY_EXPAND, false)) | ||
focusCompose.assertValue(false) | ||
|
||
this.vm.intent(Intent().putExtra("Some other Key", false)) | ||
focusCompose.assertValues(false) | ||
|
||
this.vm.intent(Intent().putExtra(IntentKey.REPLY_EXPAND, true)) | ||
focusCompose.assertValues(false, true) | ||
} | ||
} |