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

Commit ababa1e

Browse files
author
Evgenii Kanivets
committed
#95. Completely move all logic to EditAccountFragment.
1 parent a942879 commit ababa1e

File tree

5 files changed

+83
-31
lines changed

5 files changed

+83
-31
lines changed

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

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.view.Menu
88
import android.view.MenuItem
99

1010
import com.blogspot.e_kanivets.moneytracker.R
11+
import com.blogspot.e_kanivets.moneytracker.activity.account.edit.adapter.EditAccountFragmentPagerAdapter
1112
import com.blogspot.e_kanivets.moneytracker.activity.base.BaseBackActivity
1213
import com.blogspot.e_kanivets.moneytracker.controller.data.AccountController
1314
import com.blogspot.e_kanivets.moneytracker.entity.data.Account
@@ -38,15 +39,11 @@ class EditAccountActivity : BaseBackActivity() {
3839
override fun initViews() {
3940
super.initViews()
4041

41-
fabDone.setOnClickListener { /*done()*/ }
42+
viewPager.adapter = EditAccountFragmentPagerAdapter(supportFragmentManager, account)
4243
}
4344

4445
override fun onCreateOptionsMenu(menu: Menu): Boolean {
45-
if (account.isArchived) {
46-
menuInflater.inflate(R.menu.menu_archived_account, menu)
47-
} else {
48-
menuInflater.inflate(R.menu.menu_account, menu)
49-
}
46+
menuInflater.inflate(if (account.isArchived) R.menu.menu_archived_account else R.menu.menu_account, menu)
5047
return true
5148
}
5249

@@ -59,18 +56,6 @@ class EditAccountActivity : BaseBackActivity() {
5956
}
6057
}
6158

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()
71-
}
72-
}
73-
7459
private fun archive(): Boolean {
7560
if (account == accountController.readDefaultAccount()) {
7661
showToast(R.string.cant_archive_default_account)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.blogspot.e_kanivets.moneytracker.activity.account.edit.adapter
2+
3+
import android.support.v4.app.Fragment
4+
import android.support.v4.app.FragmentManager
5+
import android.support.v4.app.FragmentPagerAdapter
6+
import com.blogspot.e_kanivets.moneytracker.activity.account.edit.fragment.EditAccountFragment
7+
import com.blogspot.e_kanivets.moneytracker.entity.data.Account
8+
9+
class EditAccountFragmentPagerAdapter(fragmentManager: FragmentManager, private val account: Account) :
10+
FragmentPagerAdapter(fragmentManager) {
11+
12+
override fun getItem(position: Int): Fragment {
13+
return when (position) {
14+
0 -> EditAccountFragment.newInstance(account)
15+
else -> Fragment()
16+
}
17+
}
18+
19+
override fun getCount(): Int = 1
20+
21+
}

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
11
package com.blogspot.e_kanivets.moneytracker.activity.account.edit.fragment
22

3+
import android.app.Activity
34
import android.os.Bundle
4-
import android.support.v4.app.Fragment
5-
import android.view.LayoutInflater
5+
import android.support.design.widget.FloatingActionButton
66
import android.view.View
7-
import android.view.ViewGroup
87
import com.blogspot.e_kanivets.moneytracker.R
8+
import com.blogspot.e_kanivets.moneytracker.R.layout
9+
import com.blogspot.e_kanivets.moneytracker.activity.base.BaseFragment
10+
import com.blogspot.e_kanivets.moneytracker.controller.data.AccountController
911
import com.blogspot.e_kanivets.moneytracker.entity.data.Account
1012
import kotlinx.android.synthetic.main.fragment_edit_account.*
13+
import javax.inject.Inject
1114

12-
class EditAccountFragment() : Fragment() {
15+
class EditAccountFragment : BaseFragment() {
16+
17+
@Inject
18+
internal lateinit var accountController: AccountController
1319

1420
private lateinit var account: Account
1521

16-
override fun onCreate(savedInstanceState: Bundle?) {
17-
super.onCreate(savedInstanceState)
18-
arguments?.let { arguments -> account = arguments.getParcelable(KEY_ACCOUNT) }
19-
}
22+
override val contentViewId: Int = layout.fragment_edit_account
2023

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
24+
override fun initData() {
25+
appComponent.inject(this@EditAccountFragment)
26+
arguments?.let { arguments -> account = arguments.getParcelable(KEY_ACCOUNT) }
2527
}
2628

27-
fun initViews() {
29+
override fun initViews(view: View) {
2830
etTitle.setText(account.title)
2931
etGoal.setText(account.goal.toString())
3032
viewColor.setBackgroundColor(account.color)
33+
34+
val fabDone = view.rootView.findViewById<FloatingActionButton>(R.id.fabDone)
35+
fabDone.setOnClickListener { done() }
3136
}
3237

3338
private fun done() {
@@ -40,6 +45,11 @@ class EditAccountFragment() : Fragment() {
4045
account.id, title, account.curSum.toDouble(),
4146
account.currency, account.goal, account.isArchived, account.color
4247
)
48+
val updated = accountController.update(newAccount) != null
49+
if (updated) {
50+
activity?.setResult(Activity.RESULT_OK)
51+
activity?.finish()
52+
}
4353
}
4454
}
4555

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.blogspot.e_kanivets.moneytracker.activity.base
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.MtApp
9+
import com.blogspot.e_kanivets.moneytracker.di.AppComponent
10+
11+
abstract class BaseFragment : Fragment() {
12+
13+
protected val appComponent: AppComponent = MtApp.get().appComponent
14+
15+
protected abstract val contentViewId: Int
16+
17+
protected abstract fun initData()
18+
19+
protected abstract fun initViews(view: View)
20+
21+
override fun onCreate(savedInstanceState: Bundle?) {
22+
super.onCreate(savedInstanceState)
23+
initData()
24+
}
25+
26+
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
27+
inflater.inflate(contentViewId, container, false)
28+
29+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
30+
super.onViewCreated(view, savedInstanceState)
31+
initViews(view)
32+
}
33+
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/di/AppComponent.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.blogspot.e_kanivets.moneytracker.di;
22

33
import com.blogspot.e_kanivets.moneytracker.activity.account.edit.EditAccountActivity;
4+
import com.blogspot.e_kanivets.moneytracker.activity.account.edit.fragment.EditAccountFragment;
45
import com.blogspot.e_kanivets.moneytracker.activity.charts.ChartsActivity;
56
import com.blogspot.e_kanivets.moneytracker.activity.external.BackupActivity;
67
import com.blogspot.e_kanivets.moneytracker.activity.external.ImportExportActivity;
@@ -81,4 +82,6 @@ public interface AppComponent {
8182
void inject(MonthSummaryAdapter monthSummaryAdapter);
8283

8384
void inject(EditAccountActivity editAccountActivity);
85+
86+
void inject(EditAccountFragment editAccountFragment);
8487
}

0 commit comments

Comments
 (0)