Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/user activities screen #74

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ android {
branchNames = ["master"] // branch names from which you can deploy, master by default
remoteRepoName = "origin" // alias repository, origin by default
}

multiDexEnabled true
}
signingConfigs {
debug {
Expand Down Expand Up @@ -105,12 +107,19 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$versions.kotlin"
implementation 'com.google.code.gson:gson:2.8.6'
implementation "androidx.paging:paging-runtime-ktx:2.1.2"
implementation "com.google.android.material:material:1.2.1"
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
implementation 'com.android.support:multidex:1.0.3'
implementation "com.squareup.okhttp3:logging-interceptor:4.9.0"

implementation androidLibs
implementation apolloLibs
implementation coroutinesLibs
implementation kodeinLibs
implementation roomLibs
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
kapt compilerLibs

testImplementation unitTestLibs
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
<activity android:name=".activities.UserActivitiesActivity" />
<activity
android:name=".profile.ProfileActivity"
android:label="@string/app_name" />
Expand All @@ -31,4 +32,4 @@
</activity>
</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
query GetActivities($after: String, $before: String, $events: [ActivityEvent!], $first: Int, $last: Int) {
activities(
after: $after,
before: $before,
events: $events,
first: $first,
last: $last
) {
...ActivityConnectionFragment
}
}

fragment ActivityConnectionFragment on ActivityConnection {
edges {
node {
...ActivityFragment
}
}
pageInfo {
endCursor
hasNextPage
hasPreviousPage
startCursor
}
}


fragment ActivityFragment on Activity {
body,
createdAt,
event,
id,
title,
user {
firstName,
lastName,
email,
avatarUrl
}
}
9 changes: 9 additions & 0 deletions app/src/main/java/com/flatstack/android/Router.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.flatstack.android

import android.content.Context
import android.content.Intent
import com.flatstack.android.activities.UserActivitiesActivity
import com.flatstack.android.login.LoginActivity
import com.flatstack.android.profile.ProfileActivity

Expand All @@ -23,4 +24,12 @@ class Router(
}
})
}

