Skip to content

Commit

Permalink
GibQuote v2.6.1 | It's 2018 | Starring Quotes | RecyclerView Phix
Browse files Browse the repository at this point in the history
Umm...
** Happy New Year! **

* Implement Un-Starring functionality
  - User can now un-star a starred quote by pressing the 'star' again
  - Process:
    > User stars a quote
      > Quote is stored in FavQuotes DB, and assigned an unique ID
        > When user unstars, the FavQuote record is deleted by the ID
          > Done!

* Override getItemId and give position of item, instead of default implementation
  - i.e. NO_ID -> -1
  - This causes RecyclerView to restore the cached View state of an incorrect item (located at a fixed interval)
  - StackOverflow FTW: https://stackoverflow.com/a/39095384
  - Umm, reddit too: https://www.reddit.com/r/androiddev/comments/2lr8bf/what_does_recyclerview_sethasstableids_do_and_why/
  - Did this, considering itemId in GibQuoteRecyler is stable (unchanged)
    - Unchanged, coz Quotes are just added, not removed (from top/bottom/whatever)

Signed-off-by: a7r3 <arvindultimate7352@gmail.com>
  • Loading branch information
a7r3 committed Dec 31, 2017
1 parent 15eb42e commit 3fa6d29
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 16
targetSdkVersion 27
versionCode 2
versionName "2.6.0"
versionName "2.6.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/java/com/arvind/quote/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,19 @@ public static void shareQuote(Context context, Quote quote) {
context.startActivity(Intent.createChooser(quoteIntent, "Share this Quote"));
}

public static void addToFavQuoteList(Context context, Quote quoteData) {
public static int addToFavQuoteList(Context context, Quote quoteData) {
FavDatabaseHelper favDatabaseHelper = FavDatabaseHelper.getInstance(context);
int id = (int) favDatabaseHelper.getRowCount();
Log.d("MainActivity", "Inserting FavQuote " + id);
favDatabaseHelper.addFavQuote(id, quoteData);
Toast.makeText(context, "Added to Favorites", Toast.LENGTH_SHORT).show();
return id;
}

public static void removeFromFavQuotesList(Context context, int id) {
FavDatabaseHelper favDatabaseHelper = FavDatabaseHelper.getInstance(context);
Log.d("MainActivity", "Removing FavQuote " + id);
favDatabaseHelper.removeFavQuote(id);
}

@Override
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/com/arvind/quote/adapter/Quote.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,37 @@ public class Quote {
private final String quoteText;
private final String authorText;
private int id;
private boolean isStarred;

public Quote(int id, String quoteText, String authorText) {
this.id = id;
this.quoteText = quoteText;
this.authorText = authorText;
this.isStarred = false;
}

public Quote(String quoteText, String authorText) {
this.quoteText = quoteText;
this.authorText = authorText;
this.isStarred = false;
}

public boolean isStarred() {
return isStarred;
}

public void setStarred(boolean starred) {
isStarred = starred;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getQuoteText() {
return quoteText;
}
Expand Down
43 changes: 32 additions & 11 deletions app/src/main/java/com/arvind/quote/adapter/QuoteAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,26 @@ public void onBindViewHolder(QuoteViewHolder holder, int position) {

}

@Override
public long getItemId(int position) {
// Return position right away
// This RecyclerView would have Stable IDs since
// The Nodes are just added, not removed
// Default Implementation returns -1 (NO_ID)
return position;
}

@Override
public int getItemViewType(int position) {
return position;
}

@Override
public int getItemCount() {
return quoteList.size();
}

public class QuoteViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
public class QuoteViewHolder extends RecyclerView.ViewHolder {

final TextView quoteTextView;
final TextView authorTextView;
Expand All @@ -70,19 +84,26 @@ public class QuoteViewHolder extends RecyclerView.ViewHolder implements View.OnL
starQuoteView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.addToFavQuoteList(context, quoteList.get(getAdapterPosition()));
starQuoteView.setImageResource(R.drawable.star_on);
starQuoteView.setEnabled(false);
Quote selectedQuote = quoteList.get(getAdapterPosition());
if (selectedQuote.isStarred()) {
MainActivity.removeFromFavQuotesList(context, selectedQuote.getId());
starQuoteView.setImageResource(R.drawable.star_off);
selectedQuote.setStarred(false);
} else {
selectedQuote.setId(MainActivity.addToFavQuoteList(context, quoteList.get(getAdapterPosition())));
starQuoteView.setImageResource(R.drawable.star_on);
selectedQuote.setStarred(true);
}
}
});

itemView.setOnLongClickListener(this);
}

@Override
public boolean onLongClick(View v) {
new MainActivity().shareQuote(context, quoteList.get(getAdapterPosition()));
return true;
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
MainActivity.shareQuote(context, quoteList.get(getAdapterPosition()));
return true;
}
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.arvind.quote.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Typeface;
Expand Down
23 changes: 14 additions & 9 deletions app/src/main/java/com/arvind/quote/utils/SomeBroadReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,30 @@ public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case "android.intent.action.BOOT_COMPLETED":
case "com.arvind.quote.TIME_SET_BY_USER":
// Create an intent which would call the BroadcastReceiver Again
Intent someIntent = new Intent(context, SomeBroadReceiver.class);
// But this time, with a different action!
someIntent.setAction("com.arvind.quote.SHOW_QUOTE");
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Log.d(TAG, "Feels good to be back");
int hour = sharedPreferences.getInt("QOTD_HOUR", 0);
int minute = sharedPreferences.getInt("QOTD_MIN", 0);
Log.d(TAG, "Scheduling QoTD Notifications");
// Get System's AlarmManager
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// Create a Pending Intent, which would be executed by the AlarmManager
// At the Given Time and Interval
notifPendingIntent = PendingIntent.getBroadcast(context, 0, someIntent, 0);
// Get the Calendar, we've got to set the time
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
// Set the Time
calendar.set(Calendar.HOUR_OF_DAY, sharedPreferences.getInt("QOTD_HOUR", 0));
calendar.set(Calendar.MINUTE, sharedPreferences.getInt("QOTD_MIN", 0));
calendar.set(Calendar.SECOND, 0);
Log.i(TAG, calendar.getTime().toString());
alarmManager.setRepeating(
AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,
notifPendingIntent);
AlarmManager.RTC_WAKEUP, // Ctrl+Q please
calendar.getTimeInMillis(), // Time at which intent has to be executed
AlarmManager.INTERVAL_DAY, // Publish the notifications daily
notifPendingIntent); // The Intent to be executed
break;
case "com.arvind.quote.SHOW_QUOTE":
Log.i(TAG, "Waking up Notification Service");
Expand Down

0 comments on commit 3fa6d29

Please sign in to comment.