From 2c99ee978053941492c9496f0fb8027ab06d94b9 Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Tue, 7 Nov 2017 22:18:31 -0400 Subject: [PATCH] - Added General Settings layout and activity - Now the user can set the prefered country. Which will set also the prefered currency. - Added the general setting model, viewmodel and dao --- app/src/main/AndroidManifest.xml | 2 + .../activities/BoardActivity.java | 12 ++ .../activities/GeneralSettingsActivity.java | 129 ++++++++++++++++++ .../crystalwallet/dao/CrystalDatabase.java | 5 +- .../crystalwallet/dao/GeneralSettingDao.java | 32 +++++ .../crystalwallet/models/GeneralSetting.java | 89 ++++++++++++ .../GeneralSettingListViewModel.java | 40 ++++++ app/src/main/res/layout/board.xml | 14 ++ app/src/main/res/layout/general_settings.xml | 21 +++ 9 files changed, 343 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/cy/agorise/crystalwallet/activities/GeneralSettingsActivity.java create mode 100644 app/src/main/java/cy/agorise/crystalwallet/dao/GeneralSettingDao.java create mode 100644 app/src/main/java/cy/agorise/crystalwallet/models/GeneralSetting.java create mode 100644 app/src/main/java/cy/agorise/crystalwallet/viewmodels/GeneralSettingListViewModel.java create mode 100644 app/src/main/res/layout/general_settings.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1a2889d..cc25f4d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -27,6 +27,8 @@ + + diff --git a/app/src/main/java/cy/agorise/crystalwallet/activities/BoardActivity.java b/app/src/main/java/cy/agorise/crystalwallet/activities/BoardActivity.java index d797ec8..cf6ac67 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/activities/BoardActivity.java +++ b/app/src/main/java/cy/agorise/crystalwallet/activities/BoardActivity.java @@ -1,5 +1,6 @@ package cy.agorise.crystalwallet.activities; +import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; @@ -7,9 +8,11 @@ import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; +import android.widget.ImageButton; import butterknife.BindView; import butterknife.ButterKnife; +import butterknife.OnClick; import cy.agorise.crystalwallet.R; import cy.agorise.crystalwallet.fragments.BalanceFragment; import cy.agorise.crystalwallet.fragments.ContactsFragment; @@ -24,6 +27,9 @@ public class BoardActivity extends AppCompatActivity { @BindView(R.id.pager) public ViewPager mPager; + @BindView(R.id.btnGeneralSettings) + public ImageButton btnGeneralSettings; + public BoardPagerAdapter boardAdapter; @Override @@ -36,6 +42,12 @@ public class BoardActivity extends AppCompatActivity { mPager.setAdapter(boardAdapter); } + @OnClick(R.id.btnGeneralSettings) + public void onBtnGeneralSettingsClick(){ + Intent intent = new Intent(this, GeneralSettingsActivity.class); + startActivity(intent); + } + private class BoardPagerAdapter extends FragmentStatePagerAdapter { public BoardPagerAdapter(FragmentManager fm) { super(fm); diff --git a/app/src/main/java/cy/agorise/crystalwallet/activities/GeneralSettingsActivity.java b/app/src/main/java/cy/agorise/crystalwallet/activities/GeneralSettingsActivity.java new file mode 100644 index 0000000..33a14d9 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/activities/GeneralSettingsActivity.java @@ -0,0 +1,129 @@ +package cy.agorise.crystalwallet.activities; + +import android.arch.lifecycle.LiveData; +import android.arch.lifecycle.Observer; +import android.arch.lifecycle.ViewModelProviders; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v7.app.AppCompatActivity; +import android.widget.ArrayAdapter; +import android.widget.Spinner; +import android.widget.TextView; + +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Currency; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; + +import butterknife.BindView; +import butterknife.ButterKnife; +import butterknife.OnItemSelected; +import cy.agorise.crystalwallet.R; +import cy.agorise.crystalwallet.dao.CrystalDatabase; +import cy.agorise.crystalwallet.models.CryptoCoinBalance; +import cy.agorise.crystalwallet.models.CryptoCurrency; +import cy.agorise.crystalwallet.models.CryptoNetAccount; +import cy.agorise.crystalwallet.models.GeneralSetting; +import cy.agorise.crystalwallet.models.GrapheneAccount; +import cy.agorise.crystalwallet.viewmodels.GeneralSettingListViewModel; +import cy.agorise.crystalwallet.views.CryptoCurrencyAdapter; + +public class GeneralSettingsActivity extends AppCompatActivity { + + @BindView(R.id.spPreferedCountry) + Spinner spPreferedCountry; + + private GeneralSettingListViewModel generalSettingListViewModel; + private LiveData> generalSettingListLiveData; + private HashMap countriesMap; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.general_settings); + + ButterKnife.bind(this); + + this.generalSettingListViewModel = ViewModelProviders.of(this).get(GeneralSettingListViewModel.class); + generalSettingListLiveData = generalSettingListViewModel.getGeneralSettingList(); + + // Initializes the countries spinner + countriesMap = new HashMap(); + String[] countryCodeList = Locale.getISOCountries(); + ArrayList countryAndCurrencyList = new ArrayList(); + String countryAndCurrencyLabel = ""; + for (String countryCode : countryCodeList) { + Locale locale = new Locale("", countryCode); + try { + Currency currency = Currency.getInstance(locale); + countryAndCurrencyLabel = locale.getDisplayCountry() + " (" + currency.getCurrencyCode() + ")"; + countryAndCurrencyList.add(countryAndCurrencyLabel); + countriesMap.put(countryCode, countryAndCurrencyLabel); + countriesMap.put(countryAndCurrencyLabel, countryCode); + } catch (Exception e) { + + } + } + Collections.sort(countryAndCurrencyList); + countryAndCurrencyList.add(0,"SELECT COUNTRY"); + ArrayAdapter countryAdapter = new ArrayAdapter(this.getApplicationContext(), android.R.layout.simple_spinner_item, countryAndCurrencyList); + spPreferedCountry.setAdapter(countryAdapter); + + //Observes the general settings data + generalSettingListLiveData.observe(this, new Observer>() { + @Override + public void onChanged(@Nullable List generalSettings) { + loadSettings(generalSettings); + } + }); + } + + public GeneralSetting getSetting(String name){ + for (GeneralSetting generalSetting:this.generalSettingListLiveData.getValue()) { + if (generalSetting.getName().equals(name)) { + return generalSetting; + } + } + + return null; + } + + @OnItemSelected(R.id.spPreferedCountry) + void onItemSelected(int position) { + if (position != 0) { + GeneralSetting generalSettingCountryCode = this.getSetting(GeneralSetting.SETTING_NAME_PREFERED_COUNTRY); + GeneralSetting generalSettingCurrency = this.getSetting(GeneralSetting.SETTING_NAME_PREFERED_CURRENCY); + + if (generalSettingCountryCode == null){ + generalSettingCountryCode = new GeneralSetting(); + generalSettingCountryCode.setName(GeneralSetting.SETTING_NAME_PREFERED_COUNTRY); + } + if (generalSettingCurrency == null){ + generalSettingCurrency = new GeneralSetting(); + generalSettingCurrency.setName(GeneralSetting.SETTING_NAME_PREFERED_CURRENCY); + } + + String countryCode = countriesMap.get((String) spPreferedCountry.getSelectedItem()); + Locale locale = new Locale("", countryCode); + Currency currency = Currency.getInstance(locale); + + generalSettingCountryCode.setValue(countryCode); + generalSettingCurrency.setValue(currency.getCurrencyCode()); + this.generalSettingListViewModel.saveGeneralSettings(generalSettingCountryCode, generalSettingCurrency); + } + } + + public void loadSettings(List generalSettings){ + for (GeneralSetting generalSetting:generalSettings) { + if (generalSetting.getName().equals(GeneralSetting.SETTING_NAME_PREFERED_COUNTRY)){ + String preferedCountryCode = generalSetting.getValue(); + spPreferedCountry.setSelection(((ArrayAdapter)spPreferedCountry.getAdapter()).getPosition(countriesMap.get(preferedCountryCode))); + } + } + } +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/CrystalDatabase.java b/app/src/main/java/cy/agorise/crystalwallet/dao/CrystalDatabase.java index a6df432..073ddfe 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/CrystalDatabase.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/CrystalDatabase.java @@ -14,6 +14,7 @@ import cy.agorise.crystalwallet.models.CryptoCoinTransaction; import cy.agorise.crystalwallet.models.CryptoCurrency; import cy.agorise.crystalwallet.models.CryptoCurrencyEquivalence; import cy.agorise.crystalwallet.models.CryptoNetAccount; +import cy.agorise.crystalwallet.models.GeneralSetting; import cy.agorise.crystalwallet.models.GrapheneAccountInfo; /** @@ -29,7 +30,8 @@ import cy.agorise.crystalwallet.models.GrapheneAccountInfo; CryptoCoinBalance.class, GrapheneAccountInfo.class, BitsharesAssetInfo.class, - CryptoCurrencyEquivalence.class + CryptoCurrencyEquivalence.class, + GeneralSetting.class }, version = 2) @TypeConverters({Converters.class}) public abstract class CrystalDatabase extends RoomDatabase { @@ -44,6 +46,7 @@ public abstract class CrystalDatabase extends RoomDatabase { public abstract CryptoCurrencyDao cryptoCurrencyDao(); public abstract BitsharesAssetDao bitsharesAssetDao(); public abstract CryptoCurrencyEquivalenceDao cryptoCurrencyEquivalenceDao(); + public abstract GeneralSettingDao generalSettingDao(); public static CrystalDatabase getAppDatabase(Context context) { if (instance == null) { diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/GeneralSettingDao.java b/app/src/main/java/cy/agorise/crystalwallet/dao/GeneralSettingDao.java new file mode 100644 index 0000000..0a8dc96 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/GeneralSettingDao.java @@ -0,0 +1,32 @@ +package cy.agorise.crystalwallet.dao; + +import android.arch.lifecycle.LiveData; +import android.arch.persistence.room.Dao; +import android.arch.persistence.room.Insert; +import android.arch.persistence.room.OnConflictStrategy; +import android.arch.persistence.room.Query; + +import java.util.List; + +import cy.agorise.crystalwallet.models.AccountSeed; +import cy.agorise.crystalwallet.models.GeneralSetting; + +/** + * Created by Henry Varona on 10/9/2017. + */ + +@Dao +public interface GeneralSettingDao { + + @Query("SELECT * FROM general_setting") + LiveData> getAll(); + + @Query("SELECT * FROM general_setting WHERE name = :name") + LiveData getByName(String name); + + @Insert(onConflict = OnConflictStrategy.REPLACE) + public long[] insertGeneralSettings(GeneralSetting... generalSettings); + + @Insert(onConflict = OnConflictStrategy.REPLACE) + public long insertGeneralSetting(GeneralSetting generalSetting); +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/GeneralSetting.java b/app/src/main/java/cy/agorise/crystalwallet/models/GeneralSetting.java new file mode 100644 index 0000000..b5b322d --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/models/GeneralSetting.java @@ -0,0 +1,89 @@ +package cy.agorise.crystalwallet.models; + + +import android.arch.persistence.room.ColumnInfo; +import android.arch.persistence.room.Entity; +import android.arch.persistence.room.PrimaryKey; +import android.support.annotation.NonNull; +import android.support.v7.recyclerview.extensions.DiffCallback; + +import cy.agorise.crystalwallet.enums.SeedType; + +/** + * Created by Henry Varona on 6/11/2017. + */ +@Entity(tableName = "general_setting") +public class GeneralSetting { + + public final static String SETTING_NAME_PREFERED_COUNTRY = "PREFERED_COUNTRY"; + public final static String SETTING_NAME_PREFERED_CURRENCY = "PREFERED_CURRENCY"; + + /** + * The id on the database + */ + @PrimaryKey(autoGenerate = true) + @ColumnInfo(name = "id") + private long mId; + + /** + * The name of this setting + */ + @ColumnInfo(name = "name") + private String mName; + + /** + * The value of this setting + */ + @ColumnInfo(name = "value") + private String mValue; + + public long getId() { + return mId; + } + + public void setId(long id){ + this.mId = id; + } + + public String getName() { + return mName; + } + + public void setName(String mName) { + this.mName = mName; + } + + public String getValue() { + return mValue; + } + + public void setValue(String mValue) { + this.mValue = mValue; + } + + public static final DiffCallback DIFF_CALLBACK = new DiffCallback() { + @Override + public boolean areItemsTheSame( + @NonNull GeneralSetting oldSetting, @NonNull GeneralSetting newSetting) { + return oldSetting.getId() == newSetting.getId(); + } + @Override + public boolean areContentsTheSame( + @NonNull GeneralSetting oldSetting, @NonNull GeneralSetting newSetting) { + return oldSetting.equals(newSetting); + } + }; + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + GeneralSetting that = (GeneralSetting) o; + + if (mId != that.mId) return false; + if (mName != that.mName) return false; + return mValue.equals(that.mValue); + + } +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/GeneralSettingListViewModel.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/GeneralSettingListViewModel.java new file mode 100644 index 0000000..492c1b3 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/GeneralSettingListViewModel.java @@ -0,0 +1,40 @@ +package cy.agorise.crystalwallet.viewmodels; + +import android.app.Application; +import android.arch.lifecycle.AndroidViewModel; +import android.arch.lifecycle.LiveData; +import android.arch.paging.PagedList; + +import java.util.List; + +import cy.agorise.crystalwallet.dao.CrystalDatabase; +import cy.agorise.crystalwallet.models.CryptoCoinTransaction; +import cy.agorise.crystalwallet.models.GeneralSetting; + +/** + * Created by Henry Varona on 6/11/2017. + */ + +public class GeneralSettingListViewModel extends AndroidViewModel { + + private LiveData> generalSettingList; + private CrystalDatabase db; + + public GeneralSettingListViewModel(Application application) { + super(application); + this.db = CrystalDatabase.getAppDatabase(application.getApplicationContext()); + generalSettingList = this.db.generalSettingDao().getAll(); + } + + public LiveData> getGeneralSettingList(){ + return this.generalSettingList; + } + + public void saveGeneralSetting(GeneralSetting generalSetting){ + this.db.generalSettingDao().insertGeneralSetting(generalSetting); + } + + public void saveGeneralSettings(GeneralSetting... generalSettings){ + this.db.generalSettingDao().insertGeneralSettings(generalSettings); + } +} diff --git a/app/src/main/res/layout/board.xml b/app/src/main/res/layout/board.xml index 0d54b5a..abb5e7b 100644 --- a/app/src/main/res/layout/board.xml +++ b/app/src/main/res/layout/board.xml @@ -7,4 +7,18 @@ android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" /> + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/general_settings.xml b/app/src/main/res/layout/general_settings.xml new file mode 100644 index 0000000..96fbfd9 --- /dev/null +++ b/app/src/main/res/layout/general_settings.xml @@ -0,0 +1,21 @@ + + + + + + +