package net.ktnx.mobileledger.model;
import net.ktnx.mobileledger.utils.ObservableAtomicInteger;
+import net.ktnx.mobileledger.utils.ObservableList;
import net.ktnx.mobileledger.utils.ObservableValue;
import java.util.ArrayList;
public static ObservableAtomicInteger backgroundTaskCount = new ObservableAtomicInteger(0);
public static ObservableValue<Date> lastUpdateDate = new ObservableValue<>();
public static ObservableValue<MobileLedgerProfile> profile = new ObservableValue<>();
+ public static ObservableList<MobileLedgerProfile> profiles =
+ new ObservableList<>(new ArrayList<>());
}
public void set_option_value(String name, long value) {
set_option_value(name, String.valueOf(value));
}
+ public void removeFromDB() {
+ SQLiteDatabase db = MLDB.getWritableDatabase();
+ Log.d("db", String.format("removinf progile %s from DB", uuid));
+ db.execSQL("delete from profiles where uuid=?", new Object[]{uuid});
+ }
}
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
-import java.util.List;
import java.util.Observable;
import java.util.Observer;
});
}
private void setupProfile() {
- List<MobileLedgerProfile> profiles = MobileLedgerProfile.loadAllFromDB();
+ Data.profiles.setList(MobileLedgerProfile.loadAllFromDB());
MobileLedgerProfile profile = null;
String profileUUID = MLDB.get_option_value(MLDB.OPT_PROFILE_UUID, null);
if (profileUUID == null) {
- if (profiles.isEmpty()) {
- profiles = MobileLedgerProfile.createInitialProfileList();
- profile = profiles.get(0);
+ if (Data.profiles.isEmpty()) {
+ Data.profiles.setList(MobileLedgerProfile.createInitialProfileList());
+ profile = Data.profiles.get(0);
SharedPreferences backend = getSharedPreferences("backend", MODE_PRIVATE);
Log.d("profiles", "Migrating from preferences to profiles");
profile = MobileLedgerProfile.loadUUIDFromDB(profileUUID);
}
- if (profile == null) profile = profiles.get(0);
+ if (profile == null) profile = Data.profiles.get(0);
if (profile == null) throw new AssertionError("profile must have a value");
}
private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
- List<MobileLedgerProfile> list = MobileLedgerProfile.loadAllFromDB();
- recyclerView.setAdapter(new ProfilesRecyclerViewAdapter(this, list, mTwoPane));
+ recyclerView.setAdapter(new ProfilesRecyclerViewAdapter(this, mTwoPane));
}
public static class ProfilesRecyclerViewAdapter
extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
private final ProfileListActivity mParentActivity;
- private final List<MobileLedgerProfile> mValues;
private final boolean mTwoPane;
private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
editProfile(view, item);
}
};
- ProfilesRecyclerViewAdapter(ProfileListActivity parent, List<MobileLedgerProfile> items,
- boolean twoPane) {
- mValues = items;
+ ProfilesRecyclerViewAdapter(ProfileListActivity parent, boolean twoPane) {
mParentActivity = parent;
mTwoPane = twoPane;
+ Data.profiles.addObserver((o, arg) ->{
+ Log.d("profiles", "profile list changed");
+ notifyDataSetChanged();
+ });
}
private void editProfile(View view, MobileLedgerProfile item) {
if (mTwoPane) {
}
@Override
public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
- final MobileLedgerProfile profile = mValues.get(position);
+ final MobileLedgerProfile profile = Data.profiles.get(position);
final MobileLedgerProfile currentProfile = Data.profile.get();
Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
currentProfile.getUuid()));
}
@Override
public int getItemCount() {
- return mValues.size();
+ return Data.profiles.size();
}
class ProfileListViewHolder extends RecyclerView.ViewHolder {
final RadioButton mRadioView;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
-import android.support.v7.widget.Toolbar;
-import android.view.View;
-import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBar;
+import android.support.v7.app.AppCompatActivity;
+import android.support.v7.widget.Toolbar;
+import android.util.Log;
+import android.view.Menu;
import android.view.MenuItem;
+import android.view.View;
import net.ktnx.mobileledger.R;
+import net.ktnx.mobileledger.model.Data;
+import net.ktnx.mobileledger.model.MobileLedgerProfile;
import net.ktnx.mobileledger.ui.activity.ProfileListActivity;
/**
* in a {@link ProfileListActivity}.
*/
public class ProfileDetailActivity extends AppCompatActivity {
-
+ private MobileLedgerProfile profile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// http://developer.android.com/guide/components/fragments.html
//
if (savedInstanceState == null) {
+ final String profileUUID =
+ getIntent().getStringExtra(ProfileDetailFragment.ARG_ITEM_ID);
+
+ if (profileUUID != null) {
+ int i = 0;
+ for (MobileLedgerProfile p : Data.profiles.getList()) {
+ if (p.getUuid().equals(profileUUID)) {
+ Log.d("profiles", String.format("found profile %s at %d", profileUUID, i));
+ profile = p;
+ break;
+ }
+ i++;
+ }
+ if (profile == null) throw new AssertionError(
+ String.format("Can't get profile " + "(uuid:%s) from the " + "global list",
+ profileUUID));
+ }
+
// Create the detail fragment and add it to the activity
// using a fragment transaction.
Bundle arguments = new Bundle();
- arguments.putString(ProfileDetailFragment.ARG_ITEM_ID,
- getIntent().getStringExtra(ProfileDetailFragment.ARG_ITEM_ID));
+ arguments.putString(ProfileDetailFragment.ARG_ITEM_ID, profileUUID);
ProfileDetailFragment fragment = new ProfileDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.add(R.id.profile_detail_container, fragment).commit();
}
}
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ super.onCreateOptionsMenu(menu);
+ Log.d("profiles", "[activity] Creating profile details options menu");
+ getMenuInflater().inflate(R.menu.profile_details, menu);
+ menu.findItem(R.id.menuDelete).setOnMenuItemClickListener(item -> {
+ Log.d("profiles", String.format("deleting profile %s", profile.getUuid()));
+ profile.removeFromDB();
+ Data.profiles.remove(profile);
+ Data.profile.set(Data.profiles.get(0));
+ finish();
+ return true;
+ });
+
+ return true;
+ }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
*/
public ProfileDetailFragment() {
}
-
+ @Override
+ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
+ Log.d("profiles", "[fragment] Creating profile details options menu");
+ super.onCreateOptionsMenu(menu, inflater);
+ inflater.inflate(R.menu.profile_details, menu);
+ menu.findItem(R.id.menuDelete).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
+ @Override
+ public boolean onMenuItemClick(MenuItem item) {
+ Log.d("profiles", String.format("[fragment] removing profile %s", mItem.getUuid()));
+ mItem.removeFromDB();
+ Data.profiles.remove(mItem);
+ if (Data.profile.get().getUuid().equals(mItem.getUuid())) {
+ Data.profile.set(Data.profiles.get(0));
+ }
+ return false;
+ }
+ });
+ }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mItem = new MobileLedgerProfile(profileName.getText(), url.getText(),
useAuthentication.isChecked(), userName.getText(), password.getText());
mItem.storeInDB();
+ Data.profiles.add(mItem);
}
Activity activity = getActivity();
--- /dev/null
+<!--
+ ~ Copyright Google Inc.
+ ~
+ ~ Licensed under the Apache License, version 2.0 ("the License");
+ ~ you may not use this file except in compliance with the License.
+ ~ You may obtain a copy of the license at:
+ ~
+ ~ https://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distribution under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissionsand
+ ~ limitations under the License.
+ -->
+
+<vector android:height="24dp" android:tint="#EEEEEE"
+ android:viewportHeight="24.0" android:viewportWidth="24.0"
+ android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
+ <path android:fillColor="#FF000000" android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z"/>
+</vector>
--- /dev/null
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ ~ Copyright © 2019 Damyan Ivanov.
+ ~ This file is part of Mobile-Ledger.
+ ~ Mobile-Ledger is free software: you can distribute it and/or modify it
+ ~ under the term of the GNU General Public License as published by
+ ~ the Free Software Foundation, either version 3 of the License, or
+ ~ (at your opinion), any later version.
+ ~
+ ~ Mobile-Ledger is distributed in the hope that it will be useful,
+ ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ ~ GNU General Public License terms for details.
+ ~
+ ~ You should have received a copy of the GNU General Public License
+ ~ along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
+ -->
+
+<menu xmlns:android="http://schemas.android.com/apk/res/android">
+
+ <item
+ android:id="@+id/menuDelete"
+ android:icon="@drawable/ic_delete_white_24dp"
+ android:title="@string/delete" />
+</menu>
\ No newline at end of file
<string name="title_activity_transaction_list">Трансакции</string>
<string name="err_http_error">Грешка в HTTP</string>
<string name="new_profile_title">Нов профил</string>
+ <string name="delete">Изтриване на профила</string>
</resources>
\ No newline at end of file
<string name="title_profile_details">Profile Details</string>
<string name="profiles">Profiles</string>
<string name="new_profile_title" type="id">New profile</string>
+ <string name="delete">Delete profile</string>
</resources>