Skip to content

Commit

Permalink
nextActivityStart
Browse files Browse the repository at this point in the history
시작
  • Loading branch information
KWY0218 committed Nov 11, 2021
1 parent 9aa5c32 commit 50699d9
Show file tree
Hide file tree
Showing 14 changed files with 325 additions and 40 deletions.
4 changes: 3 additions & 1 deletion .idea/misc.xml

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

7 changes: 6 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.INTERNET"/>

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity
android:name=".LectureDetailsActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/example/myapplication/GoActivity.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.example.myapplication

interface goActivity {
interface GoActivity {
fun goActivity(id:Int?)
}
81 changes: 63 additions & 18 deletions app/src/main/java/com/example/myapplication/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,34 +1,79 @@
package com.example.myapplication

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.myapplication.adapter.HanGangAdapter
import com.example.myapplication.databinding.ActivityMainBinding
import com.example.myapplication.repository.Repository

class MainActivity : AppCompatActivity() {

class MainActivity: AppCompatActivity(),GoActivity {
private lateinit var binding: ActivityMainBinding
private lateinit var viewModel: MainViewModel
private lateinit var hangangAdapter:HanGangAdapter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val repository = Repository()
val viewModelFactory = MainViewModelFactory(repository)
viewModel = ViewModelProvider(this,viewModelFactory).get(MainViewModel::class.java)
viewModel.getStatusCode()

viewModel.myResponse.observe(this, Observer { response->
if(response.isSuccessful){
Log.d("Response1","status: "+response.body()?.statusCode.toString())
Log.d("Response1","status: "+response.body()?.body?.bodyInForce.toString())
Log.d("Response1","status: "+response.body()?.body?.bodyInVersion.toString())
}else{
Log.d("Response1","status: "+response.errorBody().toString())
Log.d("Response1","status: "+response.code().toString())
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
hangangAdapter = HanGangAdapter(this)
viewModel = ViewModelProvider(this, MainViewModelFactory(Repository()))
.get(MainViewModel::class.java)

with(binding) {

with(recyclerview) {
adapter = hangangAdapter
layoutManager = LinearLayoutManager(this@MainActivity)
}

with(viewModel) {
getHangangList(10, Integer.parseInt(page.text.toString()))
lectureList.observe(this@MainActivity, Observer { response ->
if (response.isSuccessful) {
response.body()?.resultList.let {
hangangAdapter.submitList(it)
}

response.body()?.resultList?.forEach {
Log.d("Response1", "id: " + it.id)
Log.d("Response1", "professor: " + it.professor)
Log.d("Response1", "subjectName: " + it.subjectName)
Log.d("Response1", "-----------------------------------")
}

} else {
Log.d("Response1", "status: " + response.errorBody().toString())
Log.d("Response1", "status: " + response.code().toString())
}

})
}
})


rightBtn.setOnClickListener {
if (Integer.parseInt(page.text.toString()) + 1 < 102) {
page.setText((Integer.parseInt(page.text.toString()) + 1).toString())
viewModel.getHangangList(10, Integer.parseInt(page.text.toString()))
}
}

leftBtn.setOnClickListener {
if (Integer.parseInt(page.text.toString()) - 1 > 0) {
page.setText((Integer.parseInt(page.text.toString()) - 1).toString())
viewModel.getHangangList(10, Integer.parseInt(page.text.toString()))
}
}
}
}

override fun goActivity(id: Int?) {
val intent = Intent(this,LectureDetailsActivity::class.java)
intent.putExtra("id",id)
startActivity(intent)
}
}
10 changes: 9 additions & 1 deletion app/src/main/java/com/example/myapplication/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,29 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.example.myapplication.model.Body
import com.example.myapplication.model.LectureResult
import com.example.myapplication.model.StatusCode
import com.example.myapplication.repository.Repository
import kotlinx.coroutines.launch
import retrofit2.Response

class MainViewModel (private val repository: Repository): ViewModel(){
val myResponse:MutableLiveData<Response<StatusCode>> = MutableLiveData()
val lectureList:MutableLiveData<Response<LectureResult>> = MutableLiveData()

fun getStatusCode(){
viewModelScope.launch {
val response = repository.getStatusCode()
myResponse.value = response
}
}

fun getHangangList(limit:Int,page:Int){
viewModelScope.launch {
val response = repository.getHangangList(limit,page)
lectureList.value = response
}
}
}

class MainViewModelFactory(private val repository: Repository):ViewModelProvider.Factory{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,56 @@
package com.example.myapplication.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.GoActivity
import com.example.myapplication.R
import com.example.myapplication.model.Lecture

class HanGangAdapter(val goActivity: GoActivity) : ListAdapter<Lecture, HanGangAdapter.LectureHolder>(LectureComparator()) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LectureHolder {
return LectureHolder.create(parent)
}

override fun onBindViewHolder(holder: LectureHolder, position: Int) {
val current = getItem(position)
holder.bind(current.subjectName.toString(),current.professor.toString())
holder.root.setOnClickListener{
goActivity.goActivity(current.id)
}
}

class LectureHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val lectureName: TextView = itemView.findViewById(R.id.lecture_item_name)
private val professorName: TextView = itemView.findViewById(R.id.professor_item)
val root:ConstraintLayout = itemView.findViewById(R.id.root)
fun bind(lecture: String?, professor:String?) {
lectureName.text = lecture
professorName.text = professor
}

companion object {
fun create(parent: ViewGroup): LectureHolder {
val view: View = LayoutInflater.from(parent.context)
.inflate(R.layout.lecture_item, parent, false)
return LectureHolder(view)
}
}
}

class LectureComparator : DiffUtil.ItemCallback<Lecture>() {
override fun areItemsTheSame(oldItem: Lecture, newItem: Lecture): Boolean {
return oldItem === newItem
}

override fun areContentsTheSame(oldItem: Lecture, newItem: Lecture): Boolean {
return oldItem.id == newItem.id
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,71 @@
package com.example.myapplication.api

import com.example.myapplication.util.Constants.Companion.BASE_URL
import com.example.myapplication.util.Constants.Companion.HANGANG_URL
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.security.SecureRandom
import javax.net.ssl.SSLContext
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager

object RetrofitInstance {

private val retrofit by lazy{
private val retrofit by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}

val api:SimpleApi by lazy {
private val hangangInstance by lazy {
Retrofit.Builder()
.baseUrl(HANGANG_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(unsafeOkHttpClient().build())
.build()
}

val api: SimpleApi by lazy {
retrofit.create(SimpleApi::class.java)
}

val hangangApi by lazy{
hangangInstance.create(SimpleApi::class.java)
}

private fun unsafeOkHttpClient(): OkHttpClient.Builder {
val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
override fun checkClientTrusted(
chain: Array<out java.security.cert.X509Certificate>?,
authType: String?
) {

}

override fun checkServerTrusted(
chain: Array<out java.security.cert.X509Certificate>?,
authType: String?
) {

}

override fun getAcceptedIssuers(): Array<out java.security.cert.X509Certificate>? {
return arrayOf()
}
})

val sslContext = SSLContext.getInstance("SSL")
sslContext.init(null, trustAllCerts, SecureRandom())

val sslSocketFactory = sslContext.socketFactory

val builder = OkHttpClient.Builder()
builder.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
builder.hostnameVerifier { hostname, session -> true }

return builder

}
}
15 changes: 15 additions & 0 deletions app/src/main/java/com/example/myapplication/model/Lecture.kt
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
package com.example.myapplication.model

import com.google.gson.annotations.SerializedName

data class LectureResult(
@SerializedName("result")
var resultList:List<Lecture>? = null
)

class Lecture{
@SerializedName("id")
var id:Int? = null
@SerializedName("name")
var subjectName:String? = null
@SerializedName("professor")
var professor:String? = null
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.example.myapplication.repository

import com.example.myapplication.api.RetrofitInstance
import com.example.myapplication.model.Body
import com.example.myapplication.model.LectureResult
import com.example.myapplication.model.StatusCode
import retrofit2.Response

class Repository {
suspend fun getStatusCode(): Response<StatusCode> {
return RetrofitInstance.api.getStatusCode()
}

suspend fun getHangangList(limit:Int,page:Int): Response<LectureResult>{
return RetrofitInstance.hangangApi.getHangangList(limit,page)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package com.example.myapplication.util
class Constants{
companion object{
const val BASE_URL = "https://jhi7xevof6.execute-api.ap-northeast-2.amazonaws.com"
const val HANGANG_URL = "https://stage.api.hangang.in/lectures/"
const val HANGANG_URL = "https://stage.api.hangang.in/"
}
}
31 changes: 25 additions & 6 deletions app/src/main/res/layout/activity_lecture_details.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LectureDetailsActivity">
xmlns:tools="http://schemas.android.com/tools">

</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LectureDetailsActivity">

<TextView
android:id="@+id/lecture_details_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="제목"
android:textSize="20sp"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.123" />

</androidx.constraintlayout.widget.ConstraintLayout>

</layout>
Loading

0 comments on commit 50699d9

Please sign in to comment.