Skip to content

Commit

Permalink
detail user
Browse files Browse the repository at this point in the history
  • Loading branch information
narayoga committed Sep 25, 2023
0 parents commit 5567586
Show file tree
Hide file tree
Showing 61 changed files with 1,557 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
63 changes: 63 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("kotlin-parcelize")
}

android {
namespace = "com.example.githubuser"
compileSdk = 33

defaultConfig {
applicationId = "com.example.githubuser"
minSdk = 24
targetSdk = 33
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_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}

buildFeatures {
viewBinding = true
}

}

dependencies {

implementation ("com.squareup.retrofit2:retrofit:2.9.0")
implementation ("com.squareup.retrofit2:converter-gson:2.9.0")
implementation ("com.squareup.okhttp3:logging-interceptor:4.11.0")

implementation ("com.github.bumptech.glide:glide:4.16.0")

implementation ("com.squareup.picasso:picasso:2.8")

implementation ("androidx.lifecycle:lifecycle-runtime-ktx:2.4.0-rc01")

implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.9.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
21 changes: 21 additions & 0 deletions 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.githubuser

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.githubuser", appContext.packageName)
}
}
31 changes: 31 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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.GithubUser"
tools:targetApi="31">
<activity
android:name=".ui.ProfileActivity"
android:exported="false" />
<activity
android:name=".ui.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>
12 changes: 12 additions & 0 deletions app/src/main/java/com/example/githubuser/data/model/Profile.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.githubuser.data.model

data class Profile(
val login: String,
val id: Int,
val avatar_url: String,
val followers_url: String,
val following_url: String,
val name: String,
val following: Int,
val followers: Int
)
9 changes: 9 additions & 0 deletions app/src/main/java/com/example/githubuser/data/model/User.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.githubuser.data.model

import com.google.gson.annotations.SerializedName

data class User(
@field:SerializedName("items")
val items: ArrayList<UserDetail>
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.githubuser.data.model

data class UserDetail(
val login: String,
val id: Int,
val avatar_url: String
)
37 changes: 37 additions & 0 deletions app/src/main/java/com/example/githubuser/data/networking/Api.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.example.githubuser.data.networking

import com.example.githubuser.data.model.Profile
import com.example.githubuser.data.model.User
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Headers
import retrofit2.http.Path
import retrofit2.http.Query

interface Api {
// search user
@GET("search/users")
@Headers("Authorization: token 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")
fun getUsers(
@Query("q") query: String
): Call<User>

// detail user
@GET("users/{username}")
@Headers("Authorization: token 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")
fun getProfile(
@Path("username") username: String?
): Call<Profile>

@GET("users/{username}/followers")
@Headers("Authorization: token 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")
fun getFollowers(
@Path("username") username: String
): Call<ArrayList<Profile>>

@GET("users/{username}/following")
@Headers("Authorization: token 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")
fun getFollowing(
@Path("username") username: String
): Call<ArrayList<Profile>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.githubuser.data.networking

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitClient {
private const val BASE_URL = "https://api.github.com/"

val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()

val apiInstance = retrofit.create(Api::class.java)
}
81 changes: 81 additions & 0 deletions app/src/main/java/com/example/githubuser/ui/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.example.githubuser.ui

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.githubuser.R
import com.example.githubuser.data.model.UserDetail
import com.example.githubuser.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

private lateinit var binding : ActivityMainBinding
private lateinit var viewModel: UserViewModel
private lateinit var adapter: UserAdapter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

adapter = UserAdapter()
adapter.notifyDataSetChanged()

adapter.setOnItemClickFunction(object : UserAdapter.OnItemClickCallback{
override fun onFunctionClicked(data: UserDetail){
Intent(this@MainActivity, ProfileActivity::class.java).also {
it.putExtra(ProfileActivity.EXTRA_USERNAME, data.login)
startActivity(it)
}
}
})

viewModel = ViewModelProvider(this, ViewModelProvider.NewInstanceFactory())
.get(UserViewModel::class.java)

binding.apply {
rvUser.layoutManager = LinearLayoutManager(this@MainActivity)
rvUser.setHasFixedSize(true)
rvUser.adapter = adapter

searchBtn.setOnClickListener{
searchUser()
}

inputQuery.setOnKeyListener { v, keyCode, event ->
if (event.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER){
return@setOnKeyListener true
}
return@setOnKeyListener false
}
}

viewModel.getUsers().observe(this) {
if (it != null) {
adapter.setList(it)
loadingAnimation(false)
}
}
}

private fun searchUser() {
binding.apply {
val query = inputQuery.text.toString()
if(query.isEmpty()) return
loadingAnimation(true)
viewModel.setSearchUsers(query)
}
}

private fun loadingAnimation(state: Boolean){
if(state){
binding.progressBar.visibility = View.VISIBLE
}else{
binding.progressBar.visibility = View.GONE
}
}
}
Loading

0 comments on commit 5567586

Please sign in to comment.