SkillAgentSearch skills...

Multiutillib

A library to help with database setup; working with queries like insert, create, delete, drop, select; for using rest api consumtion; shared preferences; image zoom in and zoom out, applying font to the entire layout, checking if internet connection is available or not, setting an image view in square shape, circular shape, with initial letter or avatat, and replacing null in a string and replacing true or false with 1 or 0, switch button, custom toast message, datetime utils, location services, location address, validation of different types, set max length, set character counter, check if url is valid and much more. Check out the README.md for more information and latest update. You can check out the website 'https://amitjangid80.github.io/multiutillib/' as well.

Install / Use

/learn @amitjangid80/Multiutillib

README

multiutillib

Setup

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

allprojects{
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

Step 2. Add the dependency

dependencies {
    ...
    implementation 'com.github.amitjangid80:multiutillib:v1.7.3'
}

Using maven:

<repositories>
  <repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
  </repository>
</repositories>

Step 2. Add the dependency

<dependency>
   <groupId>com.github.amitjangid80</groupId>
   <artifactId>multiutillib</artifactId>
   <version>v1.7.3</version>
<dependency>

Add compileOptions in your gradle file for supporting this library for Java 8.

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

If you get Manifest merger failed with multiple errors, see logs error while running or syncing gradle then you just have to create a class which extends android.support.v4.content.FileProvider.

Example:

import android.support.v4.content.FileProvider;

public class MyFileProvider extends FileProvider
{

}

// after you have created this file then you have to register this in your manifest file.
Example: 
<provider
    android:name="android.support.v4.content.FileProvider" // replace this line with the line below
    android:name=".MyFileProvider"
    android:authorities="com.example.file_provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
	android:name="android.support.FILE_PROVIDER_PATHS"
	android:resource="@xml/file_provider_path" />
</provider>

Usage

ProjectApplication Class

You can create an Application class for your project if you want and set up some utils there.

public class ProjectApplication extends Application
{
    @Override
    public void onCreate()
    {
        super.onCreate();
        
        // Set up SharedPreferenceData
        // the key in the shared preference should be of same name as given below.
        // these two are the constant values which will be used in the entire application
        String apiPath = "https://www.example.com/api/";
        
        SharedPreferenceData sharedPreferenceData = new SharedPreferenceData(this);
        sharedPreferenceData.setValue("apiPath", apiPath);
        sharedPreferenceData.setValue("dbName", "YourDatabaseName");
    }
}

SharedPreferenceData

// use it in the activity or class you want to.
SharedPreferenceData sharedPreferenceData = new SharedPreferenceData(context);

// FOR SETTING DATA TO SHARED PREFERENCE.
// generic method storing string values.
sharedPreferenceData.setValue("keyname", "value for that keyname");

// specific for string values.
sharedPreferenceData.setStrValue("keyname", "value for that keyname");

// specific for int values.
sharedPreferenceData.setIntValue("keyname", 1);

// specific for boolean values.
sharedPreferenceData.setBooleanValue("keyname", true);

// spefici for float values.
sharedPreferenceData.setFloatValue("keyname", 1.0);

// specific for long values.
sharedPreferenceData.setLongValue("keyname", 123456789);

// specific for String set values.
sharedPreferenceData.setStrSetValue("keyname", Set<String> value);

// FOR GETTING DATA FROM SHARED PREFERENCE.
// all the below methods will return some defaults values.

// generic method for getting values returns 0 by default as string type.
sharedPreferenceData.getValue(key);

// generic method for getting values returns defaultValue passed if not found.
sharedPreferenceData.getValue(key, defaultValue);

// specific for getting string values returns 0 by default as string type.
sharedPreferenceData.getStrValue(key);

// generic method for getting values returns defaultValue passed if not found.
sharedPreferenceData.getStrValue(key, defaultValue);

// specific for getting int values returns 0 by default as int type.
sharedPreferenceData.getIntValue(key);

// specific for getting boolean values returns false by default as int type.
sharedPreferenceData.getBooleanValue(key);

// specific for getting float values returns 0f by default as int type.
sharedPreferenceData.getFloatValue(key);

// specific for getting long values returns 0 by default as int type.
sharedPreferenceData.getLongValue(key);

// specific for getting set of string values returns null by default as int type.
sharedPreferenceData.getStrSetValue(key);

ApiServices

Before using this class first make sure to set up the apiPath into shared preference class. Shown above in ProjectApplication Section.

/**
 * Make API Call method
 *
 * this method will handle all the api operations like GET OR POST OR PUT
 * parameters for this method are as follows
 *
 ************************************************************************************************
 *
 *** parameter #1
 *** @param apiName - this parameter will contain the name of the api with some path or without any path
 *                  - FOR EXAMPLE: with path - MobileAPI/RegisterMobile
 *                            without path - RegisterMobile
 *
 *** parameter #2
 *** @param requestMethod - this parameter will be passed with 3 values
 *                          #1 - POST OR
 *                          #2 - PUT OR
 *                          #3 - GET
 *
 *** parameter #3
 *** @param parameters - this parameter will contain parameters which will be passed to the api
 *                       and required by the api to work properly
 *
 *** parameter #4
 *** @param values - this parameter will hold the parameters in JSON format
 *                   this will be the data which we need to pass along with the api
 *                 - FOR EXAMPLE: MobileNumber = 9999999999, OTP = 9999, etc.
 *
 *** parameter #5
 *** @param hasToken - this parameter should be passed with true or false
 *                   - this parameter will be used if the api requires some token to work with
 *                   - if the api requires token then this has to be true
 *                   - if the api doesn't require token then this has to be false
 *
 * @return Pair of integer and string which contains response value and response code from the server
 *
 ************************************************************************************************
**/
ApiServices apiServices = new ApiServices(context);
apiServices.makeApiCall(apiName, requestMethod, parameters, jsonObject, hasToken).second;

DBHelper

Set the dbName in sharedPreferenceData with key name 'dbName'. Shown above in ProjectApplication section.

// this method will check if a table exists in database
// true - if table exists in database
// false - if table not exists in database
DBHelper dbHelper = new DBHelper(context);
dbHelper.isTableExists(tableName);

// this method will check if a column in a table exists or not
// returns true if column exists in table or false if not
dbHelper.checkIsColumnExists(tableName, columnName);

// parameters to be passed are as follows:
// 
// tableName - table on which query should be performed.
//
// operation - c for creating table, 
//             i - for inserting records in table
//             d - for deleting records in table
//             u - for updating records in table
//             dr - for droping the table
//
// values - it is of type LinkedHashMap<String, String>
//          declaration as below
//          LinkedHashMap<String, String> vauels = new LinkedHashMap<>();
//          now just add your column name and values or data types
//
//          example: values.put("ID", "INTEGER PRIMARY KEY AUTOINCREMENT");
//                   values.put("firstName", "TEXT");
//
//          use this way when creating a table.
//          when inserting a record in the table
//          example: value.put("firstName", "'Amit'"); 
//          notice the single quotes inside the double quotes where name is written.
//          for string value you should pass with single quotes and integer value can be passed directly
//
// hasConditions - set this value as true or false
//                 if you are setting this parameter as true
//                 then you must also pass the conditionalValues parameter as well
//                 else the query won't work
//
//                 this parameter is useful when using conditional queries like update or delete
//                 where you create another linked hash map like the one above can just pass the coloumn name or conditions with values
//                 the values should be passed the way we pass in insert query.
//
// conditionalValues - when passing this parameter make sure
// 
// return - the method will return true or false if the operation is successful or failed.
//
dbHelper.executeDatabaseOperation(tableName, operation, values, hasConditions, conditionalValues);

// this method can be used for inserting bulk data into table using database transaction
// pass the name of the table where you want to insert data
// and also pass the total number of columns for which you want to insert data in.
dbHelper.insertDataWithTransaction("tableName", 10);

Example Usage of above method is:
// first we are generating data for inserting into the table
for (int i = 0; i < 20000; i++)
{
    dbHelper.addDataForTable(new DbData("columnName1", "data"))
            .addDataForTable(new DbData("columnName2", "data"))
            .addDataForTable(new DbData("columnName3", "data"))
            .addDataForTable(new DbData("columnName4", "data"))
            .addDataForTable(new DbData("columnName5", "data"))
            .addDataForTable(new DbData("columnName6", "data"))
            .addDataForTable(new DbData("columnName7", "data"))
            .addDataForTable(new DbData("co

Related Skills

View on GitHub
GitHub Stars11
CategoryData
Updated2y ago
Forks3

Languages

Java

Security Score

80/100

Audited on Apr 27, 2023

No findings