Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Commit

Permalink
ThreadScreen: Implement posts
Browse files Browse the repository at this point in the history
Signed-off-by: Aayush Gupta <[email protected]>
  • Loading branch information
theimpulson committed Aug 26, 2023
1 parent 744652d commit ce26ecb
Show file tree
Hide file tree
Showing 17 changed files with 499 additions and 8 deletions.
12 changes: 12 additions & 0 deletions app/src/main/java/io/aayush/relabs/network/XenforoInterface.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package io.aayush.relabs.network
import io.aayush.relabs.network.data.alert.Alerts
import io.aayush.relabs.network.data.conversation.Conversations
import io.aayush.relabs.network.data.node.Nodes
import io.aayush.relabs.network.data.thread.ThreadInfo
import io.aayush.relabs.network.data.thread.Threads
import io.aayush.relabs.network.data.user.Me
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query

interface XenforoInterface {
Expand Down Expand Up @@ -51,4 +53,14 @@ interface XenforoInterface {

@GET("threads/audapp-watched/")
suspend fun getWatchedThreads(): Threads

@GET("threads/{id}")
suspend fun getThreadInfo(
@Path("id") id: Int,
@Query("with_posts") with_posts: Boolean? = null,
@Query("page") page: Int? = null,
@Query("with_first_post") with_first_post: Boolean? = null,
@Query("with_last_post") with_last_post: Boolean? = null,
@Query("order") order: String? = null
): ThreadInfo
}
19 changes: 19 additions & 0 deletions app/src/main/java/io/aayush/relabs/network/XenforoRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.aayush.relabs.network
import io.aayush.relabs.network.data.alert.Alerts
import io.aayush.relabs.network.data.conversation.Conversations
import io.aayush.relabs.network.data.node.Nodes
import io.aayush.relabs.network.data.thread.ThreadInfo
import io.aayush.relabs.network.data.thread.Threads
import io.aayush.relabs.network.data.user.Me
import javax.inject.Inject
Expand Down Expand Up @@ -71,4 +72,22 @@ class XenforoRepository @Inject constructor(
suspend fun getWatchedThreads(): Threads {
return xenforoInterface.getWatchedThreads()
}

suspend fun getThreadInfo(
id: Int,
with_posts: Boolean? = null,
page: Int? = null,
with_first_post: Boolean? = null,
with_last_post: Boolean? = null,
order: String? = null
): ThreadInfo {
return xenforoInterface.getThreadInfo(
id,
with_posts,
page,
with_first_post,
with_last_post,
order
)
}
}
17 changes: 17 additions & 0 deletions app/src/main/java/io/aayush/relabs/network/data/post/Attachment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.aayush.relabs.network.data.post

data class Attachment(
val attach_date: Int = 0,
val attachment_id: Int = 0,
val content_id: Int = 0,
val content_type: String = String(),
val direct_url: String = String(),
val file_size: Int = 0,
val filename: String = String(),
val height: Int = 0,
val is_audio: Boolean = false,
val is_video: Boolean = false,
val thumbnail_url: String = String(),
val view_count: Int = 0,
val width: Int = 0
)
31 changes: 31 additions & 0 deletions app/src/main/java/io/aayush/relabs/network/data/post/Post.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.aayush.relabs.network.data.post

import io.aayush.relabs.network.data.user.User

data class Post(
val Attachments: List<Attachment> = emptyList(),
val User: User = User(),
val attach_count: Int = 0,
val can_edit: Boolean = false,
val can_hard_delete: Boolean = false,
val can_react: Boolean = false,
val can_soft_delete: Boolean = false,
val can_view_attachments: Boolean = false,
val is_first_post: Boolean = false,
val is_last_post: Boolean = false,
val is_reacted_to: Boolean = false,
val is_unread: Boolean = false,
val last_edit_date: Int = 0,
val message: String = String(),
val message_parsed: String = String(),
val message_state: String = String(),
val position: Int = 0,
val post_date: Int = 0,
val post_id: Int = 0,
val reaction_score: Int = 0,
val thread_id: Int = 0,
val user_id: Int = 0,
val username: String = String(),
val view_url: String = String(),
val warning_message: String = String()
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.aayush.relabs.network.data.thread

import io.aayush.relabs.network.data.common.Pagination
import io.aayush.relabs.network.data.post.Post

data class ThreadInfo(
val pagination: Pagination = Pagination(),
val posts: List<Post> = emptyList(),
val thread: Thread = Thread()
)
140 changes: 140 additions & 0 deletions app/src/main/java/io/aayush/relabs/ui/components/PostItem.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package io.aayush.relabs.ui.components

import android.os.Build
import android.text.format.DateUtils
import android.text.util.Linkify
import androidx.compose.foundation.background
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.text.HtmlCompat
import coil.ImageLoader
import coil.compose.SubcomposeAsyncImage
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import coil.request.ImageRequest
import com.google.android.material.textview.MaterialTextView
import io.aayush.relabs.R
import io.aayush.relabs.network.data.post.Post
import io.aayush.relabs.utils.DesignQuoteSpan
import io.aayush.relabs.utils.LinkTransformationMethod
import io.aayush.relabs.utils.formatBlockQuotes
import java.util.Date

@Composable
fun PostItem(
modifier: Modifier,
post: Post,
linkTransformationMethod: LinkTransformationMethod,
designQuoteSpan: DesignQuoteSpan,
onClicked: () -> Unit = {}
) {
Box(
modifier = modifier
.fillMaxWidth()
.clip(RoundedCornerShape(10.dp))
.background(
color = if (isSystemInDarkTheme()) {
MaterialTheme.colorScheme.onSecondary
} else {
MaterialTheme.colorScheme.secondary
}
)
) {
Column(
modifier = Modifier.padding(10.dp),
verticalArrangement = Arrangement.spacedBy(10.dp, Alignment.Top),
horizontalAlignment = Alignment.Start
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Start
) {
SubcomposeAsyncImage(
model = ImageRequest.Builder(LocalContext.current)
.data(post.User.avatar_urls.values.first() ?: R.drawable.ic_account)
.placeholder(R.drawable.ic_account)
.crossfade(true)
.build(),
imageLoader = ImageLoader.Builder(LocalContext.current)
.components {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
add(ImageDecoderDecoder.Factory())
} else {
add(GifDecoder.Factory())
}
}.build(),
contentDescription = "",
contentScale = ContentScale.Crop,
modifier = Modifier
.requiredSize(64.dp)
.clip(CircleShape)
)
Spacer(modifier = Modifier.width(10.dp))
Column(
verticalArrangement = Arrangement.Top,
horizontalAlignment = Alignment.Start
) {
Text(
text = post.User.username,
fontSize = 15.sp,
fontWeight = FontWeight.Bold,
color = Color.White
)
Text(text = post.User.user_title, fontSize = 13.sp, color = Color.White)
Text(
text = DateUtils.getRelativeTimeSpanString(
post.post_date.toLong() * 1000L,
Date().time,
DateUtils.MINUTE_IN_MILLIS
).toString(),
fontSize = 12.sp,
fontWeight = FontWeight.Light,
color = Color.White
)
}
}
AndroidView(
modifier = modifier,
factory = {
MaterialTextView(it).apply {
autoLinkMask = Linkify.WEB_URLS
linksClickable = true
transformationMethod = linkTransformationMethod
setLinkTextColor(Color.White.toArgb())
}
},
update = {
it.setTextColor(Color.White.toArgb())
it.text = HtmlCompat.fromHtml(
post.message_parsed,
HtmlCompat.FROM_HTML_MODE_COMPACT
).formatBlockQuotes(designQuoteSpan)
}
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.aayush.relabs.ui.components

import android.os.Build
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
Expand Down Expand Up @@ -41,7 +43,9 @@ fun ThreadPreviewItem(
onClicked: () -> Unit = {}
) {
Row(
modifier = modifier,
modifier = modifier
.fillMaxWidth()
.clickable { onClicked() },
verticalAlignment = Alignment.Top,
horizontalArrangement = Arrangement.Start
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fun SetupNavGraph(
}
)
) {
ThreadScreen(navHostController, it.arguments!!.getInt(NavArg.THREAD_ID.name).toString())
ThreadScreen(navHostController, it.arguments!!.getInt(NavArg.THREAD_ID.name))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import androidx.navigation.NavHostController
import io.aayush.relabs.R
import io.aayush.relabs.network.data.thread.Thread
import io.aayush.relabs.ui.components.ThreadPreviewItem
import io.aayush.relabs.ui.navigation.Screen
import kotlinx.coroutines.launch

@Composable
Expand Down Expand Up @@ -98,7 +99,10 @@ fun HomeScreen(navHostController: NavHostController, viewModel: HomeViewModel =
totalReplies = thread.reply_count,
views = thread.view_count,
lastReplyDate = thread.last_post_date,
forum = thread.Forum.title
forum = thread.Forum.title,
onClicked = {
navHostController.navigate(Screen.Thread.withID(thread.thread_id))
}
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.aayush.relabs.ui.screens.news

import android.net.Uri
import android.util.Log
import androidx.browser.customtabs.CustomTabsIntent
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -124,8 +123,7 @@ fun NewsScreen(navHostController: NavHostController, viewModel: NewsViewModel =
date = article.pubDate ?: "",
onClicked = {
try {
CustomTabsIntent.Builder()
.build()
viewModel.customTabsIntent
.launchUrl(context, Uri.parse(article.link))
} catch (exception: Exception) {
Log.e(TAG, "Failed to open profile", exception)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.aayush.relabs.ui.screens.news

import androidx.browser.customtabs.CustomTabsIntent
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.prof.rssparser.Article
Expand All @@ -12,7 +13,8 @@ import javax.inject.Inject

@HiltViewModel
class NewsViewModel @Inject constructor(
private val rssNewsRepository: RSSNewsRepository
private val rssNewsRepository: RSSNewsRepository,
val customTabsIntent: CustomTabsIntent
) : ViewModel() {

private val _mobileFeed = MutableStateFlow<List<Article>>(emptyList())
Expand Down
Loading

0 comments on commit ce26ecb

Please sign in to comment.