Skip to content

Commit

Permalink
[WIP]
Browse files Browse the repository at this point in the history
TODO

* ログイン状態を判定する処理が必要
* Statusクラスは改良の余地があるかも?
  • Loading branch information
jageishi committed Dec 9, 2019
1 parent b9fa7e1 commit eda29af
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 13 deletions.
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ dependencies {
implementation 'io.reactivex.rxjava2:rxkotlin:2.4.0'
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation "se.akerfeldt:okhttp-signpost:1.1.0"
debugImplementation 'com.facebook.stetho:stetho:1.5.1'

def okhttp_version = '4.2.0'
implementation "com.squareup.okhttp3:okhttp:$okhttp_version"
Expand All @@ -72,10 +74,9 @@ dependencies {
implementation "com.github.scribejava:scribejava:$scribe_java_version"
implementation "com.github.scribejava:scribejava-core:$scribe_java_version"


def room_version = "2.2.2"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
kapt "androidx.room:room-compiler:$room_version"

implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0'
Expand Down
6 changes: 5 additions & 1 deletion app/src/debug/java/org/ageage/eggplant/DebugApp.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package org.ageage.eggplant

import android.app.Application
import com.facebook.stetho.Stetho
import org.ageage.eggplant.common.api.Client
import org.ageage.eggplant.common.db.AppDatabase
import timber.log.Timber

class DebugApp : Application() {
override fun onCreate() {
super.onCreate()
Timber.plant(Timber.DebugTree())
Client.setUp()
Stetho.initializeWithDefaults(this)
Client.setUp(this)
AppDatabase.setUp(this)
}
}
4 changes: 3 additions & 1 deletion app/src/main/java/org/ageage/eggplant/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package org.ageage.eggplant

import android.app.Application
import org.ageage.eggplant.common.api.Client
import org.ageage.eggplant.common.db.AppDatabase

class App : Application() {
override fun onCreate() {
super.onCreate()
Client.setUp()
Client.setUp(this)
AppDatabase.setUp(this)
}
}
24 changes: 22 additions & 2 deletions app/src/main/java/org/ageage/eggplant/common/api/Client.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package org.ageage.eggplant.common.api

import android.content.Context
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.ageage.eggplant.BuildConfig
import org.ageage.eggplant.common.enums.Endpoint
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import se.akerfeldt.okhttp.signpost.OkHttpOAuthConsumer
import se.akerfeldt.okhttp.signpost.SigningInterceptor

class Client {
companion object {

private var client: OkHttpClient? = null

fun setUp() {
fun setUp(context: Context) {
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.level = HttpLoggingInterceptor.Level.BASIC

Expand All @@ -22,7 +26,7 @@ class Client {
}

fun getInstance() =
client ?: throw IllegalStateException("You must call setup method at first.")
client ?: throw IllegalStateException("You must call setUp method at first.")

fun retrofitClient(endpoint: Endpoint): Retrofit {
return Retrofit.Builder()
Expand All @@ -32,5 +36,21 @@ class Client {
.baseUrl(endpoint.url)
.build()
}

fun oAuthClient(endpoint: Endpoint, token: String, secret: String): Retrofit {
OkHttpOAuthConsumer(BuildConfig.CONSUMER_KEY, BuildConfig.CONSUMER_SECRET).let {
it.setTokenWithSecret(token, secret)
return Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(
OkHttpClient.Builder()
.addInterceptor(SigningInterceptor(it))
.build()
)
.baseUrl(endpoint.url)
.build()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity
@Entity(tableName = "user")
data class User(
@PrimaryKey(autoGenerate = true) val id: Int,
@ColumnInfo(name = "name") val name: String,
@PrimaryKey val name: String,
@ColumnInfo(name = "oauth_token") val oAuthToken: String,
@ColumnInfo(name = "oauth_token_secret") val oAuthTokenSecret: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ enum class Endpoint(
),
HATENA_STAR(
"https://s.hatena.com"
),
HATENA_API(
"https://bookmark.hatenaapis.com"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.github.scribejava.core.builder.api.DefaultApi10a

class HatenaOAuthApi : DefaultApi10a() {
override fun getRequestTokenEndpoint(): String {
return "https://www.hatena.com/oauth/initiate?scope=read_public,write_public"
return "https://www.hatena.com/oauth/initiate?scope=read_public,write_public,read_private,write_public"
}

override fun getAuthorizationBaseUrl(): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package org.ageage.eggplant.common.repository

import org.ageage.eggplant.BuildConfig
import org.ageage.eggplant.common.api.Client
import org.ageage.eggplant.common.api.UserService
import org.ageage.eggplant.common.db.AppDatabase
import org.ageage.eggplant.common.db.entity.User
import org.ageage.eggplant.common.enums.Endpoint
import org.ageage.eggplant.common.oauth.HatenaOAuthManager

class LoginRepository {
Expand All @@ -12,7 +17,14 @@ class LoginRepository {

fun fetchAccessToken(oAuthVerifier: String) = oAuthManager.fetchAccessToken(oAuthVerifier)

fun fetchUserData() {
fun fetchUser(token: String, tokenSecret: String) =
Client.oAuthClient(Endpoint.HATENA_API, token, tokenSecret)
.create(UserService::class.java)
.userData()

fun saveUser(user: User) {
AppDatabase.getInstance()
.userDao()
.insert(user)
}
}
6 changes: 5 additions & 1 deletion app/src/main/java/org/ageage/eggplant/login/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.commitNow
import org.ageage.eggplant.R

class LoginActivity : AppCompatActivity() {
class LoginActivity : AppCompatActivity(), LoginFragment.OnLoginListener {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -26,6 +26,10 @@ class LoginActivity : AppCompatActivity() {
return super.onOptionsItemSelected(item)
}

override fun onLogin() {
finish()
}

private fun setupActionBar() {
supportActionBar?.let {
it.setDisplayHomeAsUpEnabled(true)
Expand Down
28 changes: 27 additions & 1 deletion app/src/main/java/org/ageage/eggplant/login/LoginFragment.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.ageage.eggplant.login


import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
Expand All @@ -17,6 +18,19 @@ import org.ageage.eggplant.R
class LoginFragment : Fragment() {

private val viewModel: LoginViewModel by viewModels { LoginViewModelFactory() }
private var loginListener: OnLoginListener? = null

interface OnLoginListener {
fun onLogin()
}

override fun onAttach(context: Context) {
super.onAttach(context)

if (context is OnLoginListener) {
loginListener = context
}
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
Expand All @@ -32,6 +46,12 @@ class LoginFragment : Fragment() {
initViews()
}

override fun onDetach() {
super.onDetach()

loginListener = null
}

private fun initViewModel() {
viewModel.statusFetchAuthorizationUrl.observe(viewLifecycleOwner, Observer {
when (it) {
Expand All @@ -55,7 +75,13 @@ class LoginFragment : Fragment() {
is LoginViewModel.Status.Loading -> {
}
is LoginViewModel.Status.Success -> {
Toast.makeText(requireContext(), "成功", Toast.LENGTH_SHORT).show()
loginListener?.onLogin()
Toast.makeText(
requireContext(),
getString(R.string.succeeded_to_login),
Toast.LENGTH_SHORT
).show()

}
is LoginViewModel.Status.Error -> {
Toast.makeText(
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/org/ageage/eggplant/login/LoginViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.addTo
import org.ageage.eggplant.common.db.entity.User
import org.ageage.eggplant.common.repository.LoginRepository
import org.ageage.eggplant.common.schedulerprovider.BaseSchedulerProvider

Expand Down Expand Up @@ -50,10 +51,17 @@ class LoginViewModel(

fun login(oAuthVerifier: String) {
repository.fetchAccessToken(oAuthVerifier)
.flatMap {
repository.fetchUser(it.token, it.tokenSecret)
.map { user ->
User(user.name, it.token, it.tokenSecret)
}
}
.doOnSubscribe {
_statusLogin.postValue(Status.Loading())
}
.doOnSuccess {
repository.saveUser(it)
_statusLogin.postValue(Status.Success(""))
}
.doOnError {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@
<string name="button_text_for_login_to_hatena">はてなにログインする</string>
<string name="failed_to_link_with_hatena">はてなとの連携に失敗しました。</string>
<string name="failed_to_login">ログインに失敗しました。</string>
<string name="succeeded_to_login">ログインに成功しました。</string>
</resources>

0 comments on commit eda29af

Please sign in to comment.