Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit a942879

Browse files
author
Evgenii Kanivets
committed
#95. Partly move logic from EditAccountActivity to EditAccountFragment.
1 parent bba8d27 commit a942879

File tree

4 files changed

+182
-110
lines changed

4 files changed

+182
-110
lines changed

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/account/edit/EditAccountActivity.kt

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ class EditAccountActivity : BaseBackActivity() {
3838
override fun initViews() {
3939
super.initViews()
4040

41-
etTitle.setText(account.title)
42-
etGoal.setText(account.goal.toString())
43-
viewColor.setBackgroundColor(account.color)
44-
45-
fabDone.setOnClickListener { done() }
41+
fabDone.setOnClickListener { /*done()*/ }
4642
}
4743

4844
override fun onCreateOptionsMenu(menu: Menu): Boolean {
@@ -63,21 +59,15 @@ class EditAccountActivity : BaseBackActivity() {
6359
}
6460
}
6561

66-
private fun done() {
67-
val title = etTitle.text.toString().trim { it <= ' ' }
68-
69-
if (title.isEmpty()) {
70-
tilTitle.error = getString(R.string.field_cant_be_empty)
71-
} else {
72-
val newAccount = Account(
73-
account.id, title, account.curSum.toDouble(),
74-
account.currency, account.goal, account.isArchived, account.color
75-
)
76-
val updated = accountController.update(newAccount) != null
77-
if (updated) {
78-
setResult(Activity.RESULT_OK)
79-
finish()
80-
}
62+
private fun done(title: String) {
63+
val newAccount = Account(
64+
account.id, title, account.curSum.toDouble(),
65+
account.currency, account.goal, account.isArchived, account.color
66+
)
67+
val updated = accountController.update(newAccount) != null
68+
if (updated) {
69+
setResult(Activity.RESULT_OK)
70+
finish()
8171
}
8272
}
8373

@@ -118,7 +108,7 @@ class EditAccountActivity : BaseBackActivity() {
118108

119109
companion object {
120110

121-
const val KEY_ACCOUNT = "key_account"
111+
private const val KEY_ACCOUNT = "key_account"
122112

123113
fun newIntent(context: Context, account: Account): Intent {
124114
val intent = Intent(context, EditAccountActivity::class.java)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.blogspot.e_kanivets.moneytracker.activity.account.edit.fragment
2+
3+
import android.os.Bundle
4+
import android.support.v4.app.Fragment
5+
import android.view.LayoutInflater
6+
import android.view.View
7+
import android.view.ViewGroup
8+
import com.blogspot.e_kanivets.moneytracker.R
9+
import com.blogspot.e_kanivets.moneytracker.entity.data.Account
10+
import kotlinx.android.synthetic.main.fragment_edit_account.*
11+
12+
class EditAccountFragment() : Fragment() {
13+
14+
private lateinit var account: Account
15+
16+
override fun onCreate(savedInstanceState: Bundle?) {
17+
super.onCreate(savedInstanceState)
18+
arguments?.let { arguments -> account = arguments.getParcelable(KEY_ACCOUNT) }
19+
}
20+
21+
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
22+
val rootView = inflater.inflate(R.layout.fragment_edit_account, container, false)
23+
initViews()
24+
return rootView
25+
}
26+
27+
fun initViews() {
28+
etTitle.setText(account.title)
29+
etGoal.setText(account.goal.toString())
30+
viewColor.setBackgroundColor(account.color)
31+
}
32+
33+
private fun done() {
34+
val title = etTitle.text.toString().trim { it <= ' ' }
35+
36+
if (title.isEmpty()) {
37+
tilTitle.error = getString(R.string.field_cant_be_empty)
38+
} else {
39+
val newAccount = Account(
40+
account.id, title, account.curSum.toDouble(),
41+
account.currency, account.goal, account.isArchived, account.color
42+
)
43+
}
44+
}
45+
46+
companion object {
47+
48+
private const val KEY_ACCOUNT = "key_account"
49+
50+
fun newInstance(account: Account): EditAccountFragment {
51+
val fragment = EditAccountFragment()
52+
val arguments = Bundle()
53+
arguments.putParcelable(KEY_ACCOUNT, account)
54+
fragment.arguments = arguments
55+
return fragment
56+
}
57+
58+
}
59+
60+
}

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

Lines changed: 32 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -6,97 +6,40 @@
66
android:layout_width="match_parent"
77
android:layout_height="match_parent"
88
android:fitsSystemWindows="true"
9-
tools:context="com.blogspot.e_kanivets.moneytracker.activity.account.edit.EditAccountActivity">
9+
tools:context="com.blogspot.e_kanivets.moneytracker.activity.account.edit.EditAccountActivity"
10+
>
1011

11-
<android.support.design.widget.AppBarLayout
12-
android:layout_width="match_parent"
13-
android:layout_height="wrap_content"
14-
android:theme="@style/AppTheme.AppBarOverlay">
15-
16-
<android.support.v7.widget.Toolbar
17-
android:id="@+id/toolbar"
18-
android:layout_width="match_parent"
19-
android:layout_height="?attr/actionBarSize"
20-
android:background="?attr/colorPrimary"
21-
app:popupTheme="@style/AppTheme.PopupOverlay" />
12+
<android.support.design.widget.AppBarLayout
13+
android:layout_width="match_parent"
14+
android:layout_height="wrap_content"
15+
android:theme="@style/AppTheme.AppBarOverlay"
16+
>
2217

23-
</android.support.design.widget.AppBarLayout>
24-
25-
<LinearLayout
18+
<android.support.v7.widget.Toolbar
19+
android:id="@+id/toolbar"
2620
android:layout_width="match_parent"
27-
android:layout_height="match_parent"
28-
android:orientation="vertical"
29-
android:padding="@dimen/activity_horizontal_margin"
30-
app:layout_behavior="@string/appbar_scrolling_view_behavior">
31-
32-
<android.support.design.widget.TextInputLayout
33-
android:id="@+id/tilTitle"
34-
android:layout_width="match_parent"
35-
android:layout_height="wrap_content">
36-
37-
<EditText
38-
android:id="@+id/etTitle"
39-
android:layout_width="match_parent"
40-
android:layout_height="wrap_content"
41-
android:hint="@string/title"
42-
android:inputType="text"
43-
android:maxLines="1"
44-
android:singleLine="true" />
45-
</android.support.design.widget.TextInputLayout>
46-
47-
<android.support.design.widget.TextInputLayout
48-
android:id="@+id/til_goal"
49-
android:layout_width="match_parent"
50-
android:layout_height="wrap_content"
51-
android:visibility="invisible">
52-
53-
<EditText
54-
android:id="@+id/etGoal"
55-
android:layout_width="match_parent"
56-
android:layout_height="wrap_content"
57-
android:hint="@string/goal"
58-
android:inputType="numberDecimal"
59-
android:maxLines="1"
60-
android:singleLine="true" />
61-
</android.support.design.widget.TextInputLayout>
62-
63-
<LinearLayout
64-
android:layout_width="match_parent"
65-
android:layout_height="wrap_content"
66-
android:orientation="vertical"
67-
android:visibility="invisible">
68-
69-
<TextView
70-
android:layout_width="wrap_content"
71-
android:layout_height="wrap_content"
72-
android:text="@string/color"
73-
android:theme="@style/Text_Hint" />
74-
75-
<FrameLayout
76-
android:layout_width="match_parent"
77-
android:layout_height="32dp"
78-
android:layout_margin="@dimen/quarter_margin"
79-
android:background="@color/divider_grey">
80-
81-
<View
82-
android:id="@+id/viewColor"
83-
android:layout_width="match_parent"
84-
android:layout_height="match_parent"
85-
android:layout_margin="1dp"
86-
android:background="@color/white" />
87-
</FrameLayout>
88-
89-
</LinearLayout>
90-
91-
</LinearLayout>
92-
93-
<android.support.design.widget.FloatingActionButton
94-
android:id="@+id/fabDone"
95-
android:layout_width="wrap_content"
96-
android:layout_height="wrap_content"
97-
android:layout_gravity="bottom|end"
98-
android:layout_margin="16dp"
99-
android:src="@drawable/ic_done"
100-
app:layout_anchorGravity="bottom|end" />
21+
android:layout_height="?attr/actionBarSize"
22+
android:background="?attr/colorPrimary"
23+
app:popupTheme="@style/AppTheme.PopupOverlay"
24+
/>
25+
26+
</android.support.design.widget.AppBarLayout>
27+
28+
<android.support.v4.view.ViewPager
29+
android:id="@+id/viewPager"
30+
android:layout_width="match_parent"
31+
android:layout_height="match_parent"
32+
app:layout_behavior="@string/appbar_scrolling_view_behavior"
33+
/>
34+
35+
<android.support.design.widget.FloatingActionButton
36+
android:id="@+id/fabDone"
37+
android:layout_width="wrap_content"
38+
android:layout_height="wrap_content"
39+
android:layout_gravity="bottom|end"
40+
android:layout_margin="16dp"
41+
android:src="@drawable/ic_done"
42+
app:layout_anchorGravity="bottom|end"
43+
/>
10144

10245
</android.support.design.widget.CoordinatorLayout>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:orientation="vertical"
8+
android:padding="@dimen/activity_horizontal_margin"
9+
app:layout_behavior="@string/appbar_scrolling_view_behavior"
10+
>
11+
12+
<android.support.design.widget.TextInputLayout
13+
android:id="@+id/tilTitle"
14+
android:layout_width="match_parent"
15+
android:layout_height="wrap_content"
16+
>
17+
18+
<EditText
19+
android:id="@+id/etTitle"
20+
android:layout_width="match_parent"
21+
android:layout_height="wrap_content"
22+
android:hint="@string/title"
23+
android:inputType="text"
24+
android:maxLines="1"
25+
android:singleLine="true"
26+
/>
27+
</android.support.design.widget.TextInputLayout>
28+
29+
<android.support.design.widget.TextInputLayout
30+
android:id="@+id/til_goal"
31+
android:layout_width="match_parent"
32+
android:layout_height="wrap_content"
33+
android:visibility="invisible"
34+
>
35+
36+
<EditText
37+
android:id="@+id/etGoal"
38+
android:layout_width="match_parent"
39+
android:layout_height="wrap_content"
40+
android:hint="@string/goal"
41+
android:inputType="numberDecimal"
42+
android:maxLines="1"
43+
android:singleLine="true"
44+
/>
45+
</android.support.design.widget.TextInputLayout>
46+
47+
<LinearLayout
48+
android:layout_width="match_parent"
49+
android:layout_height="wrap_content"
50+
android:orientation="vertical"
51+
android:visibility="invisible"
52+
>
53+
54+
<TextView
55+
android:layout_width="wrap_content"
56+
android:layout_height="wrap_content"
57+
android:text="@string/color"
58+
android:theme="@style/Text_Hint"
59+
/>
60+
61+
<FrameLayout
62+
android:layout_width="match_parent"
63+
android:layout_height="32dp"
64+
android:layout_margin="@dimen/quarter_margin"
65+
android:background="@color/divider_grey"
66+
>
67+
68+
<View
69+
android:id="@+id/viewColor"
70+
android:layout_width="match_parent"
71+
android:layout_height="match_parent"
72+
android:layout_margin="1dp"
73+
android:background="@color/white"
74+
/>
75+
</FrameLayout>
76+
77+
</LinearLayout>
78+
79+
</LinearLayout>

0 commit comments

Comments
 (0)