From c7682ca70d712b3fd1d89ef219a0c62eb6fd447d Mon Sep 17 00:00:00 2001 From: Samidh Talsania Date: Sat, 10 Jan 2015 21:35:07 +0530 Subject: [PATCH] Integrated database. --- .../java/pl/surecase/eu/MyDaoGenerator.java | 4 +- app/src/main/java-gen/greendao/DaoMaster.java | 4 +- app/src/main/java-gen/greendao/Match.java | 8 +-- app/src/main/java-gen/greendao/MatchDao.java | 12 ++-- .../bluealeaf/dota2ticker/MainActivity.java | 47 +++++++------- .../adapters/MatchListAdapter.java | 6 +- .../dota2ticker/async/RestClient.java | 8 +-- .../bluealeaf/dota2ticker/async/d2ticker.java | 2 +- .../dota2ticker/bus/BusProvider.java | 44 +++++++++++-- .../dota2ticker/events/GetMatchesEvent.java | 42 ++++++++++-- .../dota2ticker/events/PassIdEvent.java | 6 +- .../events/PassMatchListFromDBEvent.java | 22 +++++++ ...nt.java => PassMatchListFromNetEvent.java} | 4 +- .../events/UpdateMatchesEvent.java | 22 +++++++ .../bluealeaf/dota2ticker/models/Match.java | 6 +- .../dota2ticker/util/MatchConverter.java | 65 +++++++++++++++++++ .../main/java/database/MatchDbOperations.java | 48 ++++++++++++++ .../main/res/layout/activity_main_list.xml | 6 ++ 18 files changed, 290 insertions(+), 66 deletions(-) create mode 100644 app/src/main/java/com/bluealeaf/dota2ticker/events/PassMatchListFromDBEvent.java rename app/src/main/java/com/bluealeaf/dota2ticker/events/{PassMatchListEvent.java => PassMatchListFromNetEvent.java} (84%) create mode 100644 app/src/main/java/com/bluealeaf/dota2ticker/events/UpdateMatchesEvent.java create mode 100644 app/src/main/java/com/bluealeaf/dota2ticker/util/MatchConverter.java create mode 100644 app/src/main/java/database/MatchDbOperations.java diff --git a/MyDaoGenerator/src/main/java/pl/surecase/eu/MyDaoGenerator.java b/MyDaoGenerator/src/main/java/pl/surecase/eu/MyDaoGenerator.java index 20df834..668df69 100644 --- a/MyDaoGenerator/src/main/java/pl/surecase/eu/MyDaoGenerator.java +++ b/MyDaoGenerator/src/main/java/pl/surecase/eu/MyDaoGenerator.java @@ -7,14 +7,14 @@ public class MyDaoGenerator { public static void main(String args[]) throws Exception { - Schema schema = new Schema(3, "greendao"); + Schema schema = new Schema(1, "greendao"); Entity box = schema.addEntity("Match"); box.addIdProperty(); box.addStringProperty("t1"); box.addStringProperty("t2"); box.addStringProperty("t1c"); box.addStringProperty("t2c"); - box.addStringProperty("ETA"); + box.addLongProperty("ETA"); new DaoGenerator().generateAll(schema, args[0]); } } diff --git a/app/src/main/java-gen/greendao/DaoMaster.java b/app/src/main/java-gen/greendao/DaoMaster.java index 15395b4..2da49c4 100644 --- a/app/src/main/java-gen/greendao/DaoMaster.java +++ b/app/src/main/java-gen/greendao/DaoMaster.java @@ -11,10 +11,10 @@ // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. /** - * Master of DAO (schema version 3): knows all DAOs. + * Master of DAO (schema version 1): knows all DAOs. */ public class DaoMaster extends AbstractDaoMaster { - public static final int SCHEMA_VERSION = 3; + public static final int SCHEMA_VERSION = 1; /** Creates underlying database table using DAOs. */ public static void createAllTables(SQLiteDatabase db, boolean ifNotExists) { diff --git a/app/src/main/java-gen/greendao/Match.java b/app/src/main/java-gen/greendao/Match.java index ae3b04d..ed88451 100644 --- a/app/src/main/java-gen/greendao/Match.java +++ b/app/src/main/java-gen/greendao/Match.java @@ -11,7 +11,7 @@ public class Match { private String t2; private String t1c; private String t2c; - private String ETA; + private Long ETA; public Match() { } @@ -20,7 +20,7 @@ public Match(Long id) { this.id = id; } - public Match(Long id, String t1, String t2, String t1c, String t2c, String ETA) { + public Match(Long id, String t1, String t2, String t1c, String t2c, Long ETA) { this.id = id; this.t1 = t1; this.t2 = t2; @@ -69,11 +69,11 @@ public void setT2c(String t2c) { this.t2c = t2c; } - public String getETA() { + public Long getETA() { return ETA; } - public void setETA(String ETA) { + public void setETA(Long ETA) { this.ETA = ETA; } diff --git a/app/src/main/java-gen/greendao/MatchDao.java b/app/src/main/java-gen/greendao/MatchDao.java index af2f4cb..883e5f1 100644 --- a/app/src/main/java-gen/greendao/MatchDao.java +++ b/app/src/main/java-gen/greendao/MatchDao.java @@ -26,7 +26,7 @@ public static class Properties { public final static Property T2 = new Property(2, String.class, "t2", false, "T2"); public final static Property T1c = new Property(3, String.class, "t1c", false, "T1C"); public final static Property T2c = new Property(4, String.class, "t2c", false, "T2C"); - public final static Property ETA = new Property(5, String.class, "ETA", false, "ETA"); + public final static Property ETA = new Property(5, Long.class, "ETA", false, "ETA"); }; @@ -47,7 +47,7 @@ public static void createTable(SQLiteDatabase db, boolean ifNotExists) { "'T2' TEXT," + // 2: t2 "'T1C' TEXT," + // 3: t1c "'T2C' TEXT," + // 4: t2c - "'ETA' TEXT);"); // 5: ETA + "'ETA' INTEGER);"); // 5: ETA } /** Drops the underlying database table. */ @@ -86,9 +86,9 @@ protected void bindValues(SQLiteStatement stmt, Match entity) { stmt.bindString(5, t2c); } - String ETA = entity.getETA(); + Long ETA = entity.getETA(); if (ETA != null) { - stmt.bindString(6, ETA); + stmt.bindLong(6, ETA); } } @@ -107,7 +107,7 @@ public Match readEntity(Cursor cursor, int offset) { cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // t2 cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // t1c cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // t2c - cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5) // ETA + cursor.isNull(offset + 5) ? null : cursor.getLong(offset + 5) // ETA ); return entity; } @@ -120,7 +120,7 @@ public void readEntity(Cursor cursor, Match entity, int offset) { entity.setT2(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2)); entity.setT1c(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3)); entity.setT2c(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4)); - entity.setETA(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5)); + entity.setETA(cursor.isNull(offset + 5) ? null : cursor.getLong(offset + 5)); } /** @inheritdoc */ diff --git a/app/src/main/java/com/bluealeaf/dota2ticker/MainActivity.java b/app/src/main/java/com/bluealeaf/dota2ticker/MainActivity.java index 5d01b14..db39210 100644 --- a/app/src/main/java/com/bluealeaf/dota2ticker/MainActivity.java +++ b/app/src/main/java/com/bluealeaf/dota2ticker/MainActivity.java @@ -3,17 +3,14 @@ import android.os.Bundle; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.ActionBarActivity; -import android.util.Log; import android.widget.ListView; -import android.widget.Toast; import com.bluealeaf.dota2ticker.adapters.MatchListAdapter; import com.bluealeaf.dota2ticker.bus.BusProvider; -import com.bluealeaf.dota2ticker.constants.Errors; import com.bluealeaf.dota2ticker.constants.OkHttpClientConst; import com.bluealeaf.dota2ticker.events.GetIdFromDbEvent; -import com.bluealeaf.dota2ticker.events.PassMatchListEvent; -import com.bluealeaf.dota2ticker.models.Match; +import com.bluealeaf.dota2ticker.events.PassMatchListFromDBEvent; +import com.bluealeaf.dota2ticker.events.UpdateMatchesEvent; import com.squareup.otto.Subscribe; import java.util.ArrayList; @@ -24,13 +21,13 @@ public class MainActivity extends ActionBarActivity { //Holds the id of the last match in database - private int id; - private List matches; + + private ListView listView; private MatchListAdapter adapter; private SwipeRefreshLayout swipeRefreshLayout; - + List matches ; private static final String tag = MainActivity.class.getName(); @@ -38,7 +35,7 @@ public class MainActivity extends ActionBarActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - matches = new ArrayList(); + matches = new ArrayList(); listView = (ListView) findViewById(R.id.match_list_view); swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe); @@ -49,11 +46,9 @@ protected void onCreate(Bundle savedInstanceState) { @Override public void onRefresh() { - BusProvider.getInstance().post(new GetIdFromDbEvent(OkHttpClientConst.FORCE_NETWORK)); + BusProvider.getBusInstance().post(new GetIdFromDbEvent(OkHttpClientConst.FORCE_NETWORK)); } }); - - Log.d(tag, "Create"); } @@ -61,10 +56,9 @@ public void onRefresh() { protected void onResume() { super.onResume(); //Register subscribed event - BusProvider.getInstance().register(this); + BusProvider.getBusInstance().register(this); //Post an event to get List of Matches - BusProvider.getInstance().post(new GetIdFromDbEvent(OkHttpClientConst.FORCE_CACHE)); - Log.d(tag, "Resume"); + BusProvider.getBusInstance().post(new GetIdFromDbEvent(OkHttpClientConst.FORCE_CACHE)); } @@ -73,23 +67,28 @@ protected void onResume() { protected void onPause() { super.onPause(); //Unregister subscribed event - BusProvider.getInstance().unregister(this); + BusProvider.getBusInstance().unregister(this); + } + + @Subscribe + public void OnListReceivedFromDb(PassMatchListFromDBEvent event){ + matches = event.getMatchList(); + adapter = new MatchListAdapter(this,matches); + listView.setAdapter(adapter); } @Subscribe - public void OnListReceived(PassMatchListEvent event){ + public void OnListReceived(UpdateMatchesEvent event){ if(swipeRefreshLayout.isRefreshing()){ swipeRefreshLayout.setRefreshing(false); } - if(event != null) { - matches = event.getMatchList(); - adapter = new MatchListAdapter(this, matches); - listView.setAdapter(adapter); - } - else{ - Toast.makeText(this, Errors.Retrofit_error,Toast.LENGTH_LONG); + if(event != null){ + matches.addAll(event.getMatches()); + adapter.notifyDataSetChanged(); } } + + } diff --git a/app/src/main/java/com/bluealeaf/dota2ticker/adapters/MatchListAdapter.java b/app/src/main/java/com/bluealeaf/dota2ticker/adapters/MatchListAdapter.java index 0bd1803..06de668 100644 --- a/app/src/main/java/com/bluealeaf/dota2ticker/adapters/MatchListAdapter.java +++ b/app/src/main/java/com/bluealeaf/dota2ticker/adapters/MatchListAdapter.java @@ -9,10 +9,11 @@ import android.widget.TextView; import com.bluealeaf.dota2ticker.R; -import com.bluealeaf.dota2ticker.models.Match; import java.util.List; +import greendao.Match; + /** * Created by samidh on 5/1/15. */ @@ -26,6 +27,7 @@ static class ViewHolder{ public TextView teamOne; public TextView vs; public TextView teamTwo; + public TextView ETA; public Switch alarmSwitch; } @@ -61,6 +63,7 @@ public View getView(int position, View convertView, ViewGroup parent) { viewHolder.teamOne = (TextView) view.findViewById(R.id.teamOne); viewHolder.teamTwo = (TextView) view.findViewById(R.id.teamTwo); viewHolder.vs = (TextView) view.findViewById(R.id.vs); + viewHolder.ETA = (TextView) view.findViewById(R.id.ETA); viewHolder.alarmSwitch = (Switch) view.findViewById(R.id.alarmSwitch); view.setTag(viewHolder); } @@ -71,6 +74,7 @@ public View getView(int position, View convertView, ViewGroup parent) { viewHolder.teamOne.setText(match_data.getT1()); viewHolder.teamTwo.setText(match_data.getT2()); viewHolder.vs.setText("vs"); + viewHolder.ETA.setText(String.valueOf(match_data.getETA())); return view; } diff --git a/app/src/main/java/com/bluealeaf/dota2ticker/async/RestClient.java b/app/src/main/java/com/bluealeaf/dota2ticker/async/RestClient.java index 15ac340..905d551 100644 --- a/app/src/main/java/com/bluealeaf/dota2ticker/async/RestClient.java +++ b/app/src/main/java/com/bluealeaf/dota2ticker/async/RestClient.java @@ -6,7 +6,7 @@ import com.bluealeaf.dota2ticker.bus.BusProvider; import com.bluealeaf.dota2ticker.constants.Endpoints; -import com.bluealeaf.dota2ticker.events.PassMatchListEvent; +import com.bluealeaf.dota2ticker.events.PassMatchListFromNetEvent; import com.bluealeaf.dota2ticker.models.Api; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -22,7 +22,7 @@ public class RestClient { private static final String tag = RestClient.class.getName(); - public static void getMatchesList(int id){ + public static void getMatchesList(long id){ Gson gson = new GsonBuilder() .create(); @@ -41,13 +41,13 @@ public static void getMatchesList(int id){ public void success(Api api, Response response) { //pass an event to main activity on success Api temp = api; - BusProvider.getInstance().post(new PassMatchListEvent(temp)); + BusProvider.getBusInstance().post(new PassMatchListFromNetEvent(temp)); } @Override public void failure(RetrofitError error) { //pass an event to main activity on failure - BusProvider.getInstance().post(new PassMatchListEvent(null)); + BusProvider.getBusInstance().post(new PassMatchListFromNetEvent(null)); } }); diff --git a/app/src/main/java/com/bluealeaf/dota2ticker/async/d2ticker.java b/app/src/main/java/com/bluealeaf/dota2ticker/async/d2ticker.java index 7e08210..d84cc99 100644 --- a/app/src/main/java/com/bluealeaf/dota2ticker/async/d2ticker.java +++ b/app/src/main/java/com/bluealeaf/dota2ticker/async/d2ticker.java @@ -11,5 +11,5 @@ */ public interface d2ticker { @GET("/match/{id}") - void getMatchesList(@Path("id") int id, Callback api); + void getMatchesList(@Path("id") long id, Callback api); } diff --git a/app/src/main/java/com/bluealeaf/dota2ticker/bus/BusProvider.java b/app/src/main/java/com/bluealeaf/dota2ticker/bus/BusProvider.java index 49dc962..53db888 100644 --- a/app/src/main/java/com/bluealeaf/dota2ticker/bus/BusProvider.java +++ b/app/src/main/java/com/bluealeaf/dota2ticker/bus/BusProvider.java @@ -1,6 +1,7 @@ package com.bluealeaf.dota2ticker.bus; import android.app.Application; +import android.database.sqlite.SQLiteDatabase; import android.util.Log; import com.bluealeaf.dota2ticker.constants.Errors; @@ -13,46 +14,75 @@ import java.io.IOException; import java.util.concurrent.TimeUnit; +import greendao.DaoMaster; +import greendao.DaoSession; + /** * Created by samidh on 4/1/15. */ public class BusProvider extends Application { + + private static final String tag = BusProvider.class.getName(); + + //initialize event bus private static final Bus BUS = new Bus(); + + //initialize http client private static final OkHttpClient okHttpClient = new OkHttpClient(); - private static final String tag = BusProvider.class.getName(); - public static Bus getInstance() { + //initialize database session object + private static DaoSession daoSession; + + + //Return a singleton instance of Bus + public static Bus getBusInstance() { return BUS; } + //Return a singleton instance of Http client public static OkHttpClient getClientInstance(){ return okHttpClient; } + //Return a singleton instance of DAO Session + public static DaoSession getDaoSessionInstance(){ return daoSession ;} + private GetMatchesEvent mGetMatchesEvent; @Override public void onCreate() { super.onCreate(); + + setupDatabase(); + + setupCache(); + + //Register GetMatchesEvent class to enabe it to receive events. + //All subscriber methods are written in this class mGetMatchesEvent = new GetMatchesEvent(); - Log.d("BusProvider-onCreate", "here"); - BusProvider.getInstance().register(mGetMatchesEvent); + BusProvider.getBusInstance().register(mGetMatchesEvent); + } + + private void setupCache(){ //create cache directory BusProvider.getClientInstance() .setConnectTimeout(OkHttpClientConst.connectionTimeout, TimeUnit.MILLISECONDS); try { - Cache cache = new Cache(getApplicationContext().getCacheDir().getAbsoluteFile(), OkHttpClientConst.SIZE); - BusProvider.getClientInstance().setCache(cache); } catch(IOException ex){ Log.d(tag, Errors.Cache_IO_Error); } + } - + private void setupDatabase(){ + DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "d2ticker-db", null); + SQLiteDatabase db = helper.getWritableDatabase(); + DaoMaster daoMaster = new DaoMaster(db); + daoSession = daoMaster.newSession(); } } diff --git a/app/src/main/java/com/bluealeaf/dota2ticker/events/GetMatchesEvent.java b/app/src/main/java/com/bluealeaf/dota2ticker/events/GetMatchesEvent.java index afe3619..bf71b71 100644 --- a/app/src/main/java/com/bluealeaf/dota2ticker/events/GetMatchesEvent.java +++ b/app/src/main/java/com/bluealeaf/dota2ticker/events/GetMatchesEvent.java @@ -1,35 +1,63 @@ package com.bluealeaf.dota2ticker.events; -import android.util.Log; - import com.bluealeaf.dota2ticker.async.RestClient; import com.bluealeaf.dota2ticker.bus.BusProvider; +import com.bluealeaf.dota2ticker.util.MatchConverter; import com.squareup.otto.Subscribe; +import java.util.List; + +import database.MatchDbOperations; +import greendao.Match; + /** * Created by samidh on 4/1/15. */ public class GetMatchesEvent { - private int id; + private long id; private static final String tag = GetMatchesEvent.class.getName(); public GetMatchesEvent() { } @Subscribe public void OnRequestForId(GetIdFromDbEvent event){ + //do db stuff + List matchList = MatchDbOperations.getAllMatches(); + BusProvider.getBusInstance().post(new PassMatchListFromDBEvent(matchList)); + //Get the last ID - id = 1 ; + matchList = MatchDbOperations.getMaxId(); + if(matchList.size() == 0){ + id = 0; + } + else{ + id = matchList.get(0).getId(); + } - Log.d(tag, "Here"); - BusProvider.getInstance().post(new PassIdEvent(id)); + + //Make network call to get latest matches + BusProvider.getBusInstance().post(new PassIdEvent(id)); } @Subscribe public void onReceiveId(PassIdEvent event){ - int id = event.getId(); + long id = event.getId(); RestClient.getMatchesList(id); } + + @Subscribe + public void onReceiveNewMatches(PassMatchListFromNetEvent event){ + //New matches are fetched. + //Add the into database. + if(event != null) { + List dbMatchList = MatchConverter.netToDB(event.getMatchList()); + MatchDbOperations.insertAll(dbMatchList); + + //Send another event to main ui to update it with the new matches + BusProvider.getBusInstance().post(new UpdateMatchesEvent(dbMatchList)); + } + } } diff --git a/app/src/main/java/com/bluealeaf/dota2ticker/events/PassIdEvent.java b/app/src/main/java/com/bluealeaf/dota2ticker/events/PassIdEvent.java index 409829a..af02171 100644 --- a/app/src/main/java/com/bluealeaf/dota2ticker/events/PassIdEvent.java +++ b/app/src/main/java/com/bluealeaf/dota2ticker/events/PassIdEvent.java @@ -4,8 +4,8 @@ * Created by samidh on 5/1/15. */ public class PassIdEvent { - private int id; - public PassIdEvent(int id){ + private long id; + public PassIdEvent(long id){ this.id = id; } public PassIdEvent(){ @@ -13,7 +13,7 @@ public PassIdEvent(){ } - public int getId(){ + public long getId(){ return id; } diff --git a/app/src/main/java/com/bluealeaf/dota2ticker/events/PassMatchListFromDBEvent.java b/app/src/main/java/com/bluealeaf/dota2ticker/events/PassMatchListFromDBEvent.java new file mode 100644 index 0000000..d579694 --- /dev/null +++ b/app/src/main/java/com/bluealeaf/dota2ticker/events/PassMatchListFromDBEvent.java @@ -0,0 +1,22 @@ +package com.bluealeaf.dota2ticker.events; + +import java.util.ArrayList; +import java.util.List; + +import greendao.Match; + +/** + * Created by samidh on 10/1/15. + */ +public class PassMatchListFromDBEvent { + + List matchList = new ArrayList(); + + public PassMatchListFromDBEvent(List matchList){ + this.matchList = matchList; + } + + public List getMatchList(){ + return matchList; + } +} diff --git a/app/src/main/java/com/bluealeaf/dota2ticker/events/PassMatchListEvent.java b/app/src/main/java/com/bluealeaf/dota2ticker/events/PassMatchListFromNetEvent.java similarity index 84% rename from app/src/main/java/com/bluealeaf/dota2ticker/events/PassMatchListEvent.java rename to app/src/main/java/com/bluealeaf/dota2ticker/events/PassMatchListFromNetEvent.java index 8369d34..130c0af 100644 --- a/app/src/main/java/com/bluealeaf/dota2ticker/events/PassMatchListEvent.java +++ b/app/src/main/java/com/bluealeaf/dota2ticker/events/PassMatchListFromNetEvent.java @@ -9,9 +9,9 @@ import java.util.List; -public class PassMatchListEvent { +public class PassMatchListFromNetEvent { private Api api; - public PassMatchListEvent(Api api){ + public PassMatchListFromNetEvent(Api api){ this.api = api; } diff --git a/app/src/main/java/com/bluealeaf/dota2ticker/events/UpdateMatchesEvent.java b/app/src/main/java/com/bluealeaf/dota2ticker/events/UpdateMatchesEvent.java new file mode 100644 index 0000000..99a5470 --- /dev/null +++ b/app/src/main/java/com/bluealeaf/dota2ticker/events/UpdateMatchesEvent.java @@ -0,0 +1,22 @@ +package com.bluealeaf.dota2ticker.events; + +import java.util.ArrayList; +import java.util.List; + +import greendao.Match; + +/** + * Created by samidh on 10/1/15. + */ +public class UpdateMatchesEvent { + + private List matches ; + public UpdateMatchesEvent(List matches){ + matches = new ArrayList(); + this.matches = matches; + } + + public List getMatches() { + return matches; + } +} diff --git a/app/src/main/java/com/bluealeaf/dota2ticker/models/Match.java b/app/src/main/java/com/bluealeaf/dota2ticker/models/Match.java index 207e167..2908c2f 100644 --- a/app/src/main/java/com/bluealeaf/dota2ticker/models/Match.java +++ b/app/src/main/java/com/bluealeaf/dota2ticker/models/Match.java @@ -21,7 +21,7 @@ public class Match { @Expose private String ETA; @Expose - private int id; + private long id; /** * @@ -118,7 +118,7 @@ public void setETA(String ETA) { * @return * The id */ - public int getId() { + public long getId() { return id; } @@ -127,7 +127,7 @@ public int getId() { * @param id * The id */ - public void setId(int id) { + public void setId(long id) { this.id = id; } diff --git a/app/src/main/java/com/bluealeaf/dota2ticker/util/MatchConverter.java b/app/src/main/java/com/bluealeaf/dota2ticker/util/MatchConverter.java new file mode 100644 index 0000000..8e4e71b --- /dev/null +++ b/app/src/main/java/com/bluealeaf/dota2ticker/util/MatchConverter.java @@ -0,0 +1,65 @@ +package com.bluealeaf.dota2ticker.util; + +import com.bluealeaf.dota2ticker.models.Match; + +import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; +import org.joda.time.format.DateTimeFormat; +import org.joda.time.format.DateTimeFormatter; + +import java.util.ArrayList; +import java.util.List; + + + +/** + * Created by samidh on 10/1/15. + */ + +/* + +Utility class that converts models.Match data object to greendao,Match data object + + */ + +public class MatchConverter { + + public static List netToDB(List matches){ + List matchList = new ArrayList(); + greendao.Match newMatch; + + for(Match match : matches){ + newMatch = new greendao.Match(); + newMatch.setId(match.getId()); + newMatch.setT1(match.getT1()); + newMatch.setT1c(match.getT1c()); + newMatch.setT2(match.getT2()); + newMatch.setT2c(match.getT2c()); + + DateTimeFormatter df = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); + DateTime dt = df.withZone(DateTimeZone.UTC).parseDateTime(match.getETA()); + long seconds = dt.getMillis(); + + newMatch.setETA(seconds); + matchList.add(newMatch); + } + return matchList; + } + + public static List greenDaoToDb(List matches){ + List matchList = new ArrayList(); + Match newMatch; + for(greendao.Match match : matches){ + newMatch = new Match(); + newMatch.setId(match.getId()); + newMatch.setT1(match.getT1()); + newMatch.setT1c(match.getT1c()); + newMatch.setT2(match.getT2()); + newMatch.setT2c(match.getT2c()); + + } + + return matchList; + } + +} diff --git a/app/src/main/java/database/MatchDbOperations.java b/app/src/main/java/database/MatchDbOperations.java new file mode 100644 index 0000000..311005d --- /dev/null +++ b/app/src/main/java/database/MatchDbOperations.java @@ -0,0 +1,48 @@ +package database; + +import com.bluealeaf.dota2ticker.bus.BusProvider; + +import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; + +import java.util.List; + +import de.greenrobot.dao.query.Query; +import greendao.Match; +import greendao.MatchDao; +/** + * Created by samidh on 10/1/15. + */ +public class MatchDbOperations { + + public static void insertOrUpdate(Match match){ + getMatchDao().insertOrReplace(match); + } + + public static void delete(Match match){ + getMatchDao().delete(match); + } + + public static List getMaxId(){ + Query query = getMatchDao().queryBuilder() + .orderDesc(MatchDao.Properties.Id) + .limit(1) + .build(); + return query.list(); + } + + public static void insertAll(List matchList){ + getMatchDao().insertInTx(matchList); + } + + public static List getAllMatches(){ + DateTime dt = new DateTime(DateTimeZone.UTC); + long millis = dt.getMillis(); + List match = getMatchDao().queryBuilder().where(MatchDao.Properties.ETA.gt(millis)).build().list(); + return match; + } + + private static MatchDao getMatchDao(){ + return BusProvider.getDaoSessionInstance().getMatchDao(); + } +} diff --git a/app/src/main/res/layout/activity_main_list.xml b/app/src/main/res/layout/activity_main_list.xml index 9311cca..79b95a2 100644 --- a/app/src/main/res/layout/activity_main_list.xml +++ b/app/src/main/res/layout/activity_main_list.xml @@ -33,6 +33,12 @@ android:layout_centerVertical="true" android:id="@+id/teamTwo"/> + +