Saturday, 25 February 2023

when incrementing using do transaction it reloads my previous activity

when a user wants to favourite an app with in my app. they click on the favourite button which is button 2. this runs do transaction and increments the firebase node upvote and also adds a true boolean to my users favourite node with in firebase. if they click the button again it removes the increment and sets the favourite value to false... this all works great nut i have a weird bug that happens. everytime i click the favourite button it will restart my previous activity..

there are a few variables ill explain here

user points to the userId of the user.. key points is the genre of app the user clicked appkey is the actual app the user is wanting to download

Edit****

Here is a link to a video of what happens when I click the favourite button.

YouTube short of what's happens

my firebase database apps node looks like this

{
 "Apps": {
"Booster": {
  "ANTBOOSTER": {
    "desc": "",
    "downloads": 30,
    "image": "",
    "tag": "Keep Your phone fast",
    "title": "Ant Booster",
    "upvote": 51,
    "url": ""
  }

then my User node looks like this

"Users": {
"USERID APPEARS HERE": {
  "Advert": false,
  "Favourites": {
    "ANTBOOSTER": true
  },
  "downloads": 0,
  "userid": ""

this is the code ive used to favourite

 private void favourite() {
    FirebaseDatabase db = FirebaseDatabase.getInstance();
    DatabaseReference ref = db.getReference().child("Users").child(user).child("Favourites");
    DatabaseReference likesRef = FirebaseDatabase.getInstance().getReference().child("Apps").child(key).child(app_key);
    String Upvote = "upvote";
    if (!FAVOURITE) {
        FAVOURITE = true;
        updateCounter(FAVOURITE, likesRef, Upvote);
        button2.setText("You liked this app");
        ref.child(app_key).setValue(true);

    } else {
        FAVOURITE = false;
        button2.setText("Favourite");
        updateCounter(FAVOURITE, likesRef, Upvote);

        ref.child(app_key).removeValue();

    }
}

this is the update counter method which is called within favourite

  private void updateCounter(boolean increment, DatabaseReference likeref,String REFString) {
    likeref.child(REFString).runTransaction(new Transaction.Handler() {
        @Override
        public Transaction.Result doTransaction(MutableData mutableData) {
            if (mutableData.getValue() != null) {
                int value = mutableData.getValue(Integer.class);
                if (increment) {
                    value++;
                } else {
                    value--;
                }
                mutableData.setValue(value);
            }
            return Transaction.success(mutableData);
        }

        @Override
        public void onComplete(DatabaseError databaseError, boolean b,
                               DataSnapshot dataSnapshot) {
            // Transaction completed
            Log.d(TAG, "likeTransaction:onComplete:" + databaseError);
        }
    });
}

and finally this method checks the app is favourited when the activity is started

  private void updateCounter(boolean increment, DatabaseReference likeref,String REFString) {
    likeref.child(REFString).runTransaction(new Transaction.Handler() {
        @Override
        public Transaction.Result doTransaction(MutableData mutableData) {
            if (mutableData.getValue() != null) {
                int value = mutableData.getValue(Integer.class);
                if (increment) {
                    value++;
                } else {
                    value--;
                }
                mutableData.setValue(value);
            }
            return Transaction.success(mutableData);
        }

        @Override
        public void onComplete(DatabaseError databaseError, boolean b,
                               DataSnapshot dataSnapshot) {
            // Transaction completed
            Log.d(TAG, "likeTransaction:onComplete:" + databaseError);
        }
    });
}

finally this is the previous activities code which is a tabbed activity

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_popular, container, false);
    initVariables(root);


    return root;
}

private void initVariables(View root){

    mlist = root.findViewById(R.id.station_list);
    imageView = root.findViewById(R.id.tv_thumbup);

    getApps();

}
private void getApps() {
    db = FirebaseDatabase.getInstance();
    DatabaseReference reference = db.getReference().child("Apps").child(key);


    Query query = reference.orderByChild("upvote").startAt(50);

     valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            mUrl.clear();
            mTITLE.clear();
            mIMAGE.clear();
            mUrl.clear();
            mUP.clear();
            mDESC.clear();

            for (DataSnapshot snap : snapshot.getChildren()) {
                String stationkey = snap.getKey();

                String a = snap.child("title").getValue(String.class);
                tag = snap.child("tag").getValue(String.class);
                String c = snap.child("url").getValue(String.class);
                Long d = snap.child("upvote").getValue(Long.class);
                String f = snap.child("image").getValue(String.class);


                APP_KEY.add(stationkey);
                mTITLE.add(a);
                mDESC.add(tag);
                mUrl.add(c);
                mUP.add(String.valueOf(d));
                mIMAGE.add(f);


            }
            titlearray = mTITLE.toArray(new String[mTITLE.size()]);
            desarray = mDESC.toArray(new String[mDESC.size()]);
            uparray = mUP.toArray(new String[mUP.size()]);
            imagearray = mIMAGE.toArray(new String[mIMAGE.size()]);

            AppAdapter appAdapter =
                    new AppAdapter(
                            titlearray,
                            desarray,
                            uparray,
                            imagearray, getContext());
            mlist.setAdapter(appAdapter);

            mlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    String bundledkey = APP_KEY.get(i);
                    String bundledtitle = mTITLE.get(i);
                    String bundledtag = mDESC.get(i);
                    String bundledImage = mIMAGE.get(i);
                    String bundledfav = mUP.get(i);
                    query.removeEventListener(valueEventListener);

                    Intent intent = new Intent(getContext(), AppDownload.class);
                    intent.putExtra("app_key", bundledkey);
                    intent.putExtra("title", bundledtitle);
                    intent.putExtra("tag", bundledtag);
                    intent.putExtra("image", bundledImage);
                    intent.putExtra("favourite", bundledfav);
                    intent.putExtra("type_key", key);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);

                    startActivity(intent);


                }

            });

        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    };
    query.addValueEventListener(valueEventListener);
}
 public void setKey (String string){
    key = string;
 }
 public void setUser(String user){this.user = user;}

