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

#19 liks #9

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,6 @@ dependencies {

implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
implementation "androidx.coordinatorlayout:coordinatorlayout:1.1.0"

implementation 'com.github.bumptech.glide:glide:4.11.0'
}
Binary file removed app/src/main/assets/VID_20190615_173334.mp4
Binary file not shown.
Binary file added app/src/main/assets/VID_20190831_213015.mp4
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import com.example.android_curse.data.network.request.SignInWithEmailRequest
import com.example.android_curse.data.network.response.VerificationTokenResponse
import com.example.android_curse.data.network.response.error.*
import com.example.android_curse.entity.AuthTokens
import com.example.android_curse.entity.Lakes
import com.example.android_curse.entity.Post
import com.example.android_curse.entity.User
import com.haroldadmin.cnradapter.NetworkResponse
import com.squareup.moshi.Json

class MockApi : Api {
override suspend fun getUsers(): NetworkResponse<List<User>, Unit> {
Expand Down Expand Up @@ -61,6 +63,20 @@ class MockApi : Api {
}

override suspend fun getPosts(): NetworkResponse<List<Post>, Unit> {
TODO("Not yet implemented")
return NetworkResponse.Success(
listOf(
Post(
id = 7,
linkUrl = null,
imageUrl = null,
title = "aaaaaaaaaaaaaa",
text = null,
createAt = "",
updateAt = "",
likes = Lakes(listOf(),15)
)
),
code=200
)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.android_curse.ui.news

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import by.kirich1409.viewbindingdelegate.viewBinding
import com.example.android_curse.R
Expand All @@ -10,4 +12,11 @@ class NewsFragment : BaseFragment(R.layout.fragment_news) {
private val viewBinding by viewBinding(FragmentNewsBinding::bind)

private val viewModel: NewsViewModel by viewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val url =
"https://icatcare.org/app/uploads/2018/07/Thinking-of-getting-a-cat.png"
viewBinding.likes.urls = listOf(url).flatMap { listOf(it, it, it, it, it, it) }
.flatMap { listOf(it, it, it, it, it, it) } // 25
super.onViewCreated(view, savedInstanceState)
}
}
120 changes: 120 additions & 0 deletions app/src/main/java/com/example/android_curse/ui/news/PostLikesView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.example.android_curse.ui.news


//import androidx.transition.Transition

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.Gravity
import android.widget.FrameLayout
import android.widget.TextView
import androidx.core.view.isInvisible
import com.bumptech.glide.Glide
import com.example.android_curse.R
import com.example.android_curse.util.dpToPx
import com.example.android_curse.util.getCompatColor
import com.example.android_curse.util.inflate
import kotlinx.coroutines.*


class PostLikesView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleRes: Int = 0
) : FrameLayout(context, attrs, defStyleRes) {

private var textView: TextView

private var colorRadius: Float = dpToPx(10f)

var urls: List<String> = listOf()

private var visibleColorCount: Int = 0
private var collapsedColorCount: Int = 0

private var circlePaint: Paint =
Paint().apply {
strokeWidth = colorRadius
}
private var circleBorderPaint: Paint =
Paint()
.apply {
color = context.getCompatColor(R.color.color_post_like_border)
strokeWidth = dpToPx(2f)
style = Paint.Style.STROKE
}

init {
setWillNotDraw(false)
textView = (inflate(R.layout.item_post_remaining_likes_count, false) as TextView)
.also { textView ->
addView(
textView,
LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,
Gravity.END or Gravity.CENTER_VERTICAL
)
)
}
//
setCollapsedColorCountText(urls.size)
}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
setCollapsedColorCountText(urls.size)
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
if (urls.isNotEmpty()) {
val widthOfOneColor = (colorRadius * 3).toInt()
val widthOfAllColors = urls.size * widthOfOneColor
val widthWithoutPadding = measuredWidth - paddingStart - paddingEnd
var availableSpace = widthWithoutPadding
if (availableSpace in 1..widthOfAllColors) {
var visibleColorCount = availableSpace / widthOfOneColor + 1
do {
visibleColorCount--
setCollapsedColorCountText(urls.size - visibleColorCount)
measureChild(textView, widthMeasureSpec, heightMeasureSpec)
availableSpace = widthWithoutPadding - textView.measuredWidth
} while (0 < visibleColorCount && availableSpace <= visibleColorCount * widthOfOneColor)
textView.isInvisible = false
this.visibleColorCount = visibleColorCount
collapsedColorCount = urls.size - visibleColorCount
} else {
textView.isInvisible = true
visibleColorCount = urls.size
collapsedColorCount = 0
}
}
}

override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
val yPos = height / 2f
var xPos = paddingStart + colorRadius
for (i in 0 until visibleColorCount) {
canvas.drawCircle(xPos, yPos, colorRadius, circleBorderPaint)
MainScope().launch(Dispatchers.IO){
val theBitmap: Bitmap = Glide.with(this@PostLikesView)
.asBitmap()
.load(urls[i])
// .load(R.drawable.ic_baseline_person_24)
// .load("https://i.pinimg.com/originals/c6/e7/91/c6e7913a6ee055acf1ce60084968f40c.jpg")
.circleCrop()
.submit((colorRadius*2).toInt(), (colorRadius*2).toInt())
.get()
canvas.drawBitmap(theBitmap, xPos-colorRadius, yPos-colorRadius, null)
}
Thread.sleep(200) // если не подождать canvas умерает
xPos += (colorRadius * 3)
}
}