fun activities(context: Context, clearStack: Boolean = false) {
context.startActivity(Intent(context, UserActivitiesActivity::class.java).apply {
if (clearStack) {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.flatstack.android.activities

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagedListAdapter
import com.flatstack.android.R

class ActivitiesAdapter :
PagedListAdapter<ActivitiesViewHolderModel, ActivitiesViewHolder>(ActivitiesDiffUtilCallback()) {

override fun onBindViewHolder(holder: ActivitiesViewHolder, position: Int) {
getItem(position)?.let { holder.bind(it) }
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ActivitiesViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.item_activity, parent, false)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.flatstack.android.activities

import androidx.recyclerview.widget.DiffUtil

class ActivitiesDiffUtilCallback : DiffUtil.ItemCallback<ActivitiesViewHolderModel>() {
override fun areItemsTheSame(
oldItem: ActivitiesViewHolderModel,
newItem: ActivitiesViewHolderModel
): Boolean = oldItem == newItem
ruslan-fayzullin marked this conversation as resolved.
Show resolved Hide resolved

override fun areContentsTheSame(
oldItem: ActivitiesViewHolderModel,
newItem: ActivitiesViewHolderModel
): Boolean = oldItem == newItem
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.flatstack.android.activities

import androidx.paging.DataSource
import androidx.paging.PageKeyedDataSource
import com.apollographql.apollo.ApolloClient
import com.apollographql.apollo.api.Input
import com.apollographql.apollo.coroutines.toDeferred
import com.flatstack.android.fragment.ActivityConnectionFragment.Edge
import com.flatstack.android.fragment.ActivityConnectionFragment.PageInfo
import com.flatstack.android.graphql.query.GetActivitiesQuery
import com.flatstack.android.type.ActivityEvent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class ActivitiesRepository(
private val apolloClient: ApolloClient
) {
fun getPagedUserActivities(coroutineScope: CoroutineScope, events: List<ActivityEvent>) =
object : DataSource.Factory<PageInfo, Edge>() {
override fun create(): DataSource<PageInfo, Edge> =
object : PageKeyedDataSource<PageInfo, Edge>() {
override fun loadInitial(
params: LoadInitialParams<PageInfo>,
callback: LoadInitialCallback<PageInfo, Edge>
) {
coroutineScope.launch(Dispatchers.IO) {
apolloClient.query(
GetActivitiesQuery(
events = Input.fromNullable(events),
ruslan-fayzullin marked this conversation as resolved.
Show resolved Hide resolved
first = Input.fromNullable(15)
)
).toDeferred()
.await().data?.activities?.fragments?.activityConnectionFragment?.let {
it.edges?.let { edges ->
callback.onResult(
edges,
null,
it.pageInfo
)
}
}
}
}

override fun loadAfter(
params: LoadParams<PageInfo>,
callback: LoadCallback<PageInfo, Edge>
) {
coroutineScope.launch(Dispatchers.IO) {
apolloClient.query(
GetActivitiesQuery(
after = Input.fromNullable(params.key.endCursor),
events = Input.fromNullable(events),
first = Input.fromNullable(15)
)
).toDeferred()
.await().data?.activities?.fragments?.activityConnectionFragment?.let {
it.edges?.let { edges ->
callback.onResult(
edges,
it.pageInfo
)
}
}
}
}

override fun loadBefore(
params: LoadParams<PageInfo>,
callback: LoadCallback<PageInfo, Edge>
) {
coroutineScope.launch(Dispatchers.IO) {
apolloClient.query(
GetActivitiesQuery(
before = Input.fromNullable(params.key.startCursor),
events = Input.fromNullable(events),
first = Input.fromNullable(15)
)
).toDeferred()
.await().data?.activities?.fragments?.activityConnectionFragment?.let {
it.edges?.let { edges ->
callback.onResult(
edges,
it.pageInfo
)
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.flatstack.android.activities

import android.view.View
import com.flatstack.android.R
import com.flatstack.android.util.recyclerview.BaseHolder
import kotlinx.android.synthetic.main.item_activity.view.*
import java.text.SimpleDateFormat
import java.util.*

class ActivitiesViewHolder(itemView: View) : BaseHolder<ActivitiesViewHolderModel>(itemView) {

override fun bind(item: ActivitiesViewHolderModel) {
with(containerView) {
tv_event.text = item.title
tv_body.text = item.body
tv_created_at.text = SimpleDateFormat("HH:mm, dd MMM yyyy", Locale.ENGLISH).format(
SimpleDateFormat("yyyy-mm-dd'T'HH:MM:ss'Z'", Locale.ENGLISH).parse(item.createdAt)
)
tv_username.text = itemView.context.getString(
R.string.username_mask,
item.user.firstName,
item.user.lastName
)
tv_id.text = itemView.context.getString(R.string.id_mask, item.id)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.flatstack.android.activities

import com.flatstack.android.profile.entities.Profile
import com.flatstack.android.type.ActivityEvent

data class ActivitiesViewHolderModel(
val body: String,
val createdAt: String,
val event: ActivityEvent,
val id: String,
ruslan-fayzullin marked this conversation as resolved.
Show resolved Hide resolved
val title: String,
val user: Profile
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.flatstack.android.activities

import androidx.lifecycle.*
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import com.flatstack.android.model.entities.Resource
import com.flatstack.android.profile.entities.Profile
import com.flatstack.android.type.ActivityEvent
import com.flatstack.android.type.ActivityEvent.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart

class ActivitiesViewModel(
private val activitiesRepository: ActivitiesRepository
) : ViewModel() {

private val events = MutableLiveData<List<ActivityEvent>>()

init {
events.postValue(
listOf(
RESET_PASSWORD_REQUESTED,
USER_LOGGED_IN,
USER_REGISTERED,
USER_RESET_PASSWORD,
USER_UPDATED
)
)
}

val activities: LiveData<Resource<PagedList<ActivitiesViewHolderModel?>>> =
events.switchMap { events ->
LivePagedListBuilder(
activitiesRepository.getPagedUserActivities(viewModelScope, events)
.mapByPage { edges ->
edges.map { edge ->
edge.node?.fragments?.activityFragment?.let { fragment ->
ruslan-fayzullin marked this conversation as resolved.
Show resolved Hide resolved
ActivitiesViewHolderModel(
body = fragment.body,
createdAt = fragment.createdAt.toString(),
event = fragment.event,
id = fragment.id,
title = fragment.title,
user = Profile(
firstName = fragment.user.firstName ?: "",
lastName = fragment.user.lastName ?: ""
)
)
}
}
},
PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPageSize(15)
.build()
).build().asFlow()
.flowOn(Dispatchers.IO)
.map { Resource.success(it) }
.onStart { emit(Resource.loading()) }
.catch { error ->
error.message?.let {
emit(Resource.error(it))
}
}
.asLiveData(viewModelScope.coroutineContext)
}
}
Loading