Add ParseLog Utility
This commit is contained in:
		| @@ -30,7 +30,13 @@ dependencies { | |||||||
|     testImplementation 'junit:junit:4.12' |     testImplementation 'junit:junit:4.12' | ||||||
|     androidTestImplementation 'com.android.support.test:runner:1.0.1' |     androidTestImplementation 'com.android.support.test:runner:1.0.1' | ||||||
|     androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' |     androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' | ||||||
|  |  | ||||||
|  |     // Parse | ||||||
|     implementation project(':Parse') |     implementation project(':Parse') | ||||||
|     implementation project(':ParseLiveQuery') |     implementation project(':ParseLiveQuery') | ||||||
|  |  | ||||||
|  |     // Logger | ||||||
|  |     implementation 'com.github.danylovolokh:android-logger:1.0.2' | ||||||
|  |  | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -2,14 +2,53 @@ package net.adphi.apps.parseapplication | |||||||
|  |  | ||||||
| import android.os.Bundle | import android.os.Bundle | ||||||
| import android.support.v7.app.AppCompatActivity | import android.support.v7.app.AppCompatActivity | ||||||
|  | import com.parse.ParseQuery | ||||||
|  | import com.parse.ParseUser | ||||||
|  | import net.adphi.apps.parseapplication.Models.CustomObject | ||||||
|  | import net.adphi.apps.parseapplication.Utils.LOG | ||||||
| import net.adphi.apps.parseapplication.Utils.getTAG | import net.adphi.apps.parseapplication.Utils.getTAG | ||||||
|  |  | ||||||
| class MainActivity : AppCompatActivity() { | class MainActivity : AppCompatActivity() { | ||||||
|  |  | ||||||
|     private val TAG: String = getTAG(this) |     private val TAG: String = getTAG(this) | ||||||
|  |  | ||||||
|  |     private var user: ParseUser? = ParseUser.getCurrentUser() | ||||||
|  |  | ||||||
|     override fun onCreate(savedInstanceState: Bundle?) { |     override fun onCreate(savedInstanceState: Bundle?) { | ||||||
|         super.onCreate(savedInstanceState) |         super.onCreate(savedInstanceState) | ||||||
|         setContentView(R.layout.activity_main) |         setContentView(R.layout.activity_main) | ||||||
|  |  | ||||||
|  |         if(user == null) { | ||||||
|  |             ParseUser.logInInBackground("Adphi", "azertest", { user, e -> | ||||||
|  |                 if (e != null) { | ||||||
|  |                     LOG.err(TAG, "Authentication Error ${e.message!!}") | ||||||
|  |                     return@logInInBackground | ||||||
|  |                 } | ||||||
|  |                 this.user = user | ||||||
|  |                 LOG.inf(TAG, "Authentication Succes. User: ${user.username} ") | ||||||
|  |                 test() | ||||||
|  |             }) | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |             LOG.inf(TAG, "Authentication Already Logged. User: ${user!!.username}") | ||||||
|  |             test() | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     fun test() { | ||||||
|  |         LOG.v(TAG, "Sending Test Query.") | ||||||
|  |         val query: ParseQuery<CustomObject> = ParseQuery.getQuery(CustomObject::class.java) | ||||||
|  |         query.include("user") | ||||||
|  |         query.findInBackground { objects, e -> | ||||||
|  |             if(e != null) { | ||||||
|  |                 LOG.d(TAG, "Query Error: ${e.message}") | ||||||
|  |                 return@findInBackground | ||||||
|  |             } | ||||||
|  |             LOG.d(TAG, "Query results: ${objects.size}") | ||||||
|  |             LOG.d(TAG, "Query results: $objects") | ||||||
|  |             objects.forEach { o -> LOG.d(TAG, "CustomObject : $o ${o.user?.username}") } | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -0,0 +1,27 @@ | |||||||
|  | package net.adphi.apps.parseapplication.Models | ||||||
|  |  | ||||||
|  | import com.parse.ParseClassName | ||||||
|  | import com.parse.ParseFile | ||||||
|  | import com.parse.ParseObject | ||||||
|  | import com.parse.ParseUser | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Created by Philippe-Adrien on 25/03/2018. | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | @ParseClassName("CustomObject") | ||||||
|  | class CustomObject : ParseObject(){ | ||||||
|  |     var value: String? | ||||||
|  |         get() = getString("value") | ||||||
|  |         set(value) = put("value", value) | ||||||
|  |     var user: ParseUser? | ||||||
|  |         get() = getParseUser("user") | ||||||
|  |         set(value) = put("user", value) | ||||||
|  |     var file: ParseFile? | ||||||
|  |         get() = getParseFile("file") | ||||||
|  |         set(value) = put("file", value) | ||||||
|  |  | ||||||
|  |     override fun toString(): String { | ||||||
|  |         return "CustomObject [value: $value, user: $user]" | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -1,10 +1,12 @@ | |||||||
| package net.adphi.apps.parseapplication | package net.adphi.apps.parseapplication | ||||||
|  |  | ||||||
| import android.app.Application | import android.app.Application | ||||||
|  | import com.danylovolokh.androidlogger.AndroidLogger | ||||||
| import com.parse.Parse | import com.parse.Parse | ||||||
| import com.parse.ParseLiveQueryClient | import com.parse.ParseLiveQueryClient | ||||||
| import net.adphi.apps.parseapplication.Utils.getTAG | import net.adphi.apps.parseapplication.Utils.getTAG | ||||||
| import net.adphi.apps.parseapplication.Utils.registerParseObject | import net.adphi.apps.parseapplication.Utils.registerParseObject | ||||||
|  | import java.io.IOException | ||||||
| import java.net.URI | import java.net.URI | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -20,9 +22,24 @@ class ParseApplication : Application() { | |||||||
|     override fun onCreate() { |     override fun onCreate() { | ||||||
|         super.onCreate() |         super.onCreate() | ||||||
|  |  | ||||||
|  |         val logsDirectory = AndroidLogger.getDefaultLogFilesDirectory(this) | ||||||
|  |         val logFileMaxSizeBytes = 2 * 1024 * 1024 // 2Mb | ||||||
|  |         try { | ||||||
|  |             AndroidLogger.initialize( | ||||||
|  |                     this, | ||||||
|  |                     logsDirectory, | ||||||
|  |                     "ParseApplicationLog", | ||||||
|  |                     logFileMaxSizeBytes, | ||||||
|  |                     false | ||||||
|  |             ) | ||||||
|  |         } catch (e: IOException) { | ||||||
|  |             // Some error happened - most likely there is no free space on the system | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |  | ||||||
|         registerParseObject() |         registerParseObject() | ||||||
|         val config: Parse.Configuration = Parse.Configuration.Builder(this) |         val config: Parse.Configuration = Parse.Configuration.Builder(this) | ||||||
|                 .maxRetries(0).build() |                 .maxRetries(1).build() | ||||||
|         Parse.initialize(config) |         Parse.initialize(config) | ||||||
|         val liveQueryClient = ParseLiveQueryClient.Factory.getClient(URI("wss://parsetest.back4app.io")) |         val liveQueryClient = ParseLiveQueryClient.Factory.getClient(URI("wss://parsetest.back4app.io")) | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -0,0 +1,42 @@ | |||||||
|  | package net.adphi.apps.parseapplication.Utils | ||||||
|  |  | ||||||
|  | import android.util.Log | ||||||
|  | import com.danylovolokh.androidlogger.AndroidLogger | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Created by Philippe-Adrien on 25/03/2018. | ||||||
|  |  */ | ||||||
|  | object LOG { | ||||||
|  |  | ||||||
|  |     fun v(TAG: String, message: String): Int { | ||||||
|  |         Log.v(TAG, message) | ||||||
|  |         ParseLog.v(TAG, message) | ||||||
|  |         return AndroidLogger.v(TAG, message) | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     fun d(TAG: String, message: String): Int { | ||||||
|  |         Log.d(TAG, message) | ||||||
|  |         ParseLog.d(TAG, message) | ||||||
|  |         return AndroidLogger.d(TAG, message) | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     fun inf(TAG: String, message: String): Int { | ||||||
|  |         Log.i(TAG, message) | ||||||
|  |         ParseLog.i(TAG, message) | ||||||
|  |         return AndroidLogger.i(TAG, message) | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     fun w(TAG: String, message: String): Int { | ||||||
|  |         Log.w(TAG, message) | ||||||
|  |         ParseLog.w(TAG, message) | ||||||
|  |         return AndroidLogger.w(TAG, message) | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     fun err(TAG: String, message: String): Int { | ||||||
|  |         Log.e(TAG, message) | ||||||
|  |         ParseLog.e(TAG, message) | ||||||
|  |         return AndroidLogger.e(TAG, message) | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -0,0 +1,42 @@ | |||||||
|  | package net.adphi.apps.parseapplication.Utils | ||||||
|  |  | ||||||
|  | import com.parse.ParseClassName | ||||||
|  | import com.parse.ParseObject | ||||||
|  | import com.parse.ParseUser | ||||||
|  | import java.util.* | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Created by Philippe-Adrien on 25/03/2018. | ||||||
|  |  */ | ||||||
|  | @ParseClassName("Logs") | ||||||
|  | class ParseLog() : ParseObject(){ | ||||||
|  |     constructor(type: Type, TAG: String, message: String) : this() { | ||||||
|  |         put("user", ParseUser.getCurrentUser()) | ||||||
|  |         put("type", type.toString()) | ||||||
|  |         put("time", Date()) | ||||||
|  |         put("message", "$TAG : $message") | ||||||
|  |         saveEventually() | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     companion object { | ||||||
|  |         fun v(TAG: String, message: String) { | ||||||
|  |             ParseLog(Type.VERBOSE, TAG, message) | ||||||
|  |         } | ||||||
|  |         fun d(TAG: String, message: String) { | ||||||
|  |             ParseLog(Type.DEBUG, TAG, message) | ||||||
|  |         } | ||||||
|  |         fun i(TAG: String, message: String) { | ||||||
|  |             ParseLog(Type.INFO, TAG, message) | ||||||
|  |         } | ||||||
|  |         fun w(TAG: String, message: String) { | ||||||
|  |             ParseLog(Type.WARNING, TAG, message) | ||||||
|  |         } | ||||||
|  |         fun e(TAG: String, message: String) { | ||||||
|  |             ParseLog(Type.ERROR, TAG, message) | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | enum class Type { | ||||||
|  |     VERBOSE, DEBUG, INFO, WARNING, ERROR | ||||||
|  | } | ||||||
| @@ -1,9 +1,13 @@ | |||||||
| package net.adphi.apps.parseapplication.Utils | package net.adphi.apps.parseapplication.Utils | ||||||
|  |  | ||||||
|  | import com.parse.ParseObject | ||||||
|  | import net.adphi.apps.parseapplication.Models.CustomObject | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Created by Philippe-Adrien on 25/03/2018. |  * Created by Philippe-Adrien on 25/03/2018. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
| fun registerParseObject() : Unit { | fun registerParseObject() { | ||||||
|  |     ParseObject.registerSubclass(ParseLog::class.java) | ||||||
|  |     ParseObject.registerSubclass(CustomObject::class.java) | ||||||
| } | } | ||||||
		Reference in New Issue
	
	Block a user