Support
Quality
Security
License
Reuse
kandi has reviewed ShineButton and discovered the below as its top functions. This is intended to give you an instant insight into ShineButton implemented functionality, and help decide if they suit your requirements.
This is a UI lib for Android. Effects like shining.
Usage
shineButton = (ShineButton) findViewById(R.id.shine_button);
shineButton.init(activity);
Support Dialog
shineButton.setFixDialog(dialog);
Maven
<dependency>
<groupId>com.sackcentury</groupId>
<artifactId>shinebutton</artifactId>
<version>1.0.0</version>
<type>aar</type>
</dependency>
Gradle
buildscript {
repositories {
mavenCentral()
}
}
dependencies {
compile 'com.sackcentury:shinebutton:1.0.0'
}
License
The MIT License (MIT)
Copyright (c) 2016 Chad Song
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Confused with Likes Counter using Firebase in Android
if(isLiked){
//If already liked then user wants to unlike the post
likesRef.setValue(numLikes-1); }
else {
//If not liked already then user wants to like the post
likesRef.setValue(numLikes+1);
shineButton.setEnabled(false);
}
shineButton.setOnCheckStateChangeListener(new ShineButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(View view, boolean checked) {
if (checked){
likesRef.setValue(numLikes+1);
} else {
likesRef.setValue(numLikes-1);
}
}
});
-----------------------
if(isLiked){
//If already liked then user wants to unlike the post
likesRef.setValue(numLikes-1); }
else {
//If not liked already then user wants to like the post
likesRef.setValue(numLikes+1);
shineButton.setEnabled(false);
}
shineButton.setOnCheckStateChangeListener(new ShineButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(View view, boolean checked) {
if (checked){
likesRef.setValue(numLikes+1);
} else {
likesRef.setValue(numLikes-1);
}
}
});
QUESTION
Confused with Likes Counter using Firebase in Android
Asked 2020-Nov-22 at 10:20I am beginner and I am working on an android Project where users like the post that admin posts for them. The likes are updated to firebase when user clicks on the like button. The Issue is even though the user likes or unlikes the post, the likes count is increasing. Please look into it.
Note:
Log.d(TAG, "Liked onDataChange: "+isLiked);
returns false no matter if we like or unlike the shinebutton. Shinebutton is a library.
Link to its GitHub : Shinebutton Github
onLike Method:
@Override
public void onLike(View v, int position) {
UploadImage selectedItem = mUploads.get(position);
String selectedKey = selectedItem.getKey();
final ShineButton shineButton = new ShineButton(Objects.requireNonNull(getActivity()));
shineButton.setBtnFillColor(RED);
shineButton.setShapeResource(R.raw.heart);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100,100);
shineButton.setLayoutParams(layoutParams);
final DatabaseReference likesRef = FirebaseDatabase.getInstance().getReference().child("ImageUploads").child(selectedKey).child("likes");
likesRef.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(@NotNull DataSnapshot dataSnapshot) {
long numLikes = 0;
if(dataSnapshot.exists()){
numLikes = dataSnapshot.getValue(Long.class);
}
boolean isLiked = shineButton.isSelected();
Log.d(TAG, "Liked onDataChange: "+isLiked);
if(isLiked){
//If already liked then user wants to unlike the post
likesRef.setValue(numLikes-1);
}else {
//If not liked already then user wants to like the post
likesRef.setValue(numLikes+1);
shineButton.setEnabled(false);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
ShineButton like = layout.findViewById(R.id.like_feed);
System.out.println(RED);
like.setBackground(getResources().getDrawable(R.drawable.like_feed_bg));
}
After the modification of the Code: Now eventhough we like or unlike the count is not increasing or decreasing.
Onlike Method:
public void onLike(View v, int position) {
UploadImage selectedItem = mUploads.get(position);
String selectedKey = selectedItem.getKey();
final ShineButton shineButton = new ShineButton(Objects.requireNonNull(getActivity()));
shineButton.setBtnFillColor(RED);
shineButton.setShapeResource(R.raw.heart);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100,100);
shineButton.setLayoutParams(layoutParams);
final DatabaseReference likesRef = FirebaseDatabase.getInstance().getReference().child("ImageUploads").child(selectedKey).child("likes");
likesRef.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(@NotNull final DataSnapshot dataSnapshot) {
long numLikes = 0;
if(dataSnapshot.exists()){
numLikes = dataSnapshot.getValue(Long.class);
}
final long finalNumLikes = numLikes;
shineButton.setOnCheckStateChangeListener(new ShineButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(View view, boolean checked) {
if (checked){
likesRef.setValue(finalNumLikes-1);
}
else{
likesRef.setValue(finalNumLikes+1);
}
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
ShineButton like = layout.findViewById(R.id.like_feed);
System.out.println(RED);
like.setBackground(getResources().getDrawable(R.drawable.like_feed_bg));
}
ANSWER
Answered 2020-Nov-22 at 09:41The problem is that you do not check the state of the button being pressed. You can achieve this by doing the following:
Replace:
if(isLiked){
//If already liked then user wants to unlike the post
likesRef.setValue(numLikes-1); }
else {
//If not liked already then user wants to like the post
likesRef.setValue(numLikes+1);
shineButton.setEnabled(false);
}
With this:
shineButton.setOnCheckStateChangeListener(new ShineButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(View view, boolean checked) {
if (checked){
likesRef.setValue(numLikes+1);
} else {
likesRef.setValue(numLikes-1);
}
}
});
You will also have to keep reference of the botton for when you close the application. You can do this using SharedPreferences
.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Save this library and start creating your kit
Explore Related Topics
Save this library and start creating your kit