Skip to content

Commit

Permalink
Merge pull request #12 from NOW-SOPT-ANDROID/feat/#10-week4_xml
Browse files Browse the repository at this point in the history
[MERGE/#10] week4 xml 필수과제 구현
  • Loading branch information
hyeeum authored May 23, 2024
2 parents 3598df2 + 7e05a20 commit 45826d8
Show file tree
Hide file tree
Showing 50 changed files with 1,072 additions and 306 deletions.
17 changes: 17 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'org.jetbrains.kotlin.plugin.serialization' version '1.9.0'
}

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())

android {
namespace 'com.sopt.now'
compileSdk 34
Expand All @@ -15,6 +19,7 @@ android {
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildConfigField "String", "AUTH_BASE_URL", properties["base.url"]
}

buildTypes {
Expand All @@ -32,6 +37,7 @@ android {
}
buildFeatures {
viewBinding = true
buildConfig = true
}
}

Expand All @@ -42,6 +48,7 @@ dependencies {
implementation 'com.google.android.material:material:1.11.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.activity:activity:1.8.0'
implementation 'androidx.test:core-ktx:1.5.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
Expand All @@ -50,4 +57,14 @@ dependencies {
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2"
implementation "androidx.fragment:fragment-ktx:1.6.1"
implementation "androidx.activity:activity-ktx:1.8.0"

//api
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.1'
implementation 'com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:1.0.0'
// define a BOM and its version
implementation(platform("com.squareup.okhttp3:okhttp-bom:4.10.0"))
// define any required OkHttp artifacts without version
implementation("com.squareup.okhttp3:okhttp")
implementation("com.squareup.okhttp3:logging-interceptor")
}
12 changes: 9 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand All @@ -11,15 +13,19 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.NOWSOPTAndroid"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".SignUpActivity"
android:name=".presentation.auth.changepassword.ChangePasswordActivity"
android:exported="false" />
<activity
android:name=".presentation.auth.signup.SignUpActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:name=".presentation.main.MainActivity"
android:exported="false" />
<activity
android:name=".LoginActivity"
android:name=".presentation.auth.login.LoginActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
60 changes: 60 additions & 0 deletions app/src/main/java/com/sopt/now/ApiFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.sopt.now

import android.util.Log
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import com.sopt.now.presentation.auth.login.LoginActivity
import com.sopt.now.data.remote.service.AuthService
import com.sopt.now.data.remote.service.FollwerService
import com.sopt.now.data.remote.service.UserService
import kotlinx.serialization.json.Json
import okhttp3.Interceptor
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Response
import retrofit2.Retrofit
import java.io.IOException

object ApiFactory {
private const val BASE_URL: String = BuildConfig.AUTH_BASE_URL
const val USER_ID:String = "userId"

val retrofit: Retrofit by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
.build()
}

val userRetrofit: Retrofit by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.client(provideOkHttpClient(HeaderInterceptor()))
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
.build()
}
private fun provideOkHttpClient(interceptor: HeaderInterceptor): OkHttpClient
= OkHttpClient.Builder().run {
addInterceptor(interceptor)
build()
}
class HeaderInterceptor : Interceptor {
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response = with(chain) {
val accessToken = LoginActivity.prefs.getString(USER_ID, "")
val newRequest = request().newBuilder()
.addHeader("memberId", accessToken)
.build()
proceed(newRequest)
}
}
inline fun <reified T> create(): T = retrofit.create(T::class.java)
inline fun <reified T> createUser(): T = userRetrofit.create(T::class.java)
}

object ServicePool {
val authService = ApiFactory.create<AuthService>()
val userService = ApiFactory.createUser<UserService>()
val followerService = ApiFactory.create<FollwerService>()
}


30 changes: 30 additions & 0 deletions app/src/main/java/com/sopt/now/BindingFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.sopt.now

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding

abstract class BindingFragment<T : ViewBinding> : Fragment() {
private var _binding: T? = null
val binding: T
get() = requireNotNull(_binding) { "_binding이 null이 아닌 경우만 _binding 반환" }

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = fragmentBinding(inflater, container)
return binding.root
}

abstract fun fragmentBinding(inflater: LayoutInflater, container: ViewGroup?): T

override fun onDestroyView() {
_binding = null
super.onDestroyView()
}
}
80 changes: 0 additions & 80 deletions app/src/main/java/com/sopt/now/LoginActivity.kt

This file was deleted.

38 changes: 0 additions & 38 deletions app/src/main/java/com/sopt/now/MyPageFragment.kt

This file was deleted.

21 changes: 21 additions & 0 deletions app/src/main/java/com/sopt/now/PreferenceUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.sopt.now

import android.content.Context
import android.content.SharedPreferences

class PreferenceUtil(context: Context) {
private val prefs: SharedPreferences =
context.getSharedPreferences(USER_ID, Context.MODE_PRIVATE)

fun getString(key: String, defValue: String): String {
return prefs.getString(key, defValue).toString()
}

fun setString(key: String, str: String) {
prefs.edit().putString(key, str).apply()
}

companion object {
const val USER_ID = "userId"
}
}
27 changes: 0 additions & 27 deletions app/src/main/java/com/sopt/now/SearchFragment.kt

This file was deleted.

Loading

0 comments on commit 45826d8

Please sign in to comment.