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/message like button #37

Merged
merged 2 commits into from
Jan 16, 2021
Merged
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
4 changes: 4 additions & 0 deletions Dear-World/Dear-World.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
121BDB592597652A0062B15A /* MessageTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 121BDB582597652A0062B15A /* MessageTableViewCell.swift */; };
121BDB672597982F0062B15A /* DiscoverReactor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 121BDB662597982F0062B15A /* DiscoverReactor.swift */; };
121BDB8E259829840062B15A /* CountrySelectController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 121BDB8D259829840062B15A /* CountrySelectController.swift */; };
1221913B25B19EB8001D0D10 /* heartfull.json in Resources */ = {isa = PBXBuildFile; fileRef = 1221913A25B19EB8001D0D10 /* heartfull.json */; };
12257E1825A622750007E65E /* SortTypeSelectController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12257E1725A622750007E65E /* SortTypeSelectController.swift */; };
1263E09725A1C87400E3F121 /* Message.API.Countries.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1263E09625A1C87400E3F121 /* Message.API.Countries.swift */; };
1263E09C25A1C8DD00E3F121 /* Message.Model.Countries.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1263E09B25A1C8DD00E3F121 /* Message.Model.Countries.swift */; };
Expand Down Expand Up @@ -115,6 +116,7 @@
121BDB582597652A0062B15A /* MessageTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageTableViewCell.swift; sourceTree = "<group>"; };
121BDB662597982F0062B15A /* DiscoverReactor.swift */ = {isa = PBXFileReference; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = DiscoverReactor.swift; sourceTree = "<group>"; tabWidth = 2; };
121BDB8D259829840062B15A /* CountrySelectController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CountrySelectController.swift; sourceTree = "<group>"; };
1221913A25B19EB8001D0D10 /* heartfull.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = heartfull.json; path = ../../../../Downloads/heartfull.json; sourceTree = "<group>"; };
12257E1725A622750007E65E /* SortTypeSelectController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SortTypeSelectController.swift; sourceTree = "<group>"; };
1263E09625A1C87400E3F121 /* Message.API.Countries.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Message.API.Countries.swift; sourceTree = "<group>"; };
1263E09B25A1C8DD00E3F121 /* Message.Model.Countries.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Message.Model.Countries.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -372,6 +374,7 @@
isa = PBXGroup;
children = (
39658F9F259ADE770050D180 /* splash_1x.json */,
1221913A25B19EB8001D0D10 /* heartfull.json */,
395825B625948EE4007325AB /* Colors.xcassets */,
3958258325948E43007325AB /* Assets.xcassets */,
3958258525948E43007325AB /* LaunchScreen.storyboard */,
Expand Down Expand Up @@ -739,6 +742,7 @@
39518C7725A8FA2400F777D1 /* GoogleService-Info.plist in Resources */,
395825B725948EE4007325AB /* Colors.xcassets in Resources */,
3958258425948E43007325AB /* Assets.xcassets in Resources */,
1221913B25B19EB8001D0D10 /* heartfull.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// LikeButton.swift
// Dear-World
//
// Created by rookie.w on 2021/01/15.
//

import UIKit
import Lottie
import RxSwift

class LikeButton: UIView {
enum Progress: CGFloat {
case likeDefault = 30
case unlikeDefault = 1
case likeTouchEnd = 15
case unlikeTouchEnd = 60

func percent() -> CGFloat {
self.rawValue / 60
}
}

let disposeBag: DisposeBag = DisposeBag()
let animationView: AnimationView = AnimationView(animation: Animation.named("heartfull"))
var messageId: Int
init(isLike: Bool, messageId: Int) {
self.messageId = messageId
super.init(frame: .null)
setupUI(isLike: isLike)
bind()
}
func setupUI(isLike: Bool) {
self.addSubview(animationView)
self.translatesAutoresizingMaskIntoConstraints = false
self.clipsToBounds = false
animationView.snp.makeConstraints {
$0.top.bottom.leading.trailing.equalToSuperview()
}
self.animationView.currentProgress = isLike ? Progress.likeDefault.percent() : Progress.unlikeDefault.percent()
}
func bind() {
self.rx.tapGesture()
.skip(1)
.throttle(.milliseconds(1000), latest: false, scheduler: MainScheduler.instance)
.flatMap { [weak self] _ -> Observable<Bool?> in
guard let id = self?.messageId else {
return Observable.just(false)
}
return Network.request(Message.API.Like(messageId: id))
.map { $0?.isLiked }
}
.subscribe(onNext: { [weak self] isLike in
self?.tap(isLike!)
})
.disposed(by: self.disposeBag)
}
@objc func tap(_ isLike: Bool) {
animationView.pause()
if isLike {
self.animationView.play(fromProgress: Progress.unlikeDefault.percent(), toProgress: Progress.likeTouchEnd.percent())
} else {
self.animationView.play(fromProgress: Progress.likeDefault.percent(), toProgress: Progress.unlikeTouchEnd.percent())
}
}
override init(frame: CGRect) {
self.messageId = 0
super.init(frame: frame)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,38 @@
import RxCocoa
import RxSwift
import UIKit
import Lottie

final class MessageTableViewCell: UITableViewCell {
private enum HeartProgress: CGFloat {
case likeDefault = 30
case unlikeDefault = 1
case likeTouchEnd = 15
case unlikeTouchEnd = 60

func percent() -> CGFloat {
self.rawValue / 60
}
}

let disposeBag: DisposeBag = DisposeBag()
// MARK: 🖼 UI
let emojiImageView: UIImageView = UIImageView()
let nameLabel: UILabel = UILabel()
let countryLabel: UILabel = UILabel()
let countryFlagImageView: UIImageView = UIImageView()
let detailTextView: UITextView = UITextView()
let likeView: UIImageView = UIImageView()
let likeCountLabel: UILabel = UILabel()
let shareButton: UIButton = UIButton()
let likeView: AnimationView = AnimationView(animation: Animation.named("heartfull"))
let likeCountLabel: UILabel = UILabel()
var likeCount: Int = 0 {
didSet {
self.likeCountLabel.text = "\(likeCount)"
}
}
var isLike: Bool = false {
didSet {
self.likeView.image = isLike ? UIImage(named: "heart_liked") : UIImage(named: "heart")
self.likeView.currentProgress = isLike ? HeartProgress.likeDefault.percent() : HeartProgress.unlikeDefault.percent()
self.likeCountLabel.textColor = isLike ? .loveRed : .grayWhite
}
}
Expand Down Expand Up @@ -110,8 +122,8 @@ final class MessageTableViewCell: UITableViewCell {
}
mainView.addSubview(self.countryLabel)
self.countryLabel.snp.makeConstraints {
$0.bottom.equalTo(emojiView.snp.bottom)
$0.leading.equalTo(emojiView.snp.trailing).offset(10)
$0.centerY.equalTo(countryFlagImageView)
$0.leading.equalTo(countryFlagImageView.snp.trailing).offset(5)
$0.trailing.lessThanOrEqualToSuperview()
}

Expand All @@ -130,15 +142,16 @@ final class MessageTableViewCell: UITableViewCell {
}

self.likeView.do {
$0.image = UIImage(named: "heart")
$0.translatesAutoresizingMaskIntoConstraints = false
$0.clipsToBounds = false
}
mainView.addSubview(likeView)
self.likeView.snp.makeConstraints {
$0.leading.equalToSuperview().inset(30)
$0.top.equalTo(detailTextView.snp.bottom).offset(19)
$0.bottom.equalToSuperview().inset(24)
$0.height.equalTo(17)
$0.width.equalTo(20)
likeView.snp.makeConstraints {
$0.leading.equalToSuperview().inset(20)
$0.top.equalTo(detailTextView.snp.bottom).offset(7)
$0.bottom.equalToSuperview().inset(13)
$0.height.equalTo(40)
$0.width.equalTo(40)
}

self.likeCountLabel.do {
Expand All @@ -148,8 +161,8 @@ final class MessageTableViewCell: UITableViewCell {
mainView.addSubview(self.likeCountLabel)
self.likeCountLabel.snp.makeConstraints {
$0.centerY.equalTo(likeView.snp.centerY)
$0.height.equalTo(14)
$0.leading.equalTo(likeView.snp.trailing).offset(5)
$0.height.equalTo(17)
$0.leading.equalTo(likeView.snp.trailing).offset(-5)
}

self.shareButton.do {
Expand All @@ -169,12 +182,12 @@ final class MessageTableViewCell: UITableViewCell {
}
}

// MARK: 🔗 Bind
private func bind() {
self.likeView
.rx.tapGesture()
.debug()
.throttle(.milliseconds(300), scheduler: MainScheduler.instance)
.skip(1)
.throttle(.milliseconds(1000), latest: false, scheduler: MainScheduler.instance)
.flatMap { [weak self] _ -> Observable<Bool?> in
guard let id = self?.messageId else {
return Observable.just(false)
Expand All @@ -183,10 +196,85 @@ final class MessageTableViewCell: UITableViewCell {
.map { $0?.isLiked }
}
.filterNil()
.subscribe (onNext: {[weak self] isLike in
self?.isLike = isLike
self?.likeCount += (isLike ? 1 : -1)
.subscribe(onNext: { [weak self] isLike in
guard let likeView = self?.likeView else { return }
likeView.pause()
if !isLike {
self?.likeCount -= 1
self?.likeCountLabel.textColor = .grayWhite
}
likeView.play(
// TODO : 각 상황에 대한 값들을 enum으로 정의할것
fromProgress: isLike ? HeartProgress.unlikeDefault.percent() : HeartProgress.likeDefault.percent(),
toProgress: isLike ? HeartProgress.likeTouchEnd.percent() : HeartProgress.unlikeTouchEnd.percent()) { (completed) in
if completed && isLike{
self?.likeCount += 1
self?.likeCountLabel.textColor = .loveRed
}
}
})
.disposed(by: self.disposeBag)
}
}
//class Fdf: UIView {
// enum Progress: CGFloat {
// case likeDefault = 30
// case unlikeDefault = 1
// case likeTouchEnd = 15
// case unlikeTouchEnd = 60
//
// func percent() -> CGFloat {
// self.rawValue / 60
// }
// }
//
// let disposeBag: DisposeBag = DisposeBag()
// let animationView: AnimationView = AnimationView(animation: Animation.named("heartfull"))
// var messageId: Int
// init(isLike: Bool, messageId: Int) {
// self.messageId = messageId
// super.init(frame: .null)
// setupUI(isLike: isLike)
// bind()
// }
// func setupUI(isLike: Bool) {
// self.addSubview(animationView)
// self.translatesAutoresizingMaskIntoConstraints = false
// self.clipsToBounds = false
// animationView.snp.makeConstraints {
// $0.top.bottom.leading.trailing.equalToSuperview()
// }
// self.animationView.currentProgress = isLike ? Progress.likeDefault.percent() : Progress.unlikeDefault.percent()
// }
// func bind() {
// self.rx.tapGesture()
// .skip(1)
// .throttle(.milliseconds(1000), latest: false, scheduler: MainScheduler.instance)
// .flatMap { [weak self] _ -> Observable<Bool?> in
// guard let id = self?.messageId else {
// return Observable.just(false)
// }
// return Network.request(Message.API.Like(messageId: id))
// .map { $0?.isLiked }
// }
// .subscribe(onNext: { [weak self] isLike in
// self?.tap(isLike!)
// })
// .disposed(by: self.disposeBag)
// }
// @objc func tap(_ isLike: Bool) {
// animationView.pause()
// if isLike {
// self.animationView.play(fromProgress: Progress.unlikeDefault.percent(), toProgress: Progress.likeTouchEnd.percent())
// } else {
// self.animationView.play(fromProgress: Progress.likeDefault.percent(), toProgress: Progress.unlikeTouchEnd.percent())
// }
// }
// override init(frame: CGRect) {
// self.messageId = 0
// super.init(frame: frame)
// }
// required init?(coder: NSCoder) {
// fatalError("init(coder:) has not been implemented")
// }
//}