Edit

so here is my logcat from when i click the favourite button..also re added the video to show what is actually happening as that didnt seam to work yesterday

2023-02-24 10:40:12.295 20849-20849/com.p9p.h20droidapp 
I/Timeline: Timeline: Activity_launch_request time:673793
2023-02-24 10:40:12.320 20849-20849/com.p9p.h20droidapp 
W/ActivityThread: handleWindowVisibility: no activity for token 
android.os.BinderProxy@de19309
2023-02-24 10:40:12.333 20849-20849/com.p9p.h20droidapp 
D/ForceDarkHelper: updateByCheckExcludeList: pkg: 
com.p9p.h20droidapp activity: com.p9p.h20droidapp.tab_view@936abe6
2023-02-24 10:40:12.334 20849-20849/com.p9p.h20droidapp 
D/ForceDarkHelper: updateByCheckExcludeList: pkg: 
com.p9p.h20droidapp activity: com.p9p.h20droidapp.tab_view@936abe6
2023-02-24 10:40:12.343 20849-20849/com.p9p.h20droidapp I/chatty: 
uid=10362(com.p9p.h20droidapp) identical 5 lines
2023-02-24 10:40:12.343 20849-20849/com.p9p.h20droidapp 
D/ForceDarkHelper: updateByCheckExcludeList: pkg: 
com.p9p.h20droidapp activity: com.p9p.h20droidapp.tab_view@936abe6
2023-02-24 10:40:12.346 20849-20849/com.p9p.h20droidapp 
W/TabLayout: MODE_SCROLLABLE + GRAVITY_FILL is not supported, 
GRAVITY_START will be used instead
2023-02-24 10:40:12.346 20849-20849/com.p9p.h20droidapp 
D/ForceDarkHelper: updateByCheckExcludeList: pkg: 
com.p9p.h20droidapp activity: com.p9p.h20droidapp.tab_view@936abe6
2023-02-24 10:40:12.350 20849-20849/com.p9p.h20droidapp 
I/menu_key: MoviesnShows
2023-02-24 10:40:12.351 20849-20849/com.p9p.h20droidapp 
 D/ForceDarkHelper: updateByCheckExcludeList: pkg: 
com.p9p.h20droidapp activity: com.p9p.h20droidapp.tab_view@936abe6
2023-02-24 10:40:12.363 20849-20849/com.p9p.h20droidapp I/chatty: 
uid=10362(com.p9p.h20droidapp) identical 13 lines
2023-02-24 10:40:12.363 20849-20849/com.p9p.h20droidapp 
 D/ForceDarkHelper: updateByCheckExcludeList: pkg: 
com.p9p.h20droidapp activity: com.p9p.h20droidapp.tab_view@936abe6
2023-02-24 10:40:12.382 20849-20849/com.p9p.h20droidapp 
D/ForceDarkHelper: updateByCheckExcludeList: pkg: 
com.p9p.h20droidapp activity: com.p9p.h20droidapp.tab_view@936abe6
2023-02-24 10:40:12.382 20849-20849/com.p9p.h20droidapp 
D/ForceDarkHelper: updateByCheckExcludeList: pkg: 
com.p9p.h20droidapp activity: com.p9p.h20droidapp.tab_view@936abe6
2023-02-24 10:40:12.387 20849-20849/com.p9p.h20droidapp 
D/ForceDarkHelper: updateByCheckExcludeList: pkg: 
com.p9p.h20droidapp activity: com.p9p.h20droidapp.tab_view@936abe6
2023-02-24 10:40:12.387 20849-20849/com.p9p.h20droidapp 
D/ForceDarkHelper: updateByCheckExcludeList: pkg: 
com.p9p.h20droidapp activity: com.p9p.h20droidapp.tab_view@936abe6
2023-02-24 10:40:12.419 20849-20849/com.p9p.h20droidapp 
D/ContentValues: likeTransaction:onComplete:null


from when incrementing using do transaction it reloads my previous activity

No comments:

Post a Comment