/ Apps

Test Android application for Parse test server

Test Android application for Parse test server

parse-android-test-app

Test Android application for Parse test server.

1. Information

1.1. Features

Fully working communication with Parse test server:

  • managing simple entity (Note);
  • sync data with server;
  • restore data from server;
  • sign up from application with email address or Facebook account;
  • sign in to server with email or Facebook.

1.2. Not handling exceptions

  • Internet connection unavailable status;
  • Parse Server connection unavailable status.

2. Parse initialization

2.1. Application class

private fun initializeParse() {
    val serverAddress = "SERVER_ADDRESS"
    val applicationId = "APP_ID"
    val clientKey = "CLIENT_KEY"
    
    ParseApi.initialize(this, serverAddress, applicationId, clientKey)
    ParseAuth.initialize(this)
}

2.2. ParseApi class

fun initialize(context: Context, serverAddress: String, applicationId: String,
               clientKey: String) {
    val parseConfig = Parse.Configuration.Builder(context)
        .server(serverAddress)
        .applicationId(applicationId)
        .clientKey(clientKey)
        .build()
    Parse.initialize(parseConfig)
}

2.3. Requiring parameters

Name Description
SERVER_ADDRESS Address of Parse server in your network
APP_ID Parse server app id
CLIENT_KEY Parse server client key

2.4. Parse API

Parse API provided methods for managing entities in app.

2.4.1. Save all notes

fun saveAllNotes(notes: List<Note>, errorOnSave: (e: Exception) -> Unit) {
    ...
}
Argument Type Description
notes List<Note> Notes to save
errorOnSave Function Error callback

2.4.2. Restore all notes

fun restoreAllNotes(notes: List<Note>, afterRestore: (e: Exception?) -> Unit) {
    ...
}
Argument Type Description
notes List<Note> Notes in local database
afterRestore Function Restore callback

2.5. Parse Auth API

Parse Auth API provides methods for authorization with email or Facebook account.

2.5.1. Authorization check

fun isAuthorized() = ParseUser.getCurrentUser() != null

2.5.2. Register with email

fun register(username: String, email: String, password: String,
                 afterLogin: (e: Exception?) -> Unit) {
     ...
}
Argument Type Description
username String Name
email String Email
password String Password
afterLogin Function Login callback

2.5.3. Log in with email

fun logInWithEmail(email: String, password: String, afterLogin: (e: Exception?) -> Unit) {
    ...                       
}
Argument Type Description
email String Email
password String Password
afterLogin Function Login callback

2.5.4. Log in with Facebook

2.5.4.1. Log in method
fun logInWithFacebook(fragment: Fragment, afterFacebookLogin: (success: Boolean) -> Unit) {
    ...
}
Argument Type Description
fragment Fragment Fragment (can be replaced to Activity) for user data request
afterFacebookLogin Function Login callback
2.5.4.1. Handling onActivityResult
fun handleOnActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    ...
}
Argument Type Description
requestCode Int requestCode from request
resultCode Int resultCode from request
data Intent data from request

2.5.5. Logout

fun logout(afterLogout: (e: Exception) -> Unit) {
    ...
}
Argument Type Description
afterLogout Function Logout callback

3. Database

ORMLite. More on official site.

4. Data structure

This section contains full list of entities using in app.

4.1. Entity

Base class for all database entities.

abstract class Entity(

    @DatabaseField(generatedId = true)
    var id: Long = -1

)

4.2. Note

class Note(

    @DatabaseField
    var title: String? = null,

    @DatabaseField
    var subtitle: String? = null,

    @DatabaseField
    var parseObjectId: String? = null,

    var positionInList: Int = 0

) : Entity() {

    ...

}

4.2.1. Save note

From Note class method:

fun save() {
    DatabaseFactory.get().saveNote(this)
}

Example:

    val newNote = Note(title, subtitle)
    newNote.save()
    

4.2.2. Delete note

From Note class method:

fun delete() {
    DatabaseFactory.get().deleteNote(this)
}

Example:

    val note = notes[notes.size - 1]
    note.delete()

4.2.3. Get all notes from database

From Note class method:

    fun getAllNotes() = DatabaseFactory.get().allNotes

Example:

    val notes = Note.getAllNotes()

4.2.4. Delete all notes from database

From Note class method:

fun deleteAllNotes() {
    DatabaseFactory.get().deleteAllNotes()
}

Example:

    Note.deleteAllNotes()

4.2.5. Get ParseObject for note

From Note class method:

fun getParseObject(globalAccess: Boolean = true, user: ParseUser? = null): ParseObject {
    ...
}

Example:

val parseNote = newNote.getParseObject(false, authUser)

4.2.6. Initialize note from ParseObject

From Note class method:

fun restoreFromParseObject(parseObject: ParseObject): ParseObject {
    ...
}

Example:

for (obj in parseObjects) {
    val note = Note()
    note.restoreFromParseObject(obj)
    ...
}

5. Facebook install

5.1. Resources

5.2. Usage

In preferences.xml replace this values to own analogues:

<string name="facebook_app_id">[APP_ID]</string>
<string name="fb_login_protocol_scheme">fb[APP_ID]</string>

6. Permissions

<uses-permission android:name="android.permission.INTERNET"/>

GitHub