Skip to content
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
15 changes: 15 additions & 0 deletions Homework/Handnew04/RxStudy/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ android {
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

buildConfigField("String", "BASE_URL", '"https://r5670326j8.execute-api.ap-northeast-2.amazonaws.com/delivery_server/"')
}

buildTypes {
Expand Down Expand Up @@ -43,7 +45,20 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
//rxJava
implementation 'com.jakewharton.rxbinding3:rxbinding:3.1.0'
//retrofit2
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
//okHttp
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.1'
//room
implementation 'androidx.room:room-runtime:2.4.2'
implementation 'androidx.room:room-ktx:2.4.2'
implementation 'androidx.room:room-rxjava2:2.4.2'
kapt 'androidx.room:room-compiler:2.4.2'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
Expand Down
54 changes: 29 additions & 25 deletions Homework/Handnew04/RxStudy/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.rxstudy">
xmlns:tools="http://schemas.android.com/tools"
package="com.example.rxstudy">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.RxStudy"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<uses-permission android:name="android.permission.INTERNET" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".observerpattern.ui.ObserverPatternActivity"/>
<activity android:name=".scheduler.SchedulerSampleActivity"/>
<activity android:name=".operator.FlowControlOperatorSampleActivity"/>
<activity android:name=".rxbinding.RxBindingSampleActivity"/>
</application>
<application
android:name=".RxApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.RxStudy"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.observerpattern.ui.ObserverPatternActivity" />
<activity android:name=".scheduler.SchedulerSampleActivity" />
<activity android:name=".operator.FlowControlOperatorSampleActivity" />
<activity android:name=".rxbinding.RxBindingSampleActivity" />
<activity android:name=".login.ui.LoginExampleActivity" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.rxstudy.observerpattern
package com.example.observerpattern

interface DefaultObserver<T> {
fun notifyDataIsArrived(value : T)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.rxstudy.observerpattern
package com.example.observerpattern

import java.util.ArrayList

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.example.rxstudy.observerpattern.ui
package com.example.observerpattern.ui

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.rxstudy.R
import com.example.rxstudy.observerpattern.DefaultSubject
import com.example.observerpattern.DefaultSubject

class ObserverPatternActivity : AppCompatActivity() {
companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.example.rxstudy.observerpattern.ui
package com.example.observerpattern.ui

import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatEditText
import androidx.core.widget.doAfterTextChanged
import com.example.rxstudy.observerpattern.DefaultObserver
import com.example.observerpattern.DefaultObserver

class PercentEditText : AppCompatEditText, DefaultObserver<Int> {
constructor(context: Context) : super(context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.example.rxstudy.observerpattern.ui
package com.example.observerpattern.ui

import android.content.Context
import android.util.AttributeSet
import android.widget.SeekBar
import androidx.appcompat.widget.AppCompatSeekBar
import com.example.rxstudy.observerpattern.DefaultObserver
import com.example.observerpattern.DefaultObserver

class PercentSeekBar : AppCompatSeekBar, DefaultObserver<Int> {
constructor(context: Context) : super(context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.example.rxstudy.observerpattern.ui
package com.example.observerpattern.ui

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import com.example.rxstudy.observerpattern.DefaultObserver
import com.example.observerpattern.DefaultObserver

class PercentTextView : AppCompatTextView, DefaultObserver<Int> {
constructor(context: Context) : super(context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.example.rxstudy

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import com.example.rxstudy.observerpattern.ui.ObserverPatternActivity
import androidx.appcompat.app.AppCompatActivity
import com.example.observerpattern.ui.ObserverPatternActivity
import com.example.rxstudy.login.ui.LoginExampleActivity
import com.example.rxstudy.operator.FlowControlOperatorSampleActivity
import com.example.rxstudy.rxbinding.RxBindingSampleActivity
import com.example.rxstudy.scheduler.SchedulerSampleActivity
Expand All @@ -14,6 +15,7 @@ class MainActivity : AppCompatActivity() {
private lateinit var schedulerSampleButton: Button
private lateinit var flowOperatorButton: Button
private lateinit var rxBindingSampleButton: Button
private val loginExampleButton: Button by lazy { findViewById(R.id.bt_login_example) }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -47,5 +49,10 @@ class MainActivity : AppCompatActivity() {
val intent = Intent(this@MainActivity, RxBindingSampleActivity::class.java)
startActivity(intent)
}

loginExampleButton.setOnClickListener {
val intent = Intent(this@MainActivity, LoginExampleActivity::class.java)
startActivity(intent)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.rxstudy

import android.app.Application
import com.example.rxstudy.login.data.local.DatabaseClient
import io.reactivex.exceptions.UndeliverableException
import io.reactivex.plugins.RxJavaPlugins

class RxApplication: Application() {
override fun onCreate() {
super.onCreate()
RxJavaPlugins.setErrorHandler { e: Throwable ->
if (e is UndeliverableException) {
return@setErrorHandler
}
}
DatabaseClient.createDatabase(this)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.rxstudy.login

import android.content.Context
import android.content.Context.MODE_PRIVATE

class PrefUtil(context: Context) {
companion object {
const val DEFAULT_VALUE = ""
const val PREF_DEFAULT = "PREF_DEFAULT"
const val ACCESS_TOKEN = "LOGIN_TOKEN"
}

private val sharedPref = context.getSharedPreferences(PREF_DEFAULT, MODE_PRIVATE)
private fun getEdit() = sharedPref.edit()

fun saveToken(token: String) {
getEdit().putString(ACCESS_TOKEN, token).apply()
}

fun getToken() = sharedPref.getString(ACCESS_TOKEN, DEFAULT_VALUE)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.rxstudy.login.data.local

import androidx.room.Database
import androidx.room.RoomDatabase
import com.example.rxstudy.login.data.local.token.LocalTokenDao
import com.example.rxstudy.login.data.local.token.LocalTokenItem

@Database(entities = [LocalTokenItem::class], version = 1)
abstract class AppDatabase: RoomDatabase() {
abstract fun localTokenDao(): LocalTokenDao
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.rxstudy.login.data.local

import android.content.Context
import androidx.room.Room
import com.example.rxstudy.login.data.local.token.LocalTokenDao

object DatabaseClient {
private lateinit var appDatabase: AppDatabase

fun createDatabase(context: Context) {
appDatabase = Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
"database"
).build()
}

fun tokenDao(): LocalTokenDao = appDatabase.localTokenDao()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.rxstudy.login.data.local.token

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import io.reactivex.Completable
import io.reactivex.Single

@Dao
interface LocalTokenDao {
@Query(value = "SELECT * FROM token LIMIT 1")
fun getToken(): Single<LocalTokenItem>

@Insert
fun saveToken(tokenItem: LocalTokenItem): Completable

@Query(value = "DELETE FROM token")
fun deleteAllCachedToken(): Completable

@Query(value = "SELECT * FROM token LIMIT 1")
fun getTokenNotRx(): LocalTokenItem

@Insert
fun saveTokenNotRx(tokenItem: LocalTokenItem)

@Query(value = "DELETE FROM token")
fun deleteAllCachedTokenNotRx()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.rxstudy.login.data.local.token

import io.reactivex.Completable
import io.reactivex.Single

interface LocalTokenDataSource {
fun getToken(): Single<LocalTokenItem>
fun saveToken(tokenItem: LocalTokenItem): Completable
fun deleteAllCachedToken(): Completable
fun getTokenNotRx(): LocalTokenItem
fun saveTokenNotRx(tokenItem: LocalTokenItem)
fun deleteAllCachedTokenNotRx()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.rxstudy.login.data.local.token

import io.reactivex.Completable
import io.reactivex.Single
import io.reactivex.schedulers.Schedulers

class LocalTokenDataSourceImpl(private val localTokenDao: LocalTokenDao) : LocalTokenDataSource {
override fun getToken(): Single<LocalTokenItem> {
return localTokenDao
.getToken()
.subscribeOn(Schedulers.io())
}

override fun saveToken(tokenItem: LocalTokenItem): Completable {
return localTokenDao
.saveToken(tokenItem)
.subscribeOn(Schedulers.io())
}

override fun deleteAllCachedToken(): Completable {
return localTokenDao
.deleteAllCachedToken()
.subscribeOn(Schedulers.io())
}

override fun getTokenNotRx(): LocalTokenItem {
return localTokenDao
.getTokenNotRx()
}

override fun saveTokenNotRx(tokenItem: LocalTokenItem) {
return localTokenDao
.saveTokenNotRx(tokenItem)
}

override fun deleteAllCachedTokenNotRx() {
return localTokenDao
.deleteAllCachedTokenNotRx()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.rxstudy.login.data.local.token

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "token")
data class LocalTokenItem(
@PrimaryKey(autoGenerate = true) val idx: Int = 0,
@ColumnInfo(name = "accessToken") val accessToken: String,
@ColumnInfo(name = "refreshToken") val refreshToken: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.rxstudy.login.data.local.token

import com.example.rxstudy.login.data.remote.login.LoginItem

object LocalTokenMapper {
fun mappingRemoteDataToLocal(loginItem: LoginItem): LocalTokenItem {
return LocalTokenItem(
accessToken = loginItem.access,
refreshToken = loginItem.refresh
)
}
}
Loading