Commit d40aa00fa7f5308022d95185aa74525f490d399f
1 parent
000919da
- remove book if there is no book from scrapping result
Showing
6 changed files
with
67 additions
and
19 deletions
app/src/main/java/net/devfac/userstory/BookShelfCursorAdapter.java
... | ... | @@ -4,22 +4,16 @@ import android.content.Context; |
4 | 4 | import android.database.Cursor; |
5 | 5 | import android.graphics.Bitmap; |
6 | 6 | import android.graphics.BitmapFactory; |
7 | -import android.os.AsyncTask; | |
8 | 7 | import android.view.LayoutInflater; |
9 | 8 | import android.view.View; |
10 | 9 | import android.view.ViewGroup; |
11 | 10 | import android.widget.CursorAdapter; |
12 | 11 | import android.widget.ImageView; |
13 | -import android.widget.LinearLayout; | |
14 | 12 | import android.widget.TextView; |
15 | 13 | |
16 | 14 | import net.devfac.userstory.Utils.Database; |
17 | -import net.devfac.userstory.Utils.Logger; | |
18 | 15 | |
19 | 16 | import java.io.File; |
20 | -import java.io.IOException; | |
21 | -import java.net.MalformedURLException; | |
22 | -import java.net.URL; | |
23 | 17 | |
24 | 18 | /** |
25 | 19 | * Adapter for database |
... | ... | @@ -43,7 +37,7 @@ public class BookShelfCursorAdapter extends CursorAdapter { |
43 | 37 | ImageView shelfCoverImage = (ImageView)view.findViewById(R.id.shelf_list_item_cover); |
44 | 38 | TextView shelfTitle = (TextView)view.findViewById(R.id.shelf_lis_item_title); |
45 | 39 | |
46 | - String registeredId = cursor.getString( cursor.getColumnIndex(Database.BookShelfTable.RESISTERED_ID)); | |
40 | + String registeredId = cursor.getString( cursor.getColumnIndex(Database.BookShelfTable.REGISTERED_ID)); | |
47 | 41 | String coverUrlAdress = cursor.getString( cursor.getColumnIndex(Database.BookShelfTable.COVER_URL)); |
48 | 42 | String title = cursor.getString( cursor.getColumnIndex(Database.BookShelfTable.TITLE)); |
49 | 43 | ... | ... |
app/src/main/java/net/devfac/userstory/FSM/StateContext.java
... | ... | @@ -59,6 +59,8 @@ public class StateContext { |
59 | 59 | |
60 | 60 | public STATE_RESULT mStateResult; |
61 | 61 | |
62 | + private ArrayList<BookShelfInfo> mBookShelfInfoArray; | |
63 | + | |
62 | 64 | public enum STATE_RESULT{ |
63 | 65 | LOGIN_FAIL, |
64 | 66 | LOGIN_SUCCESS, |
... | ... | @@ -286,6 +288,7 @@ public class StateContext { |
286 | 288 | ) + "1"); |
287 | 289 | } |
288 | 290 | }); |
291 | + mBookShelfInfoArray = new ArrayList<>(); | |
289 | 292 | } |
290 | 293 | } |
291 | 294 | |
... | ... | @@ -323,10 +326,11 @@ public class StateContext { |
323 | 326 | titleArray.get(index) |
324 | 327 | ); |
325 | 328 | |
326 | - if( !mDbOpenHelper.exist(bookInfo.getResisteredId()) ){ | |
327 | - new DownloadCoverImage().execute(bookInfo.getCoverUrl()); | |
328 | - mDbOpenHelper.insert(bookInfo); | |
329 | - } | |
329 | + mBookShelfInfoArray.add(bookInfo); | |
330 | +// if( !mDbOpenHelper.exist(bookInfo.getResisteredId())) { | |
331 | +// new DownloadCoverImage().execute(bookInfo.getCoverUrl()); | |
332 | +// mDbOpenHelper.insert(bookInfo); | |
333 | +// } | |
330 | 334 | |
331 | 335 | } |
332 | 336 | |
... | ... | @@ -353,6 +357,32 @@ public class StateContext { |
353 | 357 | }); |
354 | 358 | } |
355 | 359 | else{ |
360 | + ArrayList<BookShelfInfo> mDbEntries = mDbOpenHelper.getAllEntries(); | |
361 | + | |
362 | + for( BookShelfInfo bookInfo : mBookShelfInfoArray){ | |
363 | + if( !mDbOpenHelper.exist(bookInfo.getResisteredId())) { | |
364 | + new DownloadCoverImage().execute(bookInfo.getCoverUrl()); | |
365 | + mDbOpenHelper.insert(bookInfo); | |
366 | + } | |
367 | + } | |
368 | + | |
369 | + boolean is_contained = false; | |
370 | + | |
371 | + for( BookShelfInfo dbBookInfo : mDbEntries ){ | |
372 | + for( BookShelfInfo newBookInfo : mBookShelfInfoArray){ | |
373 | + if( dbBookInfo.getResisteredId().equals(newBookInfo.getResisteredId())){ | |
374 | + is_contained = true; | |
375 | + break; | |
376 | + } | |
377 | + } | |
378 | + | |
379 | + if( !is_contained ){ | |
380 | + mDbOpenHelper.delete(dbBookInfo.getResisteredId()); | |
381 | + } | |
382 | + | |
383 | + is_contained = false; | |
384 | + } | |
385 | + mStateResult = STATE_RESULT.SYNC_SUCCESS; | |
356 | 386 | processEvent(Action.RES_SYNC, null); |
357 | 387 | } |
358 | 388 | } |
... | ... | @@ -466,7 +496,7 @@ public class StateContext { |
466 | 496 | Logger.i("Book Search Success"); |
467 | 497 | mStateResult = STATE_RESULT.BOOK_SEARCH_SUCCESS; |
468 | 498 | } |
469 | - else{ | |
499 | + else { | |
470 | 500 | Logger.e("Book Search Fail"); |
471 | 501 | mStateResult = STATE_RESULT.BOOK_SEARCH_FAIL; |
472 | 502 | } | ... | ... |
app/src/main/java/net/devfac/userstory/FSM/state/StateSync.java
... | ... | @@ -56,7 +56,9 @@ public class StateSync implements State { |
56 | 56 | |
57 | 57 | @Override |
58 | 58 | public void onExit(Object output, StateContext.STATE_RESULT stateResult) { |
59 | - | |
59 | + for( StateEventListener listener:mStateContext.mStateEventListenerArray){ | |
60 | + listener.onExit(this.getClass(), output, stateResult); | |
61 | + } | |
60 | 62 | } |
61 | 63 | |
62 | 64 | public String toString(){ | ... | ... |
app/src/main/java/net/devfac/userstory/Utils/Database.java
... | ... | @@ -9,7 +9,7 @@ import android.provider.BaseColumns; |
9 | 9 | public class Database { |
10 | 10 | |
11 | 11 | public static final class BookShelfTable implements BaseColumns { |
12 | - public static final String RESISTERED_ID = "registered_id"; | |
12 | + public static final String REGISTERED_ID = "registered_id"; | |
13 | 13 | public static final String COVER_URL = "cover_url"; |
14 | 14 | public static final String TITLE = "title"; |
15 | 15 | |
... | ... | @@ -17,7 +17,7 @@ public class Database { |
17 | 17 | public static final String _CREATE = |
18 | 18 | "create table " + _TABLE_NAME + "(" |
19 | 19 | + _ID + " integer primary key autoincrement, " |
20 | - + RESISTERED_ID + " text not null, " | |
20 | + + REGISTERED_ID + " text not null, " | |
21 | 21 | + COVER_URL + " text not null, " |
22 | 22 | + TITLE + " text not null );" |
23 | 23 | ; | ... | ... |
app/src/main/java/net/devfac/userstory/Utils/DbOpenHelper.java
... | ... | @@ -10,6 +10,7 @@ import net.devfac.userstory.Constants; |
10 | 10 | import net.devfac.userstory.types.BookShelfInfo; |
11 | 11 | |
12 | 12 | import java.sql.SQLException; |
13 | +import java.util.ArrayList; | |
13 | 14 | |
14 | 15 | /** |
15 | 16 | * Created by Onether on 15. 8. 1.. |
... | ... | @@ -56,7 +57,7 @@ public class DbOpenHelper { |
56 | 57 | Logger.i("insert BookShelfInfo : " + info.getTitle()); |
57 | 58 | |
58 | 59 | ContentValues values = new ContentValues(); |
59 | - values.put(Database.BookShelfTable.RESISTERED_ID, info.getResisteredId()); | |
60 | + values.put(Database.BookShelfTable.REGISTERED_ID, info.getResisteredId()); | |
60 | 61 | values.put(Database.BookShelfTable.COVER_URL, info.getCoverUrl()); |
61 | 62 | values.put(Database.BookShelfTable.TITLE, info.getTitle()); |
62 | 63 | |
... | ... | @@ -66,14 +67,14 @@ public class DbOpenHelper { |
66 | 67 | } |
67 | 68 | |
68 | 69 | public void delete(String registered_id){ |
69 | - mDB.delete(Database.BookShelfTable._TABLE_NAME, Database.BookShelfTable.RESISTERED_ID + "=?", new String[]{registered_id}); | |
70 | + mDB.delete(Database.BookShelfTable._TABLE_NAME, Database.BookShelfTable.REGISTERED_ID + "=?", new String[]{registered_id}); | |
70 | 71 | |
71 | 72 | refreshCursor(); |
72 | 73 | } |
73 | 74 | |
74 | 75 | public boolean exist(String registed_id){ |
75 | 76 | String queryExist = String.format( "SELECT * FROM %s", Database.BookShelfTable._TABLE_NAME + " where " |
76 | - + Database.BookShelfTable.RESISTERED_ID + " = " + registed_id); | |
77 | + + Database.BookShelfTable.REGISTERED_ID + " = " + registed_id); | |
77 | 78 | Cursor cursorExist = mDB.rawQuery(queryExist, null); |
78 | 79 | |
79 | 80 | if( cursorExist.getCount() <= 0){ |
... | ... | @@ -95,4 +96,25 @@ public class DbOpenHelper { |
95 | 96 | } |
96 | 97 | |
97 | 98 | public Cursor getCursor(){ return mCursor; } |
99 | + | |
100 | + public ArrayList<BookShelfInfo> getAllEntries(){ | |
101 | + ArrayList<BookShelfInfo> entries = new ArrayList<>(); | |
102 | + | |
103 | + if( mCursor.moveToFirst() ){ | |
104 | + do{ | |
105 | + BookShelfInfo bookShelfInfo = new BookShelfInfo( | |
106 | + mCursor.getString(mCursor.getColumnIndex(Database.BookShelfTable.REGISTERED_ID)), | |
107 | + mCursor.getString(mCursor.getColumnIndex(Database.BookShelfTable.COVER_URL)), | |
108 | + mCursor.getString(mCursor.getColumnIndex(Database.BookShelfTable.TITLE)) | |
109 | + ); | |
110 | + | |
111 | + entries.add(bookShelfInfo); | |
112 | + }while(mCursor.moveToNext()); | |
113 | + } | |
114 | + else{ | |
115 | + return null; | |
116 | + } | |
117 | + | |
118 | + return entries; | |
119 | + } | |
98 | 120 | } | ... | ... |
app/src/main/res/layout/fragment_addbook.xml