Add account type list activity

This commit is contained in:
Shinokuni 2019-05-21 12:57:08 +02:00
parent c771086989
commit feba9b321e
13 changed files with 375 additions and 13 deletions

View File

@ -16,21 +16,22 @@
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".activities.AddAccountActivity">
<activity android:name=".activities.AccountTypeListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.AddAccountActivity">
</activity>
<activity
android:name=".activities.ManageFeedsActivity"
android:parentActivityName=".activities.MainActivity" />
<activity
android:name=".activities.MainActivity"
android:theme="@style/AppTheme.NoActionBar">
</activity>
android:theme="@style/AppTheme.NoActionBar"></activity>
<activity
android:name=".activities.ItemActivity"
android:parentActivityName=".activities.MainActivity"

View File

@ -0,0 +1,99 @@
package com.readrops.app.activities;
import android.content.Intent;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.readrops.app.R;
import com.readrops.app.database.entities.Account;
import com.readrops.app.databinding.ActivityAccountTypeListBinding;
import com.readrops.app.viewmodels.AccountViewModel;
import com.readrops.app.views.AccountType;
import com.readrops.app.views.AccountTypeListAdapter;
import java.util.ArrayList;
import java.util.List;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableSingleObserver;
import io.reactivex.schedulers.Schedulers;
public class AccountTypeListActivity extends AppCompatActivity {
private ActivityAccountTypeListBinding binding;
private AccountTypeListAdapter adapter;
private AccountViewModel viewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_account_type_list);
viewModel = ViewModelProviders.of(this).get(AccountViewModel.class);
binding.accountTypeRecyclerview.setLayoutManager(new LinearLayoutManager(this));
binding.accountTypeRecyclerview.addItemDecoration(new DividerItemDecoration(this, LinearLayout.VERTICAL));
adapter = new AccountTypeListAdapter(accountType -> {
if (!(accountType.getAccountType() == Account.AccountType.LOCAL)) {
Intent intent = new Intent(getApplicationContext(), AddAccountActivity.class);
intent.putExtra("accountType", accountType);
startActivity(intent);
finish();
} else
createNewLocalAccount(accountType);
});
binding.accountTypeRecyclerview.setAdapter(adapter);
adapter.setAccountTypes(getData());
}
private List<AccountType> getData() {
List<AccountType> accountTypes = new ArrayList<>();
AccountType localAccount = new AccountType(getString(R.string.local_account),
R.drawable.ic_readrops, Account.AccountType.LOCAL);
AccountType nextNewsAccount = new AccountType(getString(R.string.nextcloud_news),
R.drawable.ic_nextcloud_news, Account.AccountType.NEXTCLOUD_NEWS);
accountTypes.add(localAccount);
accountTypes.add(nextNewsAccount);
return accountTypes;
}
private void createNewLocalAccount(AccountType accountType) {
Account account = new Account(null, accountType.getName(), Account.AccountType.LOCAL);
account.setCurrentAccount(true);
viewModel.insert(account)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new DisposableSingleObserver<Long>() {
@Override
public void onSuccess(Long id) {
account.setId(id.intValue());
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra(MainActivity.ACCOUNT_KEY, account);
startActivity(intent);
finish();
}
@Override
public void onError(Throwable e) {
Toast.makeText(AccountTypeListActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}

View File

@ -15,6 +15,7 @@ import com.readrops.app.database.entities.Account;
import com.readrops.app.databinding.ActivityAddAccountBinding;
import com.readrops.app.utils.SharedPreferencesManager;
import com.readrops.app.viewmodels.AccountViewModel;
import com.readrops.app.views.AccountType;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
@ -26,7 +27,7 @@ public class AddAccountActivity extends AppCompatActivity {
private ActivityAddAccountBinding binding;
private AccountViewModel viewModel;
private Account.AccountType accountType;
private AccountType accountType;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -35,7 +36,10 @@ public class AddAccountActivity extends AppCompatActivity {
binding = DataBindingUtil.setContentView(this, R.layout.activity_add_account);
viewModel = ViewModelProviders.of(this).get(AccountViewModel.class);
accountType = Account.AccountType.NEXTCLOUD_NEWS;
accountType = getIntent().getParcelableExtra("accountType");
binding.providerImage.setImageResource(accountType.getLogoId());
binding.providerName.setText(accountType.getName());
binding.addAccountSkip.setOnClickListener(v -> {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
@ -44,7 +48,7 @@ public class AddAccountActivity extends AppCompatActivity {
finish();
});
binding.addAccountName.setText(accountType.name().replace("_", " "));
binding.addAccountName.setText(accountType.getName());
}
public void createAccount(View view) {
@ -54,7 +58,7 @@ public class AddAccountActivity extends AppCompatActivity {
String login = binding.addAccountLogin.getText().toString().trim();
String password = binding.addAccountPassword.getText().toString().trim();
Account account = new Account(url, name, accountType);
Account account = new Account(url, name, accountType.getAccountType());
account.setLogin(login);
account.setPassword(password);
@ -77,14 +81,14 @@ public class AddAccountActivity extends AppCompatActivity {
saveLoginPassword(account);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("account", account);
intent.putExtra(MainActivity.ACCOUNT_KEY, account);
startActivity(intent);
finish();
} else {
binding.addAccountValidate.setEnabled(true);
Toast.makeText(AddAccountActivity.this, "Impossible to login",
Toast.LENGTH_LONG).show();
Toast.LENGTH_LONG).show();
}
}
@ -92,7 +96,7 @@ public class AddAccountActivity extends AppCompatActivity {
public void onError(Throwable e) {
binding.addAccountLoading.setVisibility(View.GONE);
binding.addAccountValidate.setEnabled(true);
Toast.makeText(AddAccountActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
Toast.makeText(AddAccountActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}

View File

@ -72,6 +72,8 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
public static final int ITEM_REQUEST = 3;
public static final int ADD_ACCOUNT_REQUEST = 4;
public static final String ACCOUNT_KEY = "account";
private RecyclerView recyclerView;
private MainItemListAdapter adapter;
private SwipeRefreshLayout refreshLayout;
@ -139,7 +141,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
feedCount = 0;
initRecyclerView();
account = getIntent().getParcelableExtra("account");
account = getIntent().getParcelableExtra(ACCOUNT_KEY);
if (account != null) {
buildDrawer();
refreshLayout.setRefreshing(true);
@ -155,7 +157,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
private void buildDrawer() {
ProfileDrawerItem profileItem = new ProfileDrawerItem()
.withIcon(R.drawable.ic_nextcloud_news)
.withIcon(Account.getLogoFromAccountType(account.getAccountType()))
.withName(account.getDisplayedName())
.withEmail(account.getAccountName());

View File

@ -26,4 +26,7 @@ public interface AccountDao {
@Query("Update Account set last_modified = :lastModified Where id = :accountId")
void updateLastModified(int accountId, long lastModified);
@Query("Update Account set current_account = 0 Where id Not In (:accountId)")
void setCurrentAccountsToFalse(int accountId);
}

View File

@ -3,11 +3,14 @@ package com.readrops.app.database.entities;
import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.DrawableRes;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;
import com.readrops.app.R;
@Entity
public class Account implements Parcelable {
@ -198,4 +201,15 @@ public class Account implements Parcelable {
return null;
}
public static @DrawableRes int getLogoFromAccountType(AccountType accountType) {
switch (accountType) {
case LOCAL:
return R.drawable.ic_readrops;
case NEXTCLOUD_NEWS:
return R.drawable.ic_nextcloud_news;
default:
return R.drawable.ic_readrops;
}
}
}

View File

@ -5,6 +5,7 @@ import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import com.readrops.app.database.Database;
import com.readrops.app.database.entities.Account;
import com.readrops.app.repositories.ARepository;
import com.readrops.app.repositories.NextNewsRepository;
@ -14,14 +15,24 @@ import io.reactivex.Single;
public class AccountViewModel extends AndroidViewModel {
private ARepository repository;
private Database database;
public AccountViewModel(@NonNull Application application) {
super(application);
repository = new NextNewsRepository(application);
database = Database.getInstance(application);
}
public Single<Boolean> login(Account account) {
return repository.login(account);
}
public Single<Long> insert(Account account) {
return Single.create(emitter -> {
long id = database.accountDao().insert(account);
emitter.onSuccess(id);
});
}
}

View File

@ -0,0 +1,78 @@
package com.readrops.app.views;
import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.DrawableRes;
import com.readrops.app.database.entities.Account;
public class AccountType implements Parcelable {
private String name;
private @DrawableRes int logoId;
private Account.AccountType accountType;
public AccountType(String name, int logoId, Account.AccountType accountType) {
this.name = name;
this.logoId = logoId;
this.accountType = accountType;
}
protected AccountType(Parcel in) {
name = in.readString();
logoId = in.readInt();
accountType = Account.AccountType.values()[in.readInt()];
}
public static final Creator<AccountType> CREATOR = new Creator<AccountType>() {
@Override
public AccountType createFromParcel(Parcel in) {
return new AccountType(in);
}
@Override
public AccountType[] newArray(int size) {
return new AccountType[size];
}
};
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public @DrawableRes
int getLogoId() {
return logoId;
}
public void setLogoId(@DrawableRes int logoId) {
this.logoId = logoId;
}
public Account.AccountType getAccountType() {
return accountType;
}
public void setAccountType(Account.AccountType accountType) {
this.accountType = accountType;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(logoId);
dest.writeInt(accountType.getCode());
}
}

View File

@ -0,0 +1,67 @@
package com.readrops.app.views;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import com.readrops.app.R;
import com.readrops.app.databinding.AccountTypeItemBinding;
import java.util.List;
public class AccountTypeListAdapter extends RecyclerView.Adapter<AccountTypeListAdapter.AccountTypeViewHolder> {
private List<AccountType> accountTypes;
private OnItemClickListener listener;
public AccountTypeListAdapter(OnItemClickListener listener) {
this.listener = listener;
}
@NonNull
@Override
public AccountTypeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
AccountTypeItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
R.layout.account_type_item, parent, false);
return new AccountTypeViewHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull AccountTypeViewHolder holder, int position) {
AccountType accountType = accountTypes.get(position);
holder.binding.accountTypeName.setText(accountType.getName());
holder.binding.accountTypeLogo.setImageResource(accountType.getLogoId());
holder.binding.getRoot().setOnClickListener(v -> listener.onItemClick(accountType));
}
@Override
public int getItemCount() {
return accountTypes.size();
}
public void setAccountTypes(List<AccountType> accountTypes) {
this.accountTypes = accountTypes;
notifyDataSetChanged();
}
public interface OnItemClickListener {
void onItemClick(AccountType accountType);
}
public class AccountTypeViewHolder extends RecyclerView.ViewHolder {
private AccountTypeItemBinding binding;
public AccountTypeViewHolder(AccountTypeItemBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
}

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:padding="8dp">
<ImageView
android:id="@+id/account_type_logo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
tools:src="@drawable/ic_nextcloud_news" />
<TextView
android:id="@+id/account_type_name"
style="@style/TextAppearance.AppCompat.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginStart="12dp"
android:layout_toEndOf="@id/account_type_logo"
tools:text="@string/nextcloud_news" />
</RelativeLayout>
</layout>

View File

@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<layout 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"
tools:context=".activities.AccountTypeListActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/account_type_list_app_name"
style="@style/TextAppearance.AppCompat.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="@string/app_name"
android:textSize="60sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/account_type_list_choose"
style="@style/TextAppearance.AppCompat.Subhead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="@string/choose_account"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/account_type_list_app_name" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/account_type_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="80dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/account_type_list_choose"
tools:itemCount="5"
tools:listitem="@layout/account_type_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

View File

@ -57,5 +57,7 @@
<string name="account_settings">Paramètres du compte</string>
<string name="add_account">Ajouter un compte</string>
<string name="no_feed">Aucun flux</string>
<string name="choose_account">Choisir un compte</string>
<string name="local_account">Compte local</string>
</resources>

View File

@ -60,4 +60,6 @@
<string name="account_settings">Account settings</string>
<string name="add_account">Add account</string>
<string name="no_feed">No feed</string>
<string name="choose_account">Choose an account</string>
<string name="local_account">Local account</string>
</resources>