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 Week09/LinLin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
1 change: 1 addition & 0 deletions Week09/LinLin/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
68 changes: 68 additions & 0 deletions Week09/LinLin/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.compose)
}

android {
namespace = "com.example.week09"
compileSdk {
version = release(36) {
minorApiLevel = 1
}
}

defaultConfig {
applicationId = "com.example.week09"
minSdk = 24
targetSdk = 36
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
buildFeatures {
compose = true
}
}

dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.lifecycle.viewmodel.ktx)
implementation(libs.androidx.lifecycle.viewmodel.compose)
implementation(libs.androidx.lifecycle.runtime.compose)
implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.compose.ui)
implementation(libs.androidx.compose.ui.graphics)
implementation(libs.androidx.compose.ui.tooling.preview)
implementation(libs.androidx.compose.material3)
implementation(libs.androidx.compose.foundation)
implementation(libs.coil.compose)
implementation(libs.retrofit)
implementation(libs.retrofit.converter.gson)
implementation(libs.okhttp.logging)
implementation(libs.gson)
implementation(libs.kotlinx.coroutines.android)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.compose.ui.test.junit4)
debugImplementation(libs.androidx.compose.ui.tooling)
debugImplementation(libs.androidx.compose.ui.test.manifest)
}
21 changes: 21 additions & 0 deletions Week09/LinLin/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.week09

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.week09", appContext.packageName)
}
}
29 changes: 29 additions & 0 deletions Week09/LinLin/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-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"
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.Week09">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.Week09">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
28 changes: 28 additions & 0 deletions Week09/LinLin/app/src/main/java/com/example/week09/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.week09

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.ui.Modifier
import com.example.week09.ui.mypage.MyPageScreen
import com.example.week09.ui.theme.Week09Theme

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
Week09Theme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
MyPageScreen(
modifier = Modifier.padding(innerPadding),
)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.week09.data

import com.example.week09.data.remote.ReqResService
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object NetworkClient {

private const val BASE_URL = "https://reqres.in/"

val reqResService: ReqResService by lazy {
val logging = HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
}

val authInterceptor = Interceptor { chain ->
val request = chain.request().newBuilder()
.addHeader("x-api-key", "reqres_3cc917778886402cb49885a9215fb6ff")
.build()
chain.proceed(request)
}

val client = OkHttpClient.Builder()
.addInterceptor(logging)
.addInterceptor(authInterceptor)
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.build()

Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(ReqResService::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.week09.data.model

import com.google.gson.annotations.SerializedName

data class UserData(
val id: Int,
val email: String,
@SerializedName("first_name")
val firstName: String,
@SerializedName("last_name")
val lastName: String,
val avatar: String,
) {
val displayName: String get() = "$firstName $lastName"
}

data class UserResponse(
val data: UserData,
)

data class UserListResponse(
val page: Int,
@SerializedName("per_page")
val perPage: Int,
val total: Int,
@SerializedName("total_pages")
val totalPages: Int,
val data: List<UserData>,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.week09.data.remote

import com.example.week09.data.model.UserListResponse
import com.example.week09.data.model.UserResponse
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query

interface ReqResService {

@GET("api/users/{id}")
suspend fun getUserProfile(
@Path("id") userId: Int,
): Response<UserResponse>

@GET("api/users")
suspend fun getUserList(
@Query("page") page: Int = 1,
): Response<UserListResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.week09.data.repository

import com.example.week09.data.NetworkClient
import com.example.week09.data.model.UserData
import com.example.week09.data.remote.ReqResService

class UserRepository(
private val service: ReqResService = NetworkClient.reqResService,
) {

suspend fun getUserProfile(userId: Int): Result<UserData> = runCatching {
val response = service.getUserProfile(userId)
if (!response.isSuccessful) {
error("Failed to load profile: ${response.code()}")
}
response.body()?.data ?: error("Empty profile response")
}

suspend fun getFollowingUsers(page: Int = 1, excludeUserId: Int): Result<List<UserData>> =
runCatching {
val response = service.getUserList(page)
if (!response.isSuccessful) {
error("Failed to load users: ${response.code()}")
}
response.body()?.data?.filter { it.id != excludeUserId }.orEmpty()
}
}
Loading