Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import android.util.Base64;
import android.util.Log;

import com.github.bukaanalytics.common.model.BukaAnalyticsSqliteOpenHelper;
import com.github.bukaanalytics.common.model.Product;
import com.github.bukaanalytics.common.model.Stat;
import com.github.bukaanalytics.common.model.Token;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.koushikdutta.async.future.FutureCallback;
Expand Down Expand Up @@ -70,7 +72,9 @@ public void getAsJSONObject(String URL, final Context context, final JSONObjectC
this.jsonObjectCallback = callback;

// ini perlu di refactor
String appended = "32856476:9RQ3MbQlP4Fac16iv4e";
final BukaAnalyticsSqliteOpenHelper db = BukaAnalyticsSqliteOpenHelper.getInstance(context.getApplicationContext());
Token tokenData = db.getTokenData();
String appended = String.valueOf(tokenData.user_id) + ":" + tokenData.token;
String auth= Base64.encodeToString(appended.getBytes(), Base64.NO_WRAP);

Ion.with(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class BukaAnalyticsSqliteOpenHelper extends SQLiteOpenHelper {
private static final String TABLE_USERS = "users";
private static final String TABLE_PRODUCTS = "products";
private static final String TABLE_STATS = "stats";
private static final String TABLE_TOKENS = "tokens";

//User Table Columns
private static final String KEY_USER_ID = "seller_id";
Expand Down Expand Up @@ -64,6 +65,11 @@ public class BukaAnalyticsSqliteOpenHelper extends SQLiteOpenHelper {
private static final String KEY_STAT_MARKETINTERESTCOUNT = "market_interest_count";
private static final String KEY_STAT_MARKETINTERESTTOTAL = "market_interest_total";

// Tokens Table Columns
private static final String KEY_TOKEN_ID = "id";
private static final String KEY_TOKEN_USER_ID = "user_id";
private static final String KEY_TOKEN_TOKEN = "token";

private static final String MLAB_API_KEY = "8wDpSrJX4XU4tX_ff56Y39I98Tnn4xb0";

// Instance
Expand Down Expand Up @@ -114,9 +120,17 @@ public void onCreate(SQLiteDatabase db) {
"FOREIGN KEY("+KEY_STAT_PRODUCTID+") REFERENCES "+TABLE_PRODUCTS+"("+KEY_PRODUCT_ID+")"+
")";

String CREATE_TOKENS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_TOKENS +
"(" +
KEY_TOKEN_ID + " INTEGER PRIMARY KEY," +
KEY_TOKEN_USER_ID + " INTEGER," +
KEY_TOKEN_TOKEN + " TEXT" +
")";

db.execSQL(CREATE_USERS_TABLE);
db.execSQL(CREATE_PRODUCTS_TABLE);
db.execSQL(CREATE_STAT_TABLE);
db.execSQL(CREATE_TOKENS_TABLE);
}

@Override
Expand All @@ -126,6 +140,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_STATS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TOKENS);
onCreate(db);
}
}
Expand Down Expand Up @@ -273,6 +288,31 @@ public List<Product> getProducts(int userId) {
return products;
}

public Token getTokenData() {
Token tokenData = new Token(0,0,"");
String GET_ACTIVE_TOKEN = String.format("SELECT * FROM %s ORDER BY %s DESC LIMIT 1", TABLE_TOKENS, KEY_TOKEN_ID);

SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery(GET_ACTIVE_TOKEN, null);
try{
if (cursor != null && cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
tokenData = new Token(cursor.getInt(cursor.getColumnIndex(KEY_TOKEN_ID)),
cursor.getInt(cursor.getColumnIndex(KEY_TOKEN_USER_ID)),
cursor.getString(cursor.getColumnIndex(KEY_TOKEN_TOKEN)));
}
}
} catch (Exception e) {
Log.d(TAG, "getTokenData() Error: " + e.getMessage());
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}

return tokenData;
}

public List<Stat> getStats(String productId) {
List<Stat> stats = new ArrayList<>();

Expand Down Expand Up @@ -421,4 +461,5 @@ public ArrayList<Cursor> getData(String Query){
return alc;
}
}

}
17 changes: 17 additions & 0 deletions app/src/main/java/com/github/bukaanalytics/common/model/Token.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.bukaanalytics.common.model;

/**
* Created by habibridho on 5/28/17.
*/

public class Token {
public int id;
public int user_id;
public String token;

public Token(int _id, int _user_id, String _token) {
id = _id;
user_id = _user_id;
token = _token;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.RemoteViews;

import com.github.bukaanalytics.R;
import com.github.bukaanalytics.common.HTTPRequestHelper;
import com.github.bukaanalytics.common.model.BukaAnalyticsSqliteOpenHelper;
import com.github.bukaanalytics.common.model.Token;
import com.github.bukaanalytics.widget.widgetalarm.WidgetAlarm;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
Expand All @@ -29,26 +32,35 @@ public class BukaAnalyticsAppWidgetProvider extends AppWidgetProvider {

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final BukaAnalyticsSqliteOpenHelper db = BukaAnalyticsSqliteOpenHelper.getInstance(context.getApplicationContext());
final int count = appWidgetIds.length;
int newOrder = 0;

for (int i = 0; i < count; i++) {
int widgetId = appWidgetIds[i];

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);

getUnread(context, appWidgetManager, remoteViews, widgetId);
getNotifByType("report", context, appWidgetManager, remoteViews, widgetId);
getNotifByType("nego", context, appWidgetManager, remoteViews, widgetId);
Token tokenData = db.getTokenData();
if (tokenData.id == 0) {
remoteViews.setViewVisibility(R.id.textView_not_login, View.VISIBLE);
remoteViews.setViewVisibility(R.id.layout_stat, View.GONE);
} else {
remoteViews.setViewVisibility(R.id.textView_not_login, View.GONE);
remoteViews.setViewVisibility(R.id.layout_stat, View.VISIBLE);
getUnread(context, appWidgetManager, remoteViews, widgetId);
getNotifByType("report", context, appWidgetManager, remoteViews, widgetId);
getNotifByType("nego", context, appWidgetManager, remoteViews, widgetId);
}

Intent intent = new Intent(context, BukaAnalyticsAppWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.layout_container, pendingIntent);

// appWidgetManager.updateAppWidget(widgetId, remoteViews);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}

Expand Down
14 changes: 13 additions & 1 deletion app/src/main/res/layout/widget.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="8dp"
Expand All @@ -11,16 +12,27 @@
android:id="@+id/textView_header_widget"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:padding="2dp"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:text="BukaAnalytics"
android:textStyle="bold"/>

<TextView
android:id="@+id/textView_not_login"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#FFFFFF"
android:text="Please login to get your stats" />

<LinearLayout
android:id="@+id/layout_stat"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingBottom="8dp"
android:orientation="horizontal">

Expand Down
8 changes: 4 additions & 4 deletions js-src/lib/BLApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ class BLApi {
}

static getTransactions(params, successCallback, errorCallback) {
let { perPage, page, since } = params
let { userId, token, perPage, page, since } = params

return this.sendApiRequest({
method: 'get',
url: 'https://api.bukalapak.com/v2/transactions.json',
auth: {
username: '6214450',
password: 'I1vy09dhNuWx3G0CGiN'
username: userId,
password: token
},
params: {
page: page,
Expand All @@ -66,7 +66,7 @@ class BLApi {

if (resData.transactions.length == perPage) {
let newParam = {
perPage, since,
userId, token, perPage, since,
page: page+1
}
this.getTransactions(newParam, successCallback, errorCallback)
Expand Down
43 changes: 43 additions & 0 deletions js-src/lib/BLSqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const config = {
users: 'users',
products: 'products',
stats: 'stats',
tokens: 'tokens'
},
col: {
users: {
Expand All @@ -31,6 +32,11 @@ const config = {
interest_count: 'interest_count',
interest_total: 'interest_total',
},
tokens: {
id: 'id',
user_id: 'user_id',
token: 'token'
}
},
};

Expand Down Expand Up @@ -164,6 +170,16 @@ class BLSqlite {
});
}

getActiveToken(callback) {
this.db.executeSql(`
SELECT * from tokens
ORDER BY id DESC
LIMIT 1
`, [], result => {
callback(this.parseActiveToken(result))
})
}

createTablesIfNotExists() {
this.db.executeSql(`
CREATE TABLE IF NOT EXISTS users (
Expand Down Expand Up @@ -191,6 +207,12 @@ class BLSqlite {
interest_count INTEGER,
interest_total INTEGER
)`);
this.db.executeSql(`
CREATE TABLE IF NOT EXISTS tokens(
id INTEGER PRIMARY KEY,
user_id INTEGER,
token TEXT
)`);
}

insertUser(param) {
Expand Down Expand Up @@ -228,6 +250,16 @@ class BLSqlite {
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`, [param.date, param.day_name, param.product_id, param.view_count, param.view_total, param.sold_count, param.sold_total, param.interest_count, param.interest_total]);
}

insertToken(param) {
this.db.executeSql(`
INSERT INTO ${config.tables.tokens} (
${config.col.tokens.user_id},
${config.col.tokens.token}
) VALUES (?, ?)
`, [param.userId, param.token]);
}

// ==================
// Private Functions
// ==================
Expand Down Expand Up @@ -291,6 +323,17 @@ class BLSqlite {
return retval;
}

parseActiveToken(result) {
const retval = {}
const result_length = results.rows.length;
for (let i = 0; i < result_length; i++) {
const row = results.rows.item(i);
retval.userId = row.user_id;
retval.token = row.token;
}
return retval;
}

}

export const Sqlite = new BLSqlite();
4 changes: 4 additions & 0 deletions js-src/redux/dashboard/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ export function refreshData(refreshType) {
dispatch(getSqliteData(latestDate));
console.log(Sqlite);

let userData = getState().user

return BLApi.getTransactions({
userId: userData.userId,
token: userData.token,
perPage: perPage,
page: 1,
since: since
Expand Down
6 changes: 6 additions & 0 deletions js-src/redux/user/actions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BLApi from '@lib/BLApi'
import { Sqlite } from '@lib/BLSqlite';

// User action types
export const USER = {
Expand All @@ -24,6 +25,11 @@ function isLoading() {
}

function loginSuccess(data) {
Sqlite.insertToken({
userId: data.user_id,
token: data.token
})

return {
type: USER.LOGIN_SUCCESS,
payload: {
Expand Down