Skip to content

Commit

Permalink
[WIP]
Browse files Browse the repository at this point in the history
* ユーザーにコードを入力してもらう形式に変更
* ログイン画面のUIの管理をFragmentに任せるように修正

TODO

* ユーザー情報を取得してDBに保存する処理を実装する
  • Loading branch information
jageishi committed Apr 14, 2020
1 parent 0e5b231 commit fb3bf7b
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 95 deletions.
18 changes: 3 additions & 15 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

<application
android:name=".App"
android:allowBackup="true"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
tools:ignore="AllowBackup,GoogleAppIndexingWarning,UnusedAttribute">
tools:ignore="GoogleAppIndexingWarning,UnusedAttribute">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand All @@ -35,19 +35,7 @@
android:theme="@style/AppTheme.ActionBar" />
<activity
android:name=".login.LoginActivity"
android:launchMode="singleTop"
android:theme="@style/AppTheme.ActionBar">
<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />

<data
android:host="ageage.org"
android:scheme="https" />
</intent-filter>
</activity>
android:theme="@style/AppTheme.ActionBar" />
<activity
android:name="com.google.android.gms.oss.licenses.OssLicensesMenuActivity"
android:theme="@style/AppTheme.ActionBar" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.github.scribejava.core.model.OAuth1RequestToken
import com.github.scribejava.core.oauth.OAuth10aService
import io.reactivex.Single

private const val CALLBACK = "https://ageage.org"
private const val CALLBACK = "oob"

class HatenaOAuthManager(
private val consumerKey: String,
Expand All @@ -32,8 +32,7 @@ class HatenaOAuthManager(
fun fetchAccessToken(oauthVerifier: String): Single<String> {
return Single.create {
service?.let { oAuthService ->
val accessToken =
oAuthService.getAccessToken(requestToken, oauthVerifier)
val accessToken = oAuthService.getAccessToken(requestToken, oauthVerifier)
it.onSuccess(accessToken.rawResponse)
}
}
Expand Down
64 changes: 7 additions & 57 deletions app/src/main/java/org/ageage/eggplant/login/LoginActivity.kt
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
package org.ageage.eggplant.login

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_login.*
import org.ageage.eggplant.BuildConfig
import androidx.fragment.app.commitNow
import org.ageage.eggplant.R
import org.ageage.eggplant.common.oauth.HatenaOAuthManager

private const val KEY_OAUTH_VERIFIER = "oauth_verifier"

class LoginActivity : AppCompatActivity() {

private val oAuthManager =
HatenaOAuthManager(BuildConfig.CONSUMER_KEY, BuildConfig.CONSUMER_SECRET)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
initViews()
setupActionBar()
if (savedInstanceState == null) {
supportFragmentManager.commitNow {
replace(R.id.container, LoginFragment.newInstance())
}
}
}

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
Expand All @@ -33,53 +26,10 @@ class LoginActivity : AppCompatActivity() {
return super.onOptionsItemSelected(item)
}

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
intent?.let {
if (it.action == Intent.ACTION_VIEW) {
it.data?.let { uri ->
uri.getQueryParameter(KEY_OAUTH_VERIFIER)?.let { oAuthVerifier ->
oAuthManager.fetchAccessToken(oAuthVerifier)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show()
},
{
Toast.makeText(this, "失敗", Toast.LENGTH_SHORT).show()
}
)
}
}
}
}
}

private fun initViews() {
setupActionBar()
setupLoginButton()
}

private fun setupActionBar() {
supportActionBar?.let {
it.setDisplayHomeAsUpEnabled(true)
it.title = getString(R.string.title_login_activity)
}
}

private fun setupLoginButton() {
buttonLogin.setOnClickListener {
oAuthManager.fetchAuthorizationUrl()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(it)))
},
{
}
)
}
}
}
81 changes: 81 additions & 0 deletions app/src/main/java/org/ageage/eggplant/login/LoginFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.ageage.eggplant.login


import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.fragment_login.*
import org.ageage.eggplant.BuildConfig
import org.ageage.eggplant.R
import org.ageage.eggplant.common.oauth.HatenaOAuthManager

class LoginFragment : Fragment() {

private val viewModel: LoginViewModel by viewModels { LoginViewModelFactory() }
private val oAuthManager =
HatenaOAuthManager(BuildConfig.CONSUMER_KEY, BuildConfig.CONSUMER_SECRET)

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_login, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initViews()
}

private fun initViews() {
setupAccessButton()
setupLoginButton()
}

private fun setupAccessButton() {
buttonAccess.setOnClickListener {
oAuthManager.fetchAuthorizationUrl()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(it)))
},
{
}
)
}
}

private fun setupLoginButton() {
buttonLogin.setOnClickListener {
oAuthVerifierEditText?.text.toString().let { oAuthVerifier ->
oAuthManager.fetchAccessToken(oAuthVerifier)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
Toast.makeText(requireContext(), "成功", Toast.LENGTH_SHORT).show()
},
{
Toast.makeText(requireContext(), "失敗", Toast.LENGTH_SHORT).show()
}
)
}
}
}

companion object {
@JvmStatic
fun newInstance() = LoginFragment()
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/org/ageage/eggplant/login/LoginViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.ageage.eggplant.login

import androidx.lifecycle.ViewModel

class LoginViewModel : ViewModel() {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.ageage.eggplant.login

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

class LoginViewModelFactory : ViewModelProvider.Factory {

@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if (modelClass != LoginViewModel::class.java) {
throw IllegalArgumentException("Illegal ViewModel class.")
}

return LoginViewModel() as T
}
}
22 changes: 3 additions & 19 deletions app/src/main/res/layout/activity_login.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".login.LoginActivity">

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="@string/description_for_cooperation_hatena" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="@string/button_text_for_login_hatena" />

</LinearLayout>
tools:context=".login.LoginActivity" />
39 changes: 39 additions & 0 deletions app/src/main/res/layout/fragment_login.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".login.LoginFragment">

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="@string/description_for_cooperation_hatena" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/buttonAccess"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="@string/button_text_for_access_to_hatena" />

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/oAuthVerifierEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="@string/hint_for_login_to_hatena"
android:maxLines="1" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:text="@string/button_text_for_login_to_hatena" />

</LinearLayout>
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@
<string name="search_option_minimum_bookmark_count_five_hundred">500 users</string>
<string name="title_login_activity">ログイン</string>
<string name="description_for_cooperation_hatena">はてなにログインして連携を許可することで、Eggplantからブックマークの追加・削除などを行うことができるようになります。</string>
<string name="button_text_for_login_hatena">はてなにログインする</string>
<string name="button_text_for_access_to_hatena">はてなとの連携を許可する</string>
<string name="hint_for_login_to_hatena">コードを貼り付けてください</string>
<string name="button_text_for_login_to_hatena">はてなにログインする</string>
</resources>

0 comments on commit fb3bf7b

Please sign in to comment.