Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
41 changes: 24 additions & 17 deletions app/src/main/java/com/example/reddittube/data/DataRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -167,22 +167,32 @@ class DefaultDataRepository(private val context: Context) : DataRepository {
val comments = mutableListOf<RedditComment>()
try {
val url = "https://oauth.reddit.com/r/$subreddit/comments/$postId.json?limit=100&sort=top&raw_json=1"
val conn = URL(url).openConnection() as HttpURLConnection
conn.requestMethod = "GET"
conn.setRequestProperty("Authorization", "Bearer $token")
conn.setRequestProperty("User-Agent", RedditOAuthHelper.getUserAgent(context))
conn.setRequestProperty("Connection", "keep-alive")
conn.connectTimeout = 8000
conn.readTimeout = 8000
val raw = withContext(Dispatchers.IO) {
val conn = URL(url).openConnection() as HttpURLConnection
conn.requestMethod = "GET"
conn.setRequestProperty("Authorization", "Bearer $token")
conn.setRequestProperty("User-Agent", RedditOAuthHelper.getUserAgent(context))
conn.setRequestProperty("Connection", "keep-alive")
conn.connectTimeout = 8000
conn.readTimeout = 8000

val code = conn.responseCode
if (code == 200) {
val stream = if ("gzip".equals(conn.contentEncoding, ignoreCase = true)) {
java.util.zip.GZIPInputStream(conn.inputStream)
val code = conn.responseCode
val responseRaw = if (code == 200) {
val stream = if ("gzip".equals(conn.contentEncoding, ignoreCase = true)) {
java.util.zip.GZIPInputStream(conn.inputStream)
} else {
conn.inputStream
}
stream.bufferedReader(Charsets.UTF_8).use { it.readText() }
} else {
conn.inputStream
Log.w("RedditRepository", "Comments HTTP $code for $url")
null
}
val raw = stream.bufferedReader(Charsets.UTF_8).use { it.readText() }
conn.disconnect()
responseRaw
}

if (raw != null) {
val jsonArray = org.json.JSONArray(raw)
if (jsonArray.length() > 1) {
val commentListing = jsonArray.getJSONObject(1)
Expand Down Expand Up @@ -210,11 +220,8 @@ class DefaultDataRepository(private val context: Context) : DataRepository {
}
}
}
} else {
Log.w("RedditRepository", "Comments HTTP $code for $url")
comments.sortByDescending { it.score }
}
conn.disconnect()
comments.sortByDescending { it.score }
} catch (e: Exception) {
Log.e("RedditRepository", "fetchPostComments error: ${e.message}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

Expand Down
Loading