Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sam #2

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
2 changes: 0 additions & 2 deletions README.md

This file was deleted.

This file was deleted.

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

6 changes: 6 additions & 0 deletions week1/udemy_android_template_bottomnavi-main/.idea/vcs.xml

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

Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}

android {
compileSdk 31

viewBinding {
viewBinding{
enabled true
}

Expand All @@ -33,6 +34,7 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
namespace 'com.example.flo'
}

dependencies {
Expand All @@ -41,7 +43,26 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
implementation 'com.google.code.gson:gson:2.8.7' //GSON
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

// Room DB 라이브러리
implementation 'androidx.room:room-ktx:2.4.1'
implementation 'androidx.room:room-runtime:2.4.1'
kapt 'androidx.room:room-compiler:2.4.1'

//Retrofit
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.retrofit2:converter-gson:2.9.0"
implementation "com.squareup.retrofit2:adapter-rxjava2:2.9.0"

//okHttp
implementation "com.squareup.okhttp3:okhttp:4.9.0"
implementation "com.squareup.okhttp3:logging-interceptor:4.9.0"

//Glide
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.flo">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:allowBackup="true"
Expand All @@ -9,15 +8,23 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.FLO">

<activity
android:name=".MainActivity"
android:exported="true">
android:exported="true"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.flo

import androidx.room.Entity
import androidx.room.PrimaryKey
import java.util.*


@Entity(tableName = "AlbumTable")
data class Album(
@PrimaryKey(autoGenerate = false) var id: Int = 0,
var title: String? = "",
var singer: String? = "",
var coverImg: Int? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.flo

import androidx.room.*


@Dao
interface AlbumDao {
@Insert
fun insert(album: Album)

@Update
fun update(album: Album)

@Delete
fun delete(album: Album)

@Query("SELECT * FROM AlbumTable") // 테이블의 모든 값을 가져와라
fun getAlbums(): List<Album>

@Query("SELECT * FROM AlbumTable WHERE id = :id")
fun getAlbum(id: Int): Album

@Insert
fun likeAlbum(like: Like)

@Query("DELETE FROM LikeTable WHERE userId = :userId AND albumId = :albumId")
fun disLikeAlbum(userId: Int, albumId: Int)

@Query("SELECT id FROM LikeTable WHERE userId = :userId AND albumId = :albumId")
fun isLikedAlbum(userId: Int, albumId: Int): Int?

@Query("SELECT AT.* FROM LikeTable as LT LEFT JOIN AlbumTable as AT ON LT.albumId = AT.id WHERE LT.userId = :userId")
fun getLikedAlbums(userId: Int): List<Album>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.example.flo

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.example.flo.databinding.FragmentAlbumBinding
import com.google.android.material.tabs.TabLayoutMediator
import com.google.gson.Gson
import androidx.viewpager2.widget.ViewPager2

class AlbumFragment : Fragment() {
private lateinit var binding: FragmentAlbumBinding
private val information = arrayListOf("수록곡", "상세정보", "영상")

private var isLiked: Boolean = false

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentAlbumBinding.inflate(inflater, container, false)

val albumData = arguments?.getString("album")
val gson = Gson()

val album = gson.fromJson(albumData, Album::class.java)
isLiked = isLikedAlbum(album.id)

setViews(album)
initViewPager()
setClickListeners(album)


return binding.root
}

private fun setViews(album: Album) {
binding.albumMusicTitleTv.text = album.title.toString()
binding.albumSingerNameTv.text = album.singer.toString()
binding.albumAlbumIv.setImageResource(album.coverImg!!)

if(isLiked) {
binding.albumLikeIv.setImageResource(R.drawable.ic_my_like_on)
} else {
binding.albumLikeIv.setImageResource(R.drawable.ic_my_like_off)
}
}

private fun setClickListeners(album: Album) {
val userId: Int = getJwt()

binding.albumLikeIv.setOnClickListener {
if(isLiked) {
binding.albumLikeIv.setImageResource(R.drawable.ic_my_like_off)
disLikeAlbum(userId, album.id)
} else {
binding.albumLikeIv.setImageResource(R.drawable.ic_my_like_on)
likeAlbum(userId, album.id)
}

isLiked = !isLiked
}

//set click listener
binding.albumBackIv.setOnClickListener {
(context as MainActivity).supportFragmentManager.beginTransaction()
.replace(R.id.main_frm, HomeFragment())
.commitAllowingStateLoss()
}
}

private fun initViewPager() {
//init viewpager
val albumAdapter = AlbumVPAdapter(this)

binding.albumContentVp.adapter = albumAdapter
TabLayoutMediator(binding.albumContentTb, binding.albumContentVp) { tab, position ->
tab.text = information[position]
}.attach()
}

private fun disLikeAlbum(userId: Int, albumId: Int) {
val songDB = SongDatabase.getInstance(requireContext())!!
songDB.albumDao().disLikeAlbum(userId, albumId)
}

private fun likeAlbum(userId: Int, albumId: Int) {
val songDB = SongDatabase.getInstance(requireContext())!!
val like = Like(userId, albumId)

songDB.albumDao().likeAlbum(like)
}


private fun isLikedAlbum(albumId: Int): Boolean {
val songDB = SongDatabase.getInstance(requireContext())!!
val userId = getJwt()

val likeId: Int? = songDB.albumDao().isLikedAlbum(userId, albumId)

return likeId != null
}

private fun getJwt(): Int {
val spf = activity?.getSharedPreferences("auth", AppCompatActivity.MODE_PRIVATE)
val jwt = spf!!.getInt("jwt", 0)
Log.d("MAIN_ACT/GET_JWT", "jwt_token: $jwt")

return jwt
}
}
Loading