Skip to content

Commit

Permalink
GibQuote v2.6.5 | Linting | CommonUtil{s,Service}
Browse files Browse the repository at this point in the history
** Resolve some warnings raised by the Android Linter

** Move static functions `addtoFavQuotesList', `removeFromFavQuotesList', and
   `shareQuote' under a Utility class : utils/CommonUtils
   - Usages of these utility functions in different classes has been changed
     appropriately

* Create a service to make use of this utilities, when used by QoTD notification
  - services/CommonUtilService

* MainActivity:
  - Consider the UPDATE_CHECK boolean before checking for updates, forgot to add

* fragment/GibQuoteFragment:
  - Don't log quoteJson string, it might be big at times, no LogSpam pls

* utils/SomeBroadReceiver:
  - Add action 'COMMON_UTILS' to handle Notification Dialog broadcasts
    - i.e. 'Share' and 'Fav This' actions in Notification

* utils/Updater:
  - JavaDoc-ify it (I think this could become a standalone library)
  - checkForUpdates: I'm bad with if-booleans ;-;
    - if-boolean statements should be fine now
  - Bring in constructDialog to createDialog (it's unnecessary)

* utils/notifications/NotificationUtils:
  - Add Notification Actions to share and fav QoTD
  - Send a Broadcast to `SomeBroadReceiver' with 'COMMON_UTILS' action
    - Add two Extras
      - 1. The Quote, in serialized way (common for both actions)
      - 2. The Method to be executed (share/Fav)

* res/layout/activity_notification_dialog.xml:
  - Some Layout changes

Signed-off-by: a7r3 <arvindultimate7352@gmail.com>
  • Loading branch information
a7r3 committed Jan 3, 2018
1 parent 4ed5033 commit 669800d
Show file tree
Hide file tree
Showing 23 changed files with 220 additions and 175 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions 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.4"
versionName "2.6.5"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -29,8 +29,8 @@ dependencies {
transitive = true
}
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.android.support:support-annotations:+'
compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'
compile 'com.android.support:support-annotations:27.0.2'
compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:2.0.1'
compile 'com.getkeepsafe.taptargetview:taptargetview:1.10.0'
compile 'com.takisoft.fix:preference-v7:27+'
compile 'com.takisoft.fix:preference-v7-datetimepicker:+'
Expand All @@ -40,6 +40,6 @@ dependencies {
compile 'com.android.support:cardview-v7:27+'
compile 'com.android.support:recyclerview-v7:27+'
compile 'com.android.support:appcompat-v7:27+'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
compile 'com.android.support.constraint:constraint-layout:1.1.0-beta4'
testCompile 'junit:junit:4.12'
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
</intent-filter>
</receiver>
<service android:name=".services.NotificationService" />
<service android:name=".services.CommonUtilService" />
</application>

</manifest>
80 changes: 18 additions & 62 deletions app/src/main/java/com/arvind/quote/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.arvind.quote;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
Expand All @@ -29,10 +27,7 @@
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.arvind.quote.adapter.Quote;
import com.arvind.quote.database.FavDatabaseHelper;
import com.arvind.quote.fragment.FavQuoteFragment;
import com.arvind.quote.fragment.GibQuoteFragment;
import com.arvind.quote.fragment.PreferencesFragment;
Expand All @@ -41,12 +36,19 @@

public class MainActivity extends AppCompatActivity implements PreferenceFragmentCompat.OnPreferenceStartScreenCallback, ActivityCompat.OnRequestPermissionsResultCallback {

// GitHub Tag Endpoint
public final static String GIT_TAG_URL = "https://api.github.com/repos/a7r3/GibQuote/git/refs/tags";
// ActionBar for the App
private static ActionBar actionBar;
// Who am I ?
private final String TAG = "MainActivity";
// GitHub Tag Endpoint
public final static String GIT_TAG_URL = "https://api.github.com/repos/a7r3/GibQuote/git/refs/tags";
// Callback value
// This would be used to check which set of permissions were asked
// This is of Storage
private final int REQUEST_WRITE_EXTERNAL_STORAGE = 10;
private final String[] requiredPerms = {
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
private boolean isStoragePermissionGranted = false;
// Layout under which fragments would reside
private DrawerLayout drawerLayout;
Expand All @@ -64,15 +66,6 @@ public class MainActivity extends AppCompatActivity implements PreferenceFragmen
// Root Layout
private RelativeLayout rootLayout;

// Callback value
// This would be used to check which set of permissions were asked
// This is of Storage
private int REQUEST_WRITE_EXTERNAL_STORAGE = 10;

private String[] requiredPerms = {
Manifest.permission.WRITE_EXTERNAL_STORAGE
};

// Required by Fragments to ...
// Set ActionBar's title
public static void setActionBarTitle(String title) {
Expand All @@ -81,48 +74,15 @@ public static void setActionBarTitle(String title) {
}

public static void showActionBar() {
if(actionBar != null)
if (actionBar != null)
actionBar.show();
}

public static void hideActionBar() {
if(actionBar != null)
if (actionBar != null)
actionBar.hide();
}

/* Allows the user to share currently displayed quote */
public static void shareQuote(Context context, Quote quote) {
Log.d("MainActivity", "Creating Share Intent");
// My intention is to send (throw) a piece of Text (ball)
Intent quoteIntent = new Intent(Intent.ACTION_SEND);
// Piece of Text (the Ball)
String quoteMessage = quote.getQuoteText() + "\n\n-- " + quote.getAuthorText();
// Specify the Text to be thrown
quoteIntent.putExtra(Intent.EXTRA_TEXT, quoteMessage);
// Specify the MIME type of the object to be thrown
quoteIntent.setType("text/plain");
// Send an Acknowledgement
Toast.makeText(context, "Select an App to GibQuote", Toast.LENGTH_SHORT).show();
// Throw the Ball!
context.startActivity(Intent.createChooser(quoteIntent, "Share this Quote"));
}

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);
Toast.makeText(context, "Removed from Favorites", Toast.LENGTH_SHORT).show();
favDatabaseHelper.removeFavQuote(id);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Expand All @@ -147,7 +107,6 @@ protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

String themeKey = sharedPreferences.getString("THEME_KEY", "light");

int cardViewBackGround = R.color.colorLightCardView;
Expand Down Expand Up @@ -293,14 +252,15 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}

if(isStoragePermissionGranted)
new Updater(this)
.setTagsUrl(GIT_TAG_URL)
.setRootLayout(R.id.root_layout)
.checkForUpdates();
if (isStoragePermissionGranted)
if (sharedPreferences.getBoolean("UPDATE_CHECK", true))
new Updater(this)
.setTagsUrl(GIT_TAG_URL)
.setRootLayout(R.id.root_layout)
.checkForUpdates();
}

public void switchFragment(MenuItem item) {
private void switchFragment(MenuItem item) {
Fragment fragment;
switch (item.getItemId()) {
case R.id.gib_quotes_item:
Expand Down Expand Up @@ -370,8 +330,4 @@ public void onConfigurationChanged(Configuration newConfig) {
drawerToggle.onConfigurationChanged(newConfig);
}

@Override
protected void onDestroy() {
super.onDestroy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import android.view.ViewGroup;
import android.widget.TextView;

import com.arvind.quote.MainActivity;
import com.arvind.quote.R;
import com.arvind.quote.database.FavDatabaseHelper;
import com.arvind.quote.utils.CommonUtils;

import java.util.List;

Expand Down Expand Up @@ -113,7 +113,7 @@ public boolean onTouch(View view, MotionEvent motionEvent) {

@Override
public boolean onLongClick(View v) {
new MainActivity().shareQuote(context, favQuoteList.get(getAdapterPosition()));
CommonUtils.shareQuote(context, favQuoteList.get(getAdapterPosition()));
return true;
}
}
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/com/arvind/quote/adapter/QuoteAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import android.widget.ImageView;
import android.widget.TextView;

import com.arvind.quote.MainActivity;
import com.arvind.quote.R;
import com.arvind.quote.utils.CommonUtils;

import java.util.List;

Expand Down Expand Up @@ -49,7 +49,7 @@ public void onBindViewHolder(QuoteViewHolder holder, int position) {

// If quote was starred in previous session
// Let the star Glow
if(quote.isStarred())
if (quote.isStarred())
holder.starQuoteView.setImageResource(R.drawable.star_on);
}

Expand Down Expand Up @@ -90,11 +90,11 @@ public class QuoteViewHolder extends RecyclerView.ViewHolder {
public void onClick(View v) {
Quote selectedQuote = quoteList.get(getAdapterPosition());
if (selectedQuote.isStarred()) {
MainActivity.removeFromFavQuotesList(context, selectedQuote.getId());
CommonUtils.removeFromFavQuotesList(context, selectedQuote.getId());
starQuoteView.setImageResource(R.drawable.star_off);
selectedQuote.setStarred(false);
} else {
selectedQuote.setId(MainActivity.addToFavQuoteList(context, quoteList.get(getAdapterPosition())));
selectedQuote.setId(CommonUtils.addToFavQuoteList(context, quoteList.get(getAdapterPosition())));
starQuoteView.setImageResource(R.drawable.star_on);
selectedQuote.setStarred(true);
}
Expand All @@ -104,7 +104,7 @@ public void onClick(View v) {
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
MainActivity.shareQuote(context, quoteList.get(getAdapterPosition()));
CommonUtils.shareQuote(context, quoteList.get(getAdapterPosition()));
return true;
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ public class FavDatabaseHelper extends SQLiteOpenHelper {
// to the database (for eg. Changing column info)
// Incrementing it would drop older version's DB
// See onUpgrade() for implementation
private static int DATABASE_VERSION = 1;
private static final int DATABASE_VERSION = 1;
// Give the DB a good name
private static String DATABASE_NAME = "FavQuote";
private static final String DATABASE_NAME = "FavQuote";
// Static Instance of Database Helper
private static FavDatabaseHelper favDatabaseHelperInstance;
private String TAG = "FavDatabaseHelper";
private final String TAG = "FavDatabaseHelper";
// Table name
private String QUOTE_TABLE = "quotes";
private final String QUOTE_TABLE = "quotes";

// Table columns
private String ID_KEY = "id";
private String QUOTE_KEY = "quote";
private String AUTHOR_KEY = "author";
private final String ID_KEY = "id";
private final String QUOTE_KEY = "quote";
private final String AUTHOR_KEY = "author";

private FavDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@
// to Application's Databases and making use of the copy
public class GibDatabaseHelper extends SQLiteAssetHelper {

private static String TAG = "GibDatabaseHelper";
private static final String TAG = "GibDatabaseHelper";

// Increment this if you've performed any changes
// to the database (for eg. Changing column info)
// Incrementing it would drop older version's DB
// See onUpgrade() for implementation
private static int DATABASE_VERSION = 1;
private static final int DATABASE_VERSION = 1;

// Give the DB a good name
private static String DATABASE_NAME = "gib.db";
private static final String DATABASE_NAME = "gib.db";

// Static Instance of Database Helper
private static GibDatabaseHelper gibDatabaseHelperInstance;

// Table name
private String QUOTE_TABLE = "quotes";
private final String QUOTE_TABLE = "quotes";

// Table columns
private String ID_KEY = "id";
private String QUOTE_KEY = "quote";
private String AUTHOR_KEY = "author";
private final String ID_KEY = "id";
private final String QUOTE_KEY = "quote";
private final String AUTHOR_KEY = "author";

private GibDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

public class FavQuoteFragment extends Fragment {

private final String TAG = "FavQuoteFragment";
private SharedPreferences sharedPreferences;
private String TAG = "FavQuoteFragment";
private Snackbar snackbar;

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

public class GibQuoteFragment extends Fragment {

private String TAG = "GibQuoteFragment";
private final String TAG = "GibQuoteFragment";

// Quote Fetching Stuff
private ArrayList<Quote> quoteArrayList = new ArrayList<>();
Expand Down Expand Up @@ -343,7 +343,7 @@ private boolean isNetworkAvailable() {
return false;
}

public void showIntroTapTargets(View view) {
private void showIntroTapTargets(View view) {
Typeface boldTypeFace = Typeface
.createFromAsset(getActivity().getAssets(),
"fonts/comfortaa/comfortaa_bold.ttf");
Expand Down Expand Up @@ -418,7 +418,6 @@ public void onDestroyView() {
// GSON can convert De-serialized Data (Java Objects) to Serialized Data (JSON) and vice-versa
// https://github.com/google/gson
String quoteJson = new Gson().toJson(quoteArrayList);
Log.d(TAG, "JSON: " + quoteJson);
// Save the serialized JSON String as a Preference
sharedPreferences.edit()
.putString("quoteData", quoteJson)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// A placeholder fragment containing a simple view.
public class PreferencesFragment extends PreferenceFragmentCompatDividers {

private String TAG = "PreferencesFragment";
private final String TAG = "PreferencesFragment";
private SharedPreferences sharedPreferences;

// Listen for changes in a SharedPreference
Expand Down Expand Up @@ -89,14 +89,14 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
switchPreference.setSummaryOff("You're not receiving daily notifications");

// Hide the TimePickerPreference if it's unchecked
if(!switchPreference.isChecked())
if (!switchPreference.isChecked())
qotdTimePref.setVisible(false);

switchPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Boolean isSwitchOn = (Boolean) newValue;
if(isSwitchOn) // show the TimePicker
if (isSwitchOn) // show the TimePicker
qotdTimePref.setVisible(true);
else {
// Hide the TimePicker, and Cancel all alarms in the BroadcastReceiver
Expand All @@ -119,7 +119,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Boolean isChecked = (Boolean) newValue;
if(isChecked) {
if (isChecked) {
sharedPreferences.edit()
.putBoolean("UPDATES_ENABLED", true)
.apply();
Expand Down

0 comments on commit 669800d

Please sign in to comment.