private fun setCollapsedColorCountText(count: Int) {
textView.text = resources.getString(R.string.common_counter_prefix, count)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.example.android_curse.ui.onbording

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.viewpager2.widget.ViewPager2
import by.kirich1409.viewbindingdelegate.viewBinding
Expand All @@ -17,22 +19,16 @@ import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.hannesdorfmann.adapterdelegates4.ListDelegationAdapter
import dev.chrisbanes.insetter.applyInsetter
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

class OnbordingFragment : BaseFragment(R.layout.fragment_onbording) {

private val viewBinding by viewBinding(FragmentOnbordingBinding::bind)
// private lateinit var binding: FragmentOnbordingBinding

private var player: ExoPlayer? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
private val viewModel: OnbordingViewModel by viewModels()

// binding = FragmentOnbordingBinding.inflate(layoutInflater)
private val viewBinding by viewBinding(FragmentOnbordingBinding::bind)

//TODO()
// viewBinding.playerView.player = player!! //TODO()
}
private fun player(): ExoPlayer = viewBinding.playerView.player as ExoPlayer

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand All @@ -45,44 +41,60 @@ class OnbordingFragment : BaseFragment(R.layout.fragment_onbording) {
type(navigationBars = true) { margin() }
}

player = SimpleExoPlayer.Builder(requireContext()).build().apply {
addMediaItem(MediaItem.fromUri("asset:///VID_20190615_173334.mp4"))
viewBinding.playerView.player = SimpleExoPlayer.Builder(requireContext()).build().apply {
addMediaItem(MediaItem.fromUri("asset:///VID_20190831_213015.mp4"))
repeatMode = Player.REPEAT_MODE_ALL
prepare()
}

viewBinding.playerView.player = player!!

viewBinding.viewPager.setTextPages()
viewBinding.viewPager.attachDots(viewBinding.onboardingTextTabLayout)

viewBinding.signInButton.setOnClickListener {
// TODO()
findNavController().navigate(R.id.action_onbordingFragment_to_signInFragment)
Toast.makeText(requireContext(), "нажата текста войти", Toast.LENGTH_SHORT).show()
}
viewBinding.signUpButton.setOnClickListener {
findNavController().navigate(R.id.action_onbordingFragment_to_signUpFragment)

// TODO()
Toast.makeText(requireContext(), "нажата текста регистрации", Toast.LENGTH_SHORT).show()
}

viewBinding.volumeControlButton.setOnClickListener { viewModel.change() }
lifecycleScope.launch {
viewModel.isOffVolumeControlStateFlow().collect { value: Boolean ->
if (value) {
player().volume = 0f
viewBinding.volumeControlButton.setImageDrawable(
ResourcesCompat.getDrawable(
resources,
R.drawable.ic_volume_off_white_24dp,
requireContext().theme
)
)
} else {
player().volume = 100f
viewBinding.volumeControlButton.setImageDrawable(
ResourcesCompat.getDrawable(
resources,
R.drawable.ic_volume_on_white_24dp,
requireContext().theme
)
)
}
}
}
}

override fun onResume() {
super.onResume()
player?.play()
viewBinding.playerView.player?.play()
}

override fun onPause() {
super.onPause()
player?.pause()
viewBinding.playerView.player?.pause()
}

override fun onDestroy() {
super.onDestroy()
player?.release()
viewBinding.playerView.player?.release()
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.android_curse.ui.onbording


import com.example.android_curse.ui.base.BaseViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow

class OnbordingViewModel : BaseViewModel() {

private val _isOffVolumeControlStateFlow = MutableStateFlow<Boolean>(true)

fun isOffVolumeControlStateFlow(): Flow<Boolean> = _isOffVolumeControlStateFlow

fun change() {
val compareAndSet : Boolean = _isOffVolumeControlStateFlow.compareAndSet(true, false)
if (!compareAndSet){
_isOffVolumeControlStateFlow.compareAndSet(false, true)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class UserAdapter : RecyclerView.Adapter<UserAdapter.ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val itemView =
LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false)
LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false) // сдесь типо вставлять
return ViewHolder(itemView)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.android_curse.util

import android.content.Context
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat

fun Context.getCompatColor(@ColorRes colorRes: Int) =
ContextCompat.getColor(this, colorRes)
13 changes: 13 additions & 0 deletions app/src/main/java/com/example/android_curse/util/UtilExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.android_curse.util

import android.util.DisplayMetrics
import android.util.TypedValue
import androidx.annotation.Dimension
import androidx.annotation.Dimension.DP

fun dpToPx(displayMetrics: DisplayMetrics, @Dimension(unit = DP) dp: Float) =
TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dp,
displayMetrics
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.android_curse.util

import android.view.View
import androidx.annotation.Dimension

fun View.dpToPx(@Dimension(unit = Dimension.DP) dp: Float): Float = dpToPx(resources.displayMetrics, dp)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.android_curse.util

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes

fun ViewGroup.inflate(@LayoutRes layoutRes: Int, attachToRoot: Boolean = false): View {
return LayoutInflater.from(context).inflate(layoutRes, this, attachToRoot)
}
4 changes: 4 additions & 0 deletions app/src/main/res/layout/fragment_news.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@
app:layout_anchor="@+id/newsRecyclerView"
app:layout_anchorGravity="bottom|end"
app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior" />
<com.example.android_curse.ui.news.PostLikesView
android:id="@+id/likes"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.example.android_curse.ui.news.PostLikesView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
1 change: 1 addition & 0 deletions app/src/main/res/layout/fragment_onbording.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="24dp"
android:backgroundTint="@color/green_blue"
android:text="@string/onboarding_sign_Up_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Expand Down
Loading