Skip to content

Commit

Permalink
Merge pull request #334 from fga-gpp-mds/feature/316
Browse files Browse the repository at this point in the history
Feature/316
  • Loading branch information
guibaldissera authored May 27, 2018
2 parents ba6d9b8 + 80fe0f1 commit 1cc8132
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.nexte.nexte.LoginScene

import com.facebook.accountkit.AccountKitLoginResult


/**
* Class to define Model of Login Scene to send informations between layers
*/
Expand Down Expand Up @@ -52,9 +49,10 @@ class LoginModel {

/**
* Class responsible to store received information from Presenter to View
* @param loginResult: Result to a request for facebook API
* @param email: Email used to auth with facebook API
* @param phone: Phone used to auth with facebook API
*/
class Request(val loginResult: AccountKitLoginResult)
class Request(val email: String?, val phone: String?)

/**
* Class responsible to store received information from worker to Presenter
Expand Down
53 changes: 37 additions & 16 deletions project/app/src/main/java/com/nexte/nexte/LoginScene/LoginView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package com.nexte.nexte.LoginScene
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.facebook.accountkit.AccountKitLoginResult
import com.facebook.accountkit.ui.AccountKitConfiguration
import com.facebook.accountkit.ui.AccountKitActivity
import com.facebook.accountkit.ui.LoginType
import com.nexte.nexte.R
import android.widget.Toast
import android.util.Log
import com.facebook.accountkit.*
import kotlinx.android.synthetic.main.activity_login_view.*
import com.facebook.accountkit.ui.AccountKitConfiguration
import com.facebook.accountkit.ui.AccountKitActivity
import com.facebook.accountkit.ui.LoginType


/**
* Interface to define Display Logic to LoginView Class that will receive information
Expand Down Expand Up @@ -49,6 +50,7 @@ class LoginView : AppCompatActivity(), LoginDisplayLogic {
*/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
AccountKit.initialize(this)
setContentView(R.layout.activity_login_view)
this.setup()

Expand Down Expand Up @@ -78,16 +80,13 @@ class LoginView : AppCompatActivity(), LoginDisplayLogic {

when (requestCode) {
LoginModel.AccountKit.ACCOUNTKIT_CODE -> {
val loginResult = data?.getParcelableExtra<AccountKitLoginResult>(AccountKitLoginResult.RESULT_KEY)
val request: LoginModel.AccountKit.Request = LoginModel.AccountKit.Request(loginResult!!)
this.interactor?.accountKitAuthentication(request)
this.getAccount()
}
}
}

override fun displayAuthenticateState(viewModel: LoginModel.Authentication.ViewModel) {
val message: String = viewModel.message
Log.i("wed", "dssdsd")
val toast = Toast.makeText(this, message, Toast.LENGTH_SHORT)
toast.show()
}
Expand All @@ -110,16 +109,25 @@ class LoginView : AppCompatActivity(), LoginDisplayLogic {
}

/**
* Method responsible for setup protocol between scenes
* Gets current account from Facebook Account Kit which include user's phone number
*/
fun setup() {
val view = this
val interactor = LoginInteractor()
val presenter = LoginPresenter()
private fun getAccount() {
AccountKit.getCurrentAccount(object : AccountKitCallback<Account> {
override fun onSuccess(account: Account) {
val phoneNumber = account.getPhoneNumber()
val phoneNumberString = phoneNumber.toString()

if(phoneNumberString != "") {
Log.i("Phone number:", phoneNumberString)
val request = LoginModel.AccountKit.Request("", phoneNumberString)
interactor?.accountKitAuthentication(request)
}
}

view.interactor = interactor
interactor.presenter = presenter
presenter.view = view
override fun onError(error: AccountKitError) {
Log.e("AccountKit", error.toString())
}
})
}

/**
Expand All @@ -143,4 +151,17 @@ class LoginView : AppCompatActivity(), LoginDisplayLogic {
intent.putExtra(AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION, builder.build())
startActivityForResult(intent, LoginModel.AccountKit.ACCOUNTKIT_CODE)
}

/**
* Method responsible for setup protocol between scenes
*/
fun setup() {
val view = this
val interactor = LoginInteractor()
val presenter = LoginPresenter()

view.interactor = interactor
interactor.presenter = presenter
presenter.view = view
}
}
58 changes: 47 additions & 11 deletions project/app/src/main/java/com/nexte/nexte/LoginScene/LoginWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.nexte.nexte.LoginScene
import com.github.kittinunf.fuel.Fuel
import com.github.kittinunf.result.failure
import com.github.kittinunf.result.success
import com.nexte.nexte.UserSingleton
import org.json.JSONObject

/**
Expand All @@ -22,7 +23,7 @@ class LoginWorker {
fun authenticateUser(request: LoginModel.Authentication.Request,
completion: (LoginModel.Authentication.Response) -> Unit) {

val authentication = "http://192.168.100.2:3000/auth/login" // Works only with IP
val authentication = "http://192.168.100.7:3000/auth/login" // Local route for auth
val headers = mapOf("Content-Type" to "application/json",
"Accept-Version" to "1.0.0")
val json = JSONObject()
Expand All @@ -35,6 +36,11 @@ class LoginWorker {
val token = "1820uf09183h9d12db092ed9has9d1j020hf90aasfjialuch"
val status = LoginModel.Authentication.StatusCode.AUTHORIZED
val response = LoginModel.Authentication.Response(token, status)

// TO DO: Add more user to server to authenticate with
val player = UserSingleton.getUserInformations()
UserSingleton.setUserInformations(player)

completion(response)
}

Expand All @@ -57,18 +63,48 @@ class LoginWorker {
fun requestForAuth(request: LoginModel.AccountKit.Request,
completion: (LoginModel.AccountKit.Response) -> Unit) {

val loginResult = request.loginResult
var response: LoginModel.AccountKit.Response? = null
val authentication = "http://192.168.100.7:3000/auth/login" // Local route for auth
val headers = mapOf("Content-Type" to "application/json",
"Accept-Version" to "1.0.0")
val body = defineBodyForAccountKitAuth(request.phone, request.email)

Fuel.post(authentication).header(headers).body(body).responseString { request, response, result ->

result.success {

// TO DO: Add auth with token in NEXTE main server
val player = UserSingleton.getUserInformations()
UserSingleton.setUserInformations(player)

val response = LoginModel.AccountKit.Response(LoginModel.AccountKit.StatusCode.SUCESSED)
completion(response)
}

result.failure {
val response = LoginModel.AccountKit.Response(LoginModel.AccountKit.StatusCode.ERROR)
completion(response)
}
}
}

if (loginResult.wasCancelled()) {
response = LoginModel.AccountKit.Response(LoginModel.AccountKit.StatusCode.CANCELLED)
} else if (loginResult.error != null) {
response = LoginModel.AccountKit.Response(LoginModel.AccountKit.StatusCode.ERROR)
} else {
response = LoginModel.AccountKit.Response(LoginModel.AccountKit.StatusCode.SUCESSED)
}
/**
* Define body to authenticate user with Nexte main server
* @param phone Phone from a user - used in Account Kit auth
* @param phone Email from a user - used in Account Kit auth
*/
private fun defineBodyForAccountKitAuth(phone: String?, email: String?): String {
val json = JSONObject()

if(phone != null) {
json.put("phone", phone) // Expected test-nexte-ramires
json.put("password", "test-nexte-ramires") // Expected ramires

} else {
json.put("email", email)
json.put("password", "test-nexte-ramires") // Expected ramires
}

completion(response)
return json.toString()
}
}

Expand Down
10 changes: 2 additions & 8 deletions project/app/src/main/java/com/nexte/nexte/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,27 @@ import com.nexte.nexte.RankingScene.RankingView
class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
bottom_nav_view.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
bottom_nav_view.setOnNavigationItemReselectedListener(mOnNavigationItemReselectedListener)
this.bottom_nav_view.selectedItemId = R.id.profile


loginButton.setOnClickListener {
val intent = Intent(this, LoginView::class.java)
startActivity(intent)
}

}

private val mOnNavigationItemReselectedListener = BottomNavigationView.OnNavigationItemReselectedListener { item ->

when(item.itemId) {
R.id.profile -> {
Log.e("Resultado d", item.itemId.toString())
}
}

}
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->

private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
Log.e("Resultado do item", item.itemId.toString())
when (item.itemId) {
R.id.feed -> {
Expand Down Expand Up @@ -75,9 +70,8 @@ class MainActivity : AppCompatActivity() {
return@OnNavigationItemSelectedListener true
}
}


}

private fun openFragment(fragment: Fragment) {
var transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.frame_layout,fragment)
Expand Down

0 comments on commit 1cc8132

Please sign in to comment.