Skip to content

Commit 4d68620

Browse files
committed
feat: Transition to DetailBookActivity.
1 parent dce7bc4 commit 4d68620

17 files changed

+292
-27
lines changed

.idea/jarRepositories.xml

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/kotlinc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

+7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ android {
3535
}
3636
}
3737

38+
repositories {
39+
jcenter()
40+
maven {
41+
url "https://kotlin.bintray.com/kotlinx"
42+
}
43+
}
44+
3845
dependencies {
3946

4047
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

app/src/main/AndroidManifest.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
android:roundIcon="@mipmap/ic_launcher_round"
1111
android:supportsRtl="true"
1212
android:theme="@style/Theme.Bookshelf">
13-
<activity android:name=".activities.CreateBookActivity"></activity>
13+
<activity android:name=".activities.DetailBookActivity"></activity>
14+
<activity android:name=".activities.CreateBookActivity" />
1415
<activity android:name=".activities.BookListActivity">
1516
<intent-filter>
1617
<action android:name="android.intent.action.MAIN" />

app/src/main/java/dev/fummicc1/lit/bookshelf/activities/BookListActivity.kt

+15-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package dev.fummicc1.lit.bookshelf.activities
33
import android.content.Intent
44
import androidx.appcompat.app.AppCompatActivity
55
import android.os.Bundle
6+
import android.view.View
67
import androidx.activity.viewModels
7-
import androidx.lifecycle.Observer
88
import androidx.recyclerview.widget.GridLayoutManager
9-
import androidx.recyclerview.widget.LinearLayoutManager
109
import dev.fummicc1.lit.bookshelf.R
10+
import dev.fummicc1.lit.bookshelf.datas.Book
1111
import dev.fummicc1.lit.bookshelf.viewmodels.BookListViewModel
1212
import dev.fummicc1.lit.bookshelf.views.BookListAdapter
1313
import kotlinx.android.synthetic.main.activity_book_list.*
@@ -20,8 +20,16 @@ class BookListActivity : AppCompatActivity() {
2020
super.onCreate(savedInstanceState)
2121
setContentView(R.layout.activity_book_list)
2222

23+
val adapterListener = object : BookListAdapter.OnItemClickListener {
24+
override fun onItemClick(item: Book) {
25+
val intent = Intent(this@BookListActivity, DetailBookActivity::class.java)
26+
intent.putExtra("detail_book", item)
27+
startActivity(intent)
28+
}
29+
}
30+
2331
// RecyclerViewの設定
24-
val adapter = BookListAdapter(this, viewModel.bookList)
32+
val adapter = BookListAdapter(this, viewModel.bookList, adapterListener)
2533
// GridLayoutManager...横と縦の両方にアイテムを並べる
2634
// LinearLayoutManager...横いっぱいにして縦にアイテムを並べる
2735
recyclerView.layoutManager = GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false)
@@ -31,5 +39,9 @@ class BookListActivity : AppCompatActivity() {
3139
val intent = Intent(this, CreateBookActivity::class.java)
3240
startActivity(intent)
3341
}
42+
43+
// エンプティステートの切り替え(RealmAdapterだと初期化以降に判断が難しいのでRoomに移行したら追加実装する)
44+
// val isEmpty = viewModel.bookList.isEmpty()
45+
// emptyStateTextView.visibility = if (isEmpty) View.VISIBLE else View.INVISIBLE
3446
}
3547
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package dev.fummicc1.lit.bookshelf.activities
2+
3+
import androidx.appcompat.app.AppCompatActivity
4+
import android.os.Bundle
5+
import androidx.activity.viewModels
6+
import dev.fummicc1.lit.bookshelf.R
7+
import dev.fummicc1.lit.bookshelf.datas.Book
8+
import dev.fummicc1.lit.bookshelf.viewmodels.DetailBookViewModel
9+
import kotlinx.android.synthetic.main.activity_detail_book.*
10+
11+
class DetailBookActivity : AppCompatActivity() {
12+
13+
private val viewModel: DetailBookViewModel by viewModels()
14+
15+
override fun onCreate(savedInstanceState: Bundle?) {
16+
super.onCreate(savedInstanceState)
17+
setContentView(R.layout.activity_detail_book)
18+
19+
20+
21+
val book = intent.getParcelableExtra("detail_book") as? Book
22+
23+
if (book == null) {
24+
return
25+
}
26+
27+
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,55 @@
11
package dev.fummicc1.lit.bookshelf.datas
22

3+
import android.os.Parcel
4+
import android.os.Parcelable
35
import io.realm.RealmObject
6+
import io.realm.annotations.PrimaryKey
7+
import kotlinx.android.parcel.Parcelize
8+
import java.text.DateFormat
49
import java.util.*
510

611
// Open修飾子をつける必要がある
712
// 変数にデフォルトの値を設定する必要がある
813
// RealmObjectを継承する必要がある→Data Classは使えない
9-
open class Book(
14+
open class Book (
15+
@PrimaryKey
16+
var id: String = UUID.randomUUID().toString(),
1017
var title: String = "",
1118
var author: String = "",
1219
var price: Int = 0,
1320
var description: String = "",
1421
var createdAt: Date = Date()
15-
): RealmObject()
22+
): RealmObject(), Parcelable {
23+
constructor(parcel: Parcel) : this(
24+
parcel.readString() ?: "",
25+
parcel.readString() ?: "",
26+
parcel.readString() ?: "",
27+
parcel.readInt() ?: 0,
28+
parcel.readString() ?: "",
29+
DateFormat.getDateInstance().parse(parcel.readString() ?: "") ?: Date()
30+
) {
31+
}
32+
33+
override fun describeContents(): Int = 0
34+
35+
override fun writeToParcel(dest: Parcel?, flags: Int) {
36+
dest?.writeString(id)
37+
dest?.writeString(title)
38+
dest?.writeString(author)
39+
dest?.writeInt(price)
40+
dest?.writeString(description)
41+
42+
val createdAtText = DateFormat.getDateInstance().format(createdAt)
43+
dest?.writeString(createdAtText)
44+
}
45+
46+
companion object CREATOR : Parcelable.Creator<Book> {
47+
override fun createFromParcel(parcel: Parcel): Book {
48+
return Book(parcel)
49+
}
50+
51+
override fun newArray(size: Int): Array<Book?> {
52+
return arrayOfNulls(size)
53+
}
54+
}
55+
}

app/src/main/java/dev/fummicc1/lit/bookshelf/viewmodels/CreateBookViewModel.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import androidx.lifecycle.MutableLiveData
55
import androidx.lifecycle.ViewModel
66
import dev.fummicc1.lit.bookshelf.datas.Book
77
import io.realm.Realm
8+
import java.util.*
89

910
class CreateBookViewModel: ViewModel() {
1011

@@ -71,7 +72,7 @@ class CreateBookViewModel: ViewModel() {
7172
checkInputAndShowError(description.value, InputField.DESCRIPTION)
7273
) {
7374
realm.executeTransactionAsync {
74-
val book = it.createObject(Book::class.java)
75+
val book = it.createObject(Book::class.java, UUID.randomUUID().toString())
7576
book.title = title.value!!
7677
book.author = author.value!!
7778
book.price = price.value!!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package dev.fummicc1.lit.bookshelf.viewmodels
2+
3+
import androidx.lifecycle.MutableLiveData
4+
import androidx.lifecycle.ViewModel
5+
import dev.fummicc1.lit.bookshelf.datas.Book
6+
import io.realm.Realm
7+
8+
class DetailBookViewModel(book: Book): ViewModel() {
9+
10+
private val realm: Realm
11+
12+
private val _title: MutableLiveData<String> = MutableLiveData()
13+
private val _description: MutableLiveData<String> = MutableLiveData()
14+
private val _content: MutableLiveData<String> = MutableLiveData()
15+
private val _price: MutableLiveData<Int> = MutableLiveData()
16+
17+
init {
18+
realm = Realm.getDefaultInstance()
19+
}
20+
21+
fun delete(book: Book) {
22+
realm.executeTransactionAsync {
23+
it.where(Book::class.java)
24+
.equalTo("id", book.id)
25+
.findAll()
26+
.apply {
27+
this.deleteAllFromRealm()
28+
}
29+
}
30+
}
31+
}

app/src/main/java/dev/fummicc1/lit/bookshelf/views/BookListAdapter.kt

+14-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ import java.time.LocalDateTime
1818
import java.time.ZoneId
1919
import java.time.temporal.ChronoUnit
2020

21-
class BookListAdapter(val context: Context, val bookList: RealmResults<Book>): RealmRecyclerViewAdapter<Book, BookListAdapter.ViewHolder>(bookList, true) {
21+
class BookListAdapter(
22+
val context: Context,
23+
val bookList: RealmResults<Book>,
24+
val listener: OnItemClickListener
25+
): RealmRecyclerViewAdapter<Book, BookListAdapter.ViewHolder>(bookList, true) {
2226

23-
class ViewHolder(view: View): RecyclerView.ViewHolder(view) {
27+
class ViewHolder(val view: View): RecyclerView.ViewHolder(view) {
2428
val titleTextView = view.titleTextView
2529
val authorTextView = view.authorTextView
2630
val timeTextView = view.timeTextView
@@ -54,5 +58,13 @@ class BookListAdapter(val context: Context, val bookList: RealmResults<Book>): R
5458
timeText = DateFormat.getDateInstance().format(book.createdAt)
5559
}
5660
holder.timeTextView.text = timeText
61+
62+
holder.view.setOnClickListener {
63+
listener.onItemClick(book)
64+
}
65+
}
66+
67+
interface OnItemClickListener {
68+
fun onItemClick(item: Book)
5769
}
5870
}

app/src/main/res/drawable/ic_baseline_menu_book_24.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<vector android:height="120dp" android:tint="?attr/colorControlNormal"
1+
<vector android:height="160dp" android:tint="?attr/colorControlNormal"
22
android:viewportHeight="24" android:viewportWidth="24"
3-
android:width="120dp" xmlns:android="http://schemas.android.com/apk/res/android">
3+
android:width="160dp" xmlns:android="http://schemas.android.com/apk/res/android">
44
<path android:fillColor="@android:color/white" android:pathData="M21,5c-1.11,-0.35 -2.33,-0.5 -3.5,-0.5c-1.95,0 -4.05,0.4 -5.5,1.5c-1.45,-1.1 -3.55,-1.5 -5.5,-1.5S2.45,4.9 1,6v14.65c0,0.25 0.25,0.5 0.5,0.5c0.1,0 0.15,-0.05 0.25,-0.05C3.1,20.45 5.05,20 6.5,20c1.95,0 4.05,0.4 5.5,1.5c1.35,-0.85 3.8,-1.5 5.5,-1.5c1.65,0 3.35,0.3 4.75,1.05c0.1,0.05 0.15,0.05 0.25,0.05c0.25,0 0.5,-0.25 0.5,-0.5V6C22.4,5.55 21.75,5.25 21,5zM21,18.5c-1.1,-0.35 -2.3,-0.5 -3.5,-0.5c-1.7,0 -4.15,0.65 -5.5,1.5V8c1.35,-0.85 3.8,-1.5 5.5,-1.5c1.2,0 2.4,0.15 3.5,0.5V18.5z"/>
55
<path android:fillColor="@android:color/white" android:pathData="M17.5,10.5c0.88,0 1.73,0.09 2.5,0.26V9.24C19.21,9.09 18.36,9 17.5,9c-1.7,0 -3.24,0.29 -4.5,0.83v1.66C14.13,10.85 15.7,10.5 17.5,10.5z"/>
66
<path android:fillColor="@android:color/white" android:pathData="M13,12.49v1.66c1.13,-0.64 2.7,-0.99 4.5,-0.99c0.88,0 1.73,0.09 2.5,0.26V11.9c-0.79,-0.15 -1.64,-0.24 -2.5,-0.24C15.8,11.66 14.26,11.96 13,12.49z"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android">
3+
<corners
4+
android:radius="32dp"/>
5+
</shape>

app/src/main/res/layout/activity_book_list.xml

+14-5
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@
2121

2222
<androidx.recyclerview.widget.RecyclerView
2323
android:id="@+id/recyclerView"
24-
android:layout_width="match_parent"
25-
android:layout_height="match_parent"
24+
android:layout_width="0dp"
25+
android:layout_height="0dp"
2626
app:layout_constraintBottom_toBottomOf="parent"
2727
app:layout_constraintEnd_toEndOf="parent"
28-
app:layout_constraintHorizontal_bias="0.0"
2928
app:layout_constraintStart_toStartOf="parent"
30-
app:layout_constraintTop_toTopOf="parent"
31-
app:layout_constraintVertical_bias="1.0" />
29+
app:layout_constraintTop_toBottomOf="@+id/bookListToolBar" />
3230

3331
<com.google.android.material.floatingactionbutton.FloatingActionButton
3432
android:id="@+id/moveToCreateBookButton"
@@ -42,4 +40,15 @@
4240
app:layout_constraintEnd_toEndOf="parent"
4341
app:srcCompat="@drawable/ic_baseline_add_24" />
4442

43+
<androidx.appcompat.widget.Toolbar
44+
android:id="@+id/bookListToolBar"
45+
android:layout_width="match_parent"
46+
android:layout_height="wrap_content"
47+
android:minHeight="?attr/actionBarSize"
48+
android:theme="@style/MyToolBar"
49+
app:layout_constraintEnd_toEndOf="parent"
50+
app:layout_constraintStart_toStartOf="parent"
51+
app:layout_constraintTop_toTopOf="parent"
52+
app:title="BookShelf" />
53+
4554
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/layout/activity_create_book.xml

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
android:layout_width="match_parent"
1313
android:layout_height="wrap_content"
1414
android:minHeight="?attr/actionBarSize"
15-
android:theme="@style/MyActionBar"
15+
android:theme="@style/MyToolBar"
1616
app:layout_constraintEnd_toEndOf="parent"
1717
app:layout_constraintStart_toStartOf="parent"
1818
app:layout_constraintTop_toTopOf="parent"
@@ -39,13 +39,12 @@
3939

4040
<ImageView
4141
android:id="@+id/imageView"
42-
android:layout_width="0dp"
42+
android:layout_width="wrap_content"
4343
android:layout_height="wrap_content"
4444
android:layout_marginStart="32dp"
4545
android:layout_marginEnd="32dp"
46-
android:scaleType="center"
46+
android:scaleType="fitCenter"
4747
app:layout_constraintEnd_toEndOf="parent"
48-
app:layout_constraintHorizontal_bias="0.49"
4948
app:layout_constraintStart_toStartOf="parent"
5049
app:layout_constraintTop_toBottomOf="@+id/createBookToolBar"
5150
app:srcCompat="@drawable/ic_baseline_menu_book_24" />

0 commit comments

Comments
 (0)