Skip to content

Commit

Permalink
Add news likes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethosa committed Sep 29, 2022
1 parent 99fb4fa commit 52697c2
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 31 deletions.
5 changes: 4 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ android {
minSdk 21
targetSdk 33
versionCode 29
versionName "0.10.0"
versionName "0.10.1"
ndk {
abiFilters 'armeabi-v7a','arm64-v8a','x86','x86_64'
}
Expand Down
36 changes: 20 additions & 16 deletions app/src/main/java/com/ethosa/ktc/college/CollegeApi.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package com.ethosa.ktc.college

import android.annotation.SuppressLint
import android.content.Context
import android.provider.Settings
import android.provider.Settings.*
import androidx.annotation.Keep
import com.ethosa.ktc.ui.dialog.AppUpdater
import okhttp3.Call
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response

/**
* Provides working with KTC api.
Expand All @@ -13,7 +20,6 @@ class CollegeApi {
private val client = OkHttpClient()

private const val API_URL = "http://api.kansk-tc.ru/"
private const val NEWS_API = "${API_URL}news/"
private const val ALBUMS_API = "${API_URL}albums/"

private const val MY_API = "http://mob.kansk-tc.ru/ktc-api"
Expand All @@ -25,6 +31,7 @@ class CollegeApi {
private const val ACTUAL_VERSION = "$MY_API/actual-version"
private const val LAST_NEWS = "$MY_API/news/"
private const val NEWS_BY_ID = "$MY_API/news/id"
private const val LIKE = "$MY_API/news/like"

/**
* Sends GET request to url.
Expand Down Expand Up @@ -73,21 +80,6 @@ class CollegeApi {
sendRequest("$COURSES/$branchId", callback)
}

/**
* Gets the last news and receives it in onResponse method
*/
fun fetchLastNews(callback: CollegeCallback) {
sendRequest(NEWS_API, callback)
}

/**
* Fetches the new by ID.
* @param id unique new's ID.
*/
fun fetchNewById(id: Int, callback: CollegeCallback) {
sendRequest("$NEWS_API$id", callback)
}

/**
* Fetches the timetable for specified group and week.
* @param groupId course group ID.
Expand Down Expand Up @@ -133,5 +125,17 @@ class CollegeApi {
fun fetchNewsByIdBeta(id: Int, callback: CollegeCallback) {
sendRequest("$NEWS_BY_ID$id", callback)
}

@SuppressLint("HardwareIds")
fun likeNewsById(id: Int, ctx: Context) {
sendRequest(
"$LIKE$id?uuid=${AppUpdater.UUID}",
object: CollegeCallback {
override fun onResponse(call: Call, response: Response) {
println(response.body?.string())
}
}
)
}
}
}
12 changes: 0 additions & 12 deletions app/src/main/java/com/ethosa/ktc/college/news/LastNews.kt

This file was deleted.

3 changes: 2 additions & 1 deletion app/src/main/java/com/ethosa/ktc/college/news/News.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ data class News(
var body: String,
val date: String,
var image: String,
val id: String
val id: String,
var likes: List<String>
)
21 changes: 21 additions & 0 deletions app/src/main/java/com/ethosa/ktc/ui/adapters/NewsAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ethosa.ktc.ui.adapters

import android.annotation.SuppressLint
import android.content.Intent
import android.content.res.Resources
import android.graphics.Bitmap
Expand All @@ -10,12 +11,14 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.ethosa.ktc.R
import com.ethosa.ktc.college.CollegeApi
import com.ethosa.ktc.ui.activities.WallPostActivity
import com.ethosa.ktc.college.news.News
import com.ethosa.ktc.databinding.LayoutNewsBinding
import com.ethosa.ktc.glide.GlideCallback
import com.ethosa.ktc.glide.transformation.CenterInsideBlur
import com.ethosa.ktc.glide.GlideListener
import com.ethosa.ktc.ui.dialog.AppUpdater
import java.lang.Exception

/**
Expand All @@ -42,6 +45,7 @@ class NewsAdapter(
* Loads image if available (without caching)
* Loads title, date and wall post text
*/
@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: ViewHolder, pos: Int) {
val binding = holder.binding
val new = items[pos]
Expand Down Expand Up @@ -71,6 +75,9 @@ class NewsAdapter(
binding.wallTitle.text = new.title
binding.wallText.text = new.body
binding.wallDate.text = new.date
binding.likeCount.text = new.likes.size.toString()
if (items[pos].likes.contains(AppUpdater.UUID))
binding.like.setImageResource(R.drawable.ic_heart_liked)

binding.root.setOnClickListener {
// Go to WallPostActivity
Expand All @@ -80,6 +87,20 @@ class NewsAdapter(
intent.putExtra("title", new.title)
binding.root.context.startActivity(intent)
}

binding.like.setOnClickListener {
CollegeApi.likeNewsById(new.id.toInt(), binding.root.context)
if (items[pos].likes.contains(AppUpdater.UUID)) {
binding.likeCount.text = (items[pos].likes.size - 1).toString()
items[pos].likes = items[pos].likes.minus(AppUpdater.UUID)
binding.like.setImageResource(R.drawable.ic_heart)
}
else {
binding.likeCount.text = (items[pos].likes.size + 1).toString()
items[pos].likes = items[pos].likes.plus(AppUpdater.UUID)
binding.like.setImageResource(R.drawable.ic_heart_liked)
}
}
}

override fun getItemCount() = items.size
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/ethosa/ktc/ui/dialog/AppUpdater.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.res.ResourcesCompat
Expand Down Expand Up @@ -36,6 +37,10 @@ class AppUpdater(
var omitted = "_omitted"

var actualVersion: ActualAppVersion? = null
var UUID = ""
}
init {
UUID = Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
}

// New version omitted
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_heart.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="@dimen/default_img_width"
android:height="@dimen/default_img_width"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@color/text"
android:pathData="M17.5,0.917a6.4,6.4 0,0 0,-5.5 3.3A6.4,6.4 0,0 0,6.5 0.917,6.8 6.8,0 0,0 0,7.967c0,6.775 10.956,14.6 11.422,14.932l0.578,0.409 0.578,-0.409C13.044,22.569 24,14.742 24,7.967A6.8,6.8 0,0 0,17.5 0.917ZM12,20.846c-3.253,-2.43 -10,-8.4 -10,-12.879a4.8,4.8 0,0 1,4.5 -5.05A4.8,4.8 0,0 1,11 7.967h2a4.8,4.8 0,0 1,4.5 -5.05A4.8,4.8 0,0 1,22 7.967C22,12.448 15.253,18.416 12,20.846Z"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_heart_liked.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="@dimen/default_img_width"
android:height="@dimen/default_img_width"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@color/primary"
android:pathData="M17.5,0.917a6.4,6.4 0,0 0,-5.5 3.3A6.4,6.4 0,0 0,6.5 0.917,6.8 6.8,0 0,0 0,7.967c0,6.775 10.956,14.6 11.422,14.932l0.578,0.409 0.578,-0.409C13.044,22.569 24,14.742 24,7.967A6.8,6.8 0,0 0,17.5 0.917ZM12,20.846c-3.253,-2.43 -10,-8.4 -10,-12.879a4.8,4.8 0,0 1,4.5 -5.05A4.8,4.8 0,0 1,11 7.967h2a4.8,4.8 0,0 1,4.5 -5.05A4.8,4.8 0,0 1,22 7.967C22,12.448 15.253,18.416 12,20.846Z"/>
</vector>
23 changes: 23 additions & 0 deletions app/src/main/res/layout/layout_news.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
app:layout_constraintGuide_begin="16dp" />

<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="0dp"
android:layout_height="256dp"
android:layout_marginTop="8dp"
Expand All @@ -78,6 +79,28 @@
android:layout_height="match_parent"
android:scaleType="fitCenter"
tools:ignore="ContentDescription" />

</androidx.cardview.widget.CardView>

<ImageView
android:id="@+id/like"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="@+id/left_padding"
app:layout_constraintTop_toBottomOf="@+id/cardView"
app:srcCompat="@drawable/ic_heart"
tools:ignore="ContentDescription" />

<TextView
android:id="@+id/like_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="0"
app:layout_constraintBottom_toBottomOf="@+id/like"
app:layout_constraintStart_toEndOf="@+id/like"
app:layout_constraintTop_toTopOf="@+id/like" />

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 52697c2

Please sign in to comment.