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

Implement history UI #6

Merged
merged 11 commits into from Oct 11, 2020
2 changes: 1 addition & 1 deletion app/build.gradle
Expand Up @@ -56,7 +56,7 @@ detekt {
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
Expand Down
70 changes: 63 additions & 7 deletions app/src/main/java/com/example/weightcontrolapp/MainActivity.kt
@@ -1,22 +1,30 @@
package com.example.weightcontrolapp

import android.app.Activity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GravityCompat
import androidx.fragment.app.Fragment
import androidx.room.Room
import com.example.weightcontrolapp.data.local.db.AppDatabase
import com.example.weightcontrolapp.ui.history.HistoryFragment
import com.example.weightcontrolapp.ui.weightrecord.WeightRecordFragment
import com.google.android.material.navigation.NavigationView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
companion object {
lateinit var db: AppDatabase
}


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(findViewById(R.id.toolbar))
setActionBar()
setDrawerLayout()

db = Room.databaseBuilder(
applicationContext,
Expand All @@ -25,17 +33,65 @@ class MainActivity : AppCompatActivity() {
).fallbackToDestructiveMigration().build()
}

private fun setActionBar() {
setSupportActionBar(toolbar)
supportActionBar?.setDisplayShowHomeEnabled(false)
}

private fun setDrawerLayout() {
val toggle = ActionBarDrawerToggle(
Activity(), drawer_layout, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close
)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The action hamburger icon rotates is useless. Remove this.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If remove this, I don't know why the icon also is removed... I leave as It is.

navigation_view.setNavigationItemSelectedListener(this)
}

override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}

override fun onNavigationItemSelected(item: MenuItem): Boolean {
var fragment: Fragment? = null

when (item.itemId) {
R.id.menu_nav_top, R.id.menu_nav_my_page -> {
return true
}
R.id.menu_nav_weight_record -> {
fragment = WeightRecordFragment()
}
R.id.menu_nav_history -> {
fragment = HistoryFragment()
}
}

if (fragment != null) {
val frag = supportFragmentManager.beginTransaction()
frag.replace(R.id.content, fragment)
frag.commit()
}

drawer_layout.closeDrawer(GravityCompat.START)
return true
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_top -> true
R.id.action_my_page -> true
R.id.action_register -> true
R.id.action_history -> true
R.id.menu_nav_top -> true
R.id.menu_nav_my_page -> true
R.id.menu_nav_weight_record -> true
R.id.menu_nav_history -> true
else -> super.onOptionsItemSelected(item)
}
}
Expand Down
Expand Up @@ -20,5 +20,5 @@ interface WeightDataDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertWeightData(weightData: WeightData)

//TODO: Delete
// delete
}
@@ -0,0 +1,22 @@
package com.example.weightcontrolapp.ui.history

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.weightcontrolapp.R

class HistoryFragment : Fragment() {

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
super.onCreateView(inflater, container, savedInstanceState)

return inflater.inflate(R.layout.history_fragment, container, false)
}

}
Expand Up @@ -15,7 +15,7 @@ class WeightRecordViewModel : ViewModel() {

private val dao = MainActivity.db.weightDataDao()

//TODO: use when implement history screen
// use when implement history screen
fun loadAllWeightDataList(): List<WeightData> {
var weightDataList = listOf<WeightData>()
CoroutineScope(Dispatchers.Main).launch {
Expand All @@ -26,7 +26,7 @@ class WeightRecordViewModel : ViewModel() {
return weightDataList
}

//TODO: use when implement history screen
// use when implement history screen
fun findWeightDataById(id: Int): WeightData {
lateinit var weightData: WeightData
CoroutineScope(Dispatchers.Main).launch {
Expand Down
47 changes: 35 additions & 12 deletions app/src/main/res/layout/activity_main.xml
@@ -1,25 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">

<com.google.android.material.appbar.AppBarLayout
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
android:layout_height="match_parent">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

</com.google.android.material.appbar.AppBarLayout>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

<include layout="@layout/content_main" />
</com.google.android.material.appbar.AppBarLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

</androidx.constraintlayout.widget.ConstraintLayout>
Comment on lines +29 to +35
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may replace this with FrameLayout.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel like doing this.


</androidx.coordinatorlayout.widget.CoordinatorLayout>

<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/menu_navigation" />

</androidx.drawerlayout.widget.DrawerLayout>
19 changes: 19 additions & 0 deletions app/src/main/res/layout/history_fragment.xml
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/cardview_dark_background"
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use more bright colors.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't decide the more appropriate colour.

android:gravity="center"
android:text="@string/history_fragment_title"
android:textColor="@android:color/white"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
25 changes: 25 additions & 0 deletions app/src/main/res/layout/nav_header.xml
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="192dp"
android:background="?attr/colorPrimaryDark"
android:gravity="bottom"
android:orientation="vertical"
android:padding="16dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

<ImageView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@mipmap/ic_launcher" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:text="@string/app_name"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"/>

</FrameLayout>
17 changes: 17 additions & 0 deletions app/src/main/res/menu/menu_navigation.xml
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/menu_nav_top"
android:title="@string/action_top" />
<item
android:id="@+id/menu_nav_my_page"
android:title="@string/action_my_page" />
<item
android:id="@+id/menu_nav_weight_record"
android:title="@string/action_record" />
<item
android:id="@+id/menu_nav_history"
android:title="@string/action_history" />

</menu>
17 changes: 14 additions & 3 deletions app/src/main/res/navigation/nav_graph.xml
Expand Up @@ -3,17 +3,21 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/WeightRecordFragment">
app:startDestination="@id/menu_nav_weight_record">

<fragment
android:id="@+id/WeightRecordFragment"
android:id="@+id/menu_nav_weight_record"
android:name="com.example.weightcontrolapp.ui.weightrecord.WeightRecordFragment"
android:label="@string/weight_record_fragment_label"
tools:layout="@layout/weight_record_fragment">

<action
android:id="@+id/action_WeightRecordFragment_to_CheckWeightRecordFragment"
app:destination="@id/CheckWeightRecordFragment" />
<action
android:id="@+id/action_WeightRecordFragment_to_HistoryFragment"
app:destination="@id/menu_nav_history"
/>
</fragment>

<fragment
Expand All @@ -24,6 +28,13 @@

<action
android:id="@+id/action_CheckWeightRecordFragment_to_WeightRecordFragment"
app:destination="@id/WeightRecordFragment" />
app:destination="@id/menu_nav_weight_record" />
</fragment>

<fragment
android:id="@+id/menu_nav_history"
android:name="com.example.weightcontrolapp.ui.history.HistoryFragment"
android:label="@string/history_fragment_label">

</fragment>
</navigation>
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -14,6 +14,9 @@
<string name="weight_record_fragment_label">Weight Record Fragment</string>
<string name="check_weight_record_fragment_label">Check Weight Record Fragment</string>
<string name="back_to_weight_record_screen">Back to weight record screen</string>
<string name="history_fragment_label">History Fragment</string>
<string name="navigation_drawer_open">Navigation Drawer Open</string>
<string name="navigation_drawer_close">Navigation Drawer Close</string>

<!-- Strings for WeightRecordFragment -->
<string name="record_your_weight">Let\'s record \n today\'s your weight!</string>
Expand All @@ -22,4 +25,7 @@

<!-- String for CheckWeightRecordFragment -->
<string name="record_weight_message">Today\'s your weight is \n %1$s kg!</string>

<!-- HistoryFragment -->
<string name="history_fragment_title">History</string>
</resources>