Skip to content

Commit bbd9e09

Browse files
committed
1. Added Floating Hint Edit Text View.
2. Added more insert and update methods in db with return values.
1 parent 6a8b12f commit bbd9e09

File tree

15 files changed

+1113
-3
lines changed

15 files changed

+1113
-3
lines changed

.idea/caches/build_file_checksums.ser

0 Bytes
Binary file not shown.

.idea/codeStyles/codeStyleConfig.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ dependencies {
4949
implementation "com.android.support:appcompat-v7:$supportLibraryVersion"
5050
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
5151

52+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1'
53+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0'
54+
5255
// RxJava dependencies
5356
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
5457

app/src/main/java/com/amit/db/DBHelper.java

Lines changed: 212 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,58 @@ public DBHelper insertData(String tableName)
11131113
return this;
11141114
}
11151115

1116+
//#region COMMENTS FOR insertData method
1117+
/**
1118+
* 2019 January 08 - Tuesday - 04:03 PM
1119+
* insert data with return id method
1120+
*
1121+
* this method is used for inserting records into table
1122+
*
1123+
* @param tableName - name of the table for inserting records
1124+
*
1125+
* @return inserted row id if successful or -1 if not inserted
1126+
**/
1127+
//#endregion COMMENTS FOR insertData method
1128+
public long insertDataWithReturnId(String tableName)
1129+
{
1130+
// checking if table name was provided or not
1131+
if (tableName == null || tableName.isEmpty())
1132+
{
1133+
if (dbDataArrayList != null)
1134+
{
1135+
dbDataArrayList.clear();
1136+
}
1137+
1138+
Log.e(TAG, "insertData: Table name was null or empty.");
1139+
return -1;
1140+
}
1141+
1142+
// checking if data was provided or not for inserting in database
1143+
if (dbDataArrayList == null || dbDataArrayList.size() == 0)
1144+
{
1145+
Log.e(TAG, "insertData: No data provided for inserting.");
1146+
return -1;
1147+
}
1148+
1149+
// content values for putting column name
1150+
// and data for inserting into database table
1151+
ContentValues contentValues = new ContentValues();
1152+
1153+
// loop for no of data to be inserted
1154+
for (int i = 0; i < dbDataArrayList.size(); i++)
1155+
{
1156+
// adding column names and column data into content values
1157+
contentValues.put(dbDataArrayList.get(i).columnName, dbDataArrayList.get(i).columnData.toString());
1158+
}
1159+
1160+
// executing inserting statement for inserting records in table
1161+
long insertedId = db.getWritableDatabase().insert(tableName, null, contentValues);
1162+
db.close();
1163+
1164+
dbDataArrayList = new ArrayList<>();
1165+
return insertedId;
1166+
}
1167+
11161168
//#region COMMENTS FOR insertDataWithTransaction method
11171169
/**
11181170
* 2019 January 09 - Wednesday - 06:49 PM
@@ -1470,9 +1522,6 @@ else if (object instanceof JSONArray)
14701522
String columnName = iterator.next();
14711523
String columnData = jsonObject.getString(columnName);
14721524

1473-
// Log.e(TAG, "insertData: name of column from json is: " + columnName);
1474-
// Log.e(TAG, "insertData: value of column from json is: " + columnData);
1475-
14761525
this.addDataForTable(new DbData(columnName, columnData));
14771526
}
14781527
}
@@ -1489,6 +1538,80 @@ else if (object instanceof JSONArray)
14891538
}
14901539
}
14911540

1541+
//#region COMMENTS FOR insertDataWithJson method
1542+
/**
1543+
* 2019 Apr 25 - Thursday - 12:25 PM
1544+
* insert data with json with return id method
1545+
*
1546+
* this method will insert data using JSON Array or JSON Object
1547+
*
1548+
* @param tableName - name of the table to insert data in
1549+
*
1550+
* @param object - JSON Object or JSON Array of records and columns to be inserted
1551+
*
1552+
* @return True or False for success for failure in inserting records
1553+
**/
1554+
//#endregion COMMENTS FOR insertDataWithJson method
1555+
public long insertDataWithJsonWithReturnId(String tableName, Object object)
1556+
{
1557+
try
1558+
{
1559+
JSONArray jsonArray = new JSONArray();
1560+
1561+
if (object == null)
1562+
{
1563+
Log.e(TAG, "insertData: object value cannot be null.");
1564+
return -1;
1565+
}
1566+
1567+
if (object instanceof ArrayList)
1568+
{
1569+
Log.e(TAG, "insertDataWithJson: cannot parse array list, you can use json object or json array.");
1570+
return -1;
1571+
}
1572+
1573+
if (object instanceof JSONObject)
1574+
{
1575+
Iterator<String> iterator = ((JSONObject) object).keys();
1576+
1577+
while (iterator.hasNext())
1578+
{
1579+
String key = iterator.next();
1580+
jsonArray = ((JSONObject) object).getJSONArray(key);
1581+
1582+
Log.e(TAG, "insertData: json array for " + key + " is: " + jsonArray);
1583+
}
1584+
}
1585+
else if (object instanceof JSONArray)
1586+
{
1587+
jsonArray = (JSONArray) object;
1588+
}
1589+
1590+
for (int i = 0; i < jsonArray.length(); i++)
1591+
{
1592+
JSONObject jsonObject = jsonArray.getJSONObject(i);
1593+
Iterator<String> iterator = jsonObject.keys();
1594+
1595+
while (iterator.hasNext())
1596+
{
1597+
String columnName = iterator.next();
1598+
String columnData = jsonObject.getString(columnName);
1599+
1600+
this.addDataForTable(new DbData(columnName, columnData));
1601+
}
1602+
}
1603+
1604+
return this.insertDataWithReturnId(tableName);
1605+
}
1606+
catch (Exception e)
1607+
{
1608+
Log.e(TAG, "insertData: exception while inserting data using json:\n");
1609+
e.printStackTrace();
1610+
1611+
return -1;
1612+
}
1613+
}
1614+
14921615
//#region COMMENTS FOR insertDataWithJsonAndTransaction method
14931616
/**
14941617
* 2019 Apr 25 - Thursday - 12:25 PM
@@ -1641,6 +1764,92 @@ public DBHelper updateData(String tableName, String whereClause, String whereArg
16411764

16421765
return this;
16431766
}
1767+
1768+
//#region COMMENTS FOR updateData method
1769+
/**
1770+
* 2019 January 08 - Tuesday - 04:28 PM
1771+
* update data with return id method
1772+
*
1773+
* @param tableName - name of the table on which update query is to be performed
1774+
*
1775+
* @param whereClause - name of the column to check whether the record is present so the data is updated
1776+
* pass this parameter in the way given in example below
1777+
* Ex: code = ? or ID = ? etc // this is important
1778+
*
1779+
* @param whereArgs - data of the column name provided to check if record is present for data update
1780+
* here you need to pass the data for the corresponding where clause
1781+
* Ex: 1 or 2 etc
1782+
*
1783+
* this method will update records of the table in database
1784+
* this method uses database's update method for updating records
1785+
*
1786+
* parameter whereClause and whereArgs must be passed in the form given
1787+
*
1788+
* @return -1 if failed to update the record
1789+
**/
1790+
//#endregion COMMENTS FOR updateData method
1791+
public long updateDataWithReturnId(String tableName, String whereClause, String whereArgs)
1792+
{
1793+
// checking if table name was provided or not
1794+
if (tableName == null || tableName.isEmpty())
1795+
{
1796+
if (dbDataArrayList != null)
1797+
{
1798+
dbDataArrayList.clear();
1799+
}
1800+
1801+
Log.e(TAG, "updateData: Table name was null or empty.");
1802+
return -1;
1803+
}
1804+
1805+
// checking if column name was provided or not
1806+
if (whereClause == null || whereClause.isEmpty())
1807+
{
1808+
if (dbDataArrayList != null)
1809+
{
1810+
dbDataArrayList.clear();
1811+
}
1812+
1813+
Log.e(TAG, "updateData: Column name was null or empty.");
1814+
return -1;
1815+
}
1816+
1817+
// checking if data was provided or not
1818+
if (dbDataArrayList == null || dbDataArrayList.size() == 0)
1819+
{
1820+
Log.e(TAG, "updateData: Data was not provided for updating records.");
1821+
return -1;
1822+
}
1823+
1824+
// content values for putting column name
1825+
// and data for inserting into database table
1826+
ContentValues contentValues = new ContentValues();
1827+
1828+
// loop for no of data provided
1829+
for (int i = 0; i < dbDataArrayList.size(); i++)
1830+
{
1831+
// adding column names and column data into content values
1832+
contentValues.put(dbDataArrayList.get(i).columnName, dbDataArrayList.get(i).columnData.toString());
1833+
}
1834+
1835+
long updatedId;
1836+
1837+
// checking if column data was provided or not
1838+
if (whereArgs != null && whereArgs.isEmpty())
1839+
{
1840+
updatedId = db.getWritableDatabase().update(tableName, contentValues, whereClause, new String[]{whereArgs});
1841+
}
1842+
else
1843+
{
1844+
// you can directly pass the values to where clause
1845+
updatedId = db.getWritableDatabase().update(tableName, contentValues, whereClause, null);
1846+
}
1847+
1848+
db.close();
1849+
dbDataArrayList = new ArrayList<>();
1850+
1851+
return updatedId;
1852+
}
16441853

16451854
//#region COMMENTS FOR deleteTable method
16461855
/**
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.amit.ui.flotingHintEditText
2+
3+
/**
4+
* Created by AMIT JANGID on 2019-08-06.
5+
**/
6+
7+
const val TEN_DP = 10
8+
const val FORTY_FIVE_DP = 45
9+
const val DEFAULT_TEXT_SIZE = 16f
10+
const val DEFAULT_HINT_OPACITY = 0.7F
11+
const val DEFAULT_ANIMATION_DURATION = 400
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.amit.ui.flotingHintEditText
2+
3+
import android.annotation.SuppressLint
4+
import android.content.Context
5+
import android.support.v7.widget.AppCompatEditText
6+
import android.util.AttributeSet
7+
import android.view.KeyEvent
8+
import android.view.MotionEvent
9+
10+
/**
11+
* Created by AMIT JANGID on 2019-08-06.
12+
**/
13+
@SuppressLint("ClickableViewAccessibility")
14+
class FloatingEditText : AppCompatEditText
15+
{
16+
private var clickable: Boolean = true
17+
18+
private var onBackPressed: Runnable? = null
19+
20+
constructor(context: Context?) : super(context)
21+
22+
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
23+
24+
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
25+
26+
/**
27+
* 2019 August 06 - Tuesday - 11:13 AM
28+
* On Back Pressed Listener method
29+
*
30+
* custom event for clicking back when on this view is active
31+
**/
32+
fun setOnBackPressedListener(onBackPressed: Runnable)
33+
{
34+
this.onBackPressed = onBackPressed
35+
}
36+
37+
override fun setClickable(clickable: Boolean)
38+
{
39+
super.setClickable(clickable)
40+
this.clickable = clickable
41+
}
42+
43+
override fun onTouchEvent(event: MotionEvent?): Boolean
44+
{
45+
return if (clickable)
46+
{
47+
super.onTouchEvent(event)
48+
}
49+
else
50+
{
51+
false
52+
}
53+
}
54+
55+
override fun onKeyPreIme(keyCode: Int, event: KeyEvent?): Boolean
56+
{
57+
if (event?.keyCode == KeyEvent.KEYCODE_BACK)
58+
{
59+
this.clearFocus()
60+
onBackPressed?.run()
61+
}
62+
63+
return super.onKeyPreIme(keyCode, event)
64+
}
65+
}

0 commit comments

Comments
 (0)