Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kotlin 변환 작업 #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
apply plugin: 'com.android.application'


apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 25
Expand All @@ -22,10 +23,25 @@ android {
}
}

kotlin {
experimental {
coroutines 'enable'
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.0.+'
compile 'com.android.support:recyclerview-v7:25.0.+'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'

kapt 'com.android.databinding:compiler:3.0.0-alpha6'

compile 'org.jetbrains.anko:anko-commons:0.10.1'
compile 'org.jetbrains.anko:anko-sdk25:0.10.1'
compile 'org.jetbrains.anko:anko-sdk25-coroutines:0.10.1'

compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.3-2"

compile ('io.socket:socket.io-client:0.8.3') {
exclude group: 'org.json', module: 'json'
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.nkzawa.socketio.androidchat

import android.app.Application
import io.socket.client.IO
import io.socket.client.Socket

import java.net.URISyntaxException

class ChatApplication : Application() {

var socket: Socket = try {
IO.socket(Constants.CHAT_SERVER_URL)
} catch (e: URISyntaxException) {
throw RuntimeException(e)
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.nkzawa.socketio.androidchat

object Constants {
const val CHAT_SERVER_URL = "https://socketio-chat.now.sh/"
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.github.nkzawa.socketio.androidchat

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import android.view.View.OnClickListener
import android.view.inputmethod.EditorInfo
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import io.socket.client.Socket
import io.socket.emitter.Emitter
import kotlinx.android.synthetic.main.activity_login.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.sdk25.coroutines.onEditorAction
import org.json.JSONException
import org.json.JSONObject


/**
* A login screen that offers login via username.
*/
class LoginActivity : Activity() {

private var userName: String? = null

private lateinit var socket: Socket

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)

val app = application as ChatApplication
socket = app.socket

// Set up the login form.
username_input.onEditorAction(returnValue = true) { v, actionId, event ->
if (actionId == R.id.login || actionId == EditorInfo.IME_NULL) {
attemptLogin()
}
}

sign_in_button.onClick { attemptLogin() }

socket.on("login", onLogin)
}

override fun onDestroy() {
super.onDestroy()

socket.off("login", onLogin)
}

/**
* Attempts to sign in the account specified by the login form.
* If there are form errors (invalid username, missing fields, etc.), the
* errors are presented and no actual login attempt is made.
*/
private fun attemptLogin() {
// Reset errors.
username_input.error = null

// Store values at the time of the login attempt.
val username = username_input.text.toString().trim()

// Check for a valid username.
if (username.isNullOrBlank()) {
// There was an error; don't attempt login and focus the first
// form field with an error.
username_input.error = getString(R.string.error_field_required)
username_input.requestFocus()
return
}

userName = username

// perform the user login attempt.
socket.emit("add user", username)
}

private val onLogin = Emitter.Listener { args ->
val data = args[0] as JSONObject

val numUsers: Int
try {
numUsers = data.getInt("numUsers")
} catch (e: JSONException) {
return@Listener
}

val intent = Intent()
intent.putExtra("username", userName)
intent.putExtra("numUsers", numUsers)
setResult(Activity.RESULT_OK, intent)
finish()
}
}



This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.nkzawa.socketio.androidchat

import android.os.Bundle
import android.support.v7.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
Loading