|
| 1 | +package ca.carleton.gcrc.n2android_mobile1; |
| 2 | + |
| 3 | +import android.annotation.SuppressLint; |
| 4 | +import android.content.Intent; |
| 5 | +import android.support.v7.app.ActionBar; |
| 6 | +import android.support.v7.app.AppCompatActivity; |
| 7 | +import android.os.Bundle; |
| 8 | +import android.os.Handler; |
| 9 | +import android.view.MotionEvent; |
| 10 | +import android.view.View; |
| 11 | +import android.widget.Toast; |
| 12 | + |
| 13 | +import com.couchbase.lite.Database; |
| 14 | +import com.couchbase.lite.DatabaseOptions; |
| 15 | +import com.couchbase.lite.Document; |
| 16 | +import com.couchbase.lite.Emitter; |
| 17 | +import com.couchbase.lite.LiveQuery; |
| 18 | +import com.couchbase.lite.Manager; |
| 19 | +import com.couchbase.lite.Mapper; |
| 20 | +import com.couchbase.lite.SavedRevision; |
| 21 | +import com.couchbase.lite.android.AndroidContext; |
| 22 | +import com.couchbase.lite.util.Log; |
| 23 | + |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +/** |
| 28 | + * An example full-screen activity that shows and hides the system UI (i.e. |
| 29 | + * status bar and navigation/system bar) with user interaction. |
| 30 | + */ |
| 31 | +public class MainActivity extends AppCompatActivity { |
| 32 | + |
| 33 | + public static String TAG = "NunaliitMobile"; |
| 34 | + |
| 35 | + // constants |
| 36 | + public static final String DATABASE_NAME = "connections"; |
| 37 | + public static final String designDocName = "local"; |
| 38 | + |
| 39 | + // couchdb internals |
| 40 | + protected static Manager manager; |
| 41 | + private Database database; |
| 42 | + private LiveQuery liveQuery; |
| 43 | + |
| 44 | + @Override |
| 45 | + protected void onCreate(Bundle savedInstanceState) { |
| 46 | + super.onCreate(savedInstanceState); |
| 47 | + |
| 48 | + setContentView(R.layout.activity_main); |
| 49 | + |
| 50 | + try { |
| 51 | + startCouchDb(); |
| 52 | + } catch(Exception e) { |
| 53 | + Toast |
| 54 | + .makeText(getApplicationContext(), "Error Initializing CouchDB, see logs for details", Toast.LENGTH_LONG) |
| 55 | + .show(); |
| 56 | + Log.e(TAG, "Error initializing CouchDB", e); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + protected void onPostCreate(Bundle savedInstanceState) { |
| 62 | + super.onPostCreate(savedInstanceState); |
| 63 | + } |
| 64 | + |
| 65 | + protected void onDestroy() { |
| 66 | + if(manager != null) { |
| 67 | + manager.close(); |
| 68 | + manager = null; |
| 69 | + } |
| 70 | + super.onDestroy(); |
| 71 | + } |
| 72 | + |
| 73 | + protected void startCouchDb() throws Exception { |
| 74 | + |
| 75 | + Manager.enableLogging(TAG, Log.VERBOSE); |
| 76 | + Manager.enableLogging(Log.TAG, Log.VERBOSE); |
| 77 | + Manager.enableLogging(Log.TAG_SYNC_ASYNC_TASK, Log.VERBOSE); |
| 78 | + Manager.enableLogging(Log.TAG_SYNC, Log.VERBOSE); |
| 79 | + Manager.enableLogging(Log.TAG_QUERY, Log.VERBOSE); |
| 80 | + Manager.enableLogging(Log.TAG_VIEW, Log.VERBOSE); |
| 81 | + Manager.enableLogging(Log.TAG_DATABASE, Log.VERBOSE); |
| 82 | + |
| 83 | + manager = new Manager(new AndroidContext(getApplicationContext()), Manager.DEFAULT_OPTIONS); |
| 84 | + |
| 85 | + // install a view definition needed by the application |
| 86 | + DatabaseOptions options = new DatabaseOptions(); |
| 87 | + options.setCreate(true); |
| 88 | + database = manager.openDatabase(DATABASE_NAME, options); |
| 89 | + |
| 90 | + Document doc = database.getDocument("testDoc"); |
| 91 | + SavedRevision currentRevision = doc.getCurrentRevision(); |
| 92 | + if( null == currentRevision ){ |
| 93 | + Log.i(TAG, "testDoc does not exist"); |
| 94 | + Map<String,Object> props = new HashMap<String,Object>(); |
| 95 | + props.put("nunaliit_test","allo"); |
| 96 | + doc.putProperties(props); |
| 97 | + } else { |
| 98 | + Log.i(TAG, "testDoc revision: "+currentRevision.getProperties().get("_rev")); |
| 99 | + } |
| 100 | +// com.couchbase.lite.View viewItemsByDate = database.getView(String.format("%s/%s", designDocName, byDateViewName)); |
| 101 | +// viewItemsByDate.setMap(new Mapper() { |
| 102 | +// @Override |
| 103 | +// public void map(Map<String, Object> document, Emitter emitter) { |
| 104 | +// Object createdAt = document.get("created_at"); |
| 105 | +// if (createdAt != null) { |
| 106 | +// emitter.emit(createdAt.toString(), null); |
| 107 | +// } |
| 108 | +// } |
| 109 | +// }, "1.0"); |
| 110 | +// |
| 111 | +// initItemListAdapter(); |
| 112 | +// |
| 113 | +// startLiveQuery(viewItemsByDate); |
| 114 | +// |
| 115 | +// startSync(); |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + public void startActivity2(View view){ |
| 120 | + Intent intent = new Intent(this, SecondActivity.class); |
| 121 | + startActivity(intent); |
| 122 | + } |
| 123 | +} |
0 commit comments