FairEmail/app/src/main/java/eu/faircode/email/FragmentRules.java

130 lines
4.3 KiB
Java
Raw Normal View History

2019-01-17 13:29:35 +00:00
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail 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 for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2019 by Marcel Bokhorst (M66B)
*/
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.Group;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;
2019-04-01 07:23:43 +00:00
import androidx.recyclerview.widget.DividerItemDecoration;
2019-01-17 13:29:35 +00:00
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.ArrayList;
import java.util.List;
2019-01-17 13:29:35 +00:00
public class FragmentRules extends FragmentBase {
private RecyclerView rvRule;
private ContentLoadingProgressBar pbWait;
private Group grpReady;
private FloatingActionButton fab;
private AdapterRule adapter;
2019-01-18 08:23:53 +00:00
private long account;
private long folder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get arguments
Bundle args = getArguments();
account = args.getLong("account", -1);
folder = args.getLong("folder", -1);
}
2019-01-17 13:29:35 +00:00
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
2019-01-18 08:23:53 +00:00
setSubtitle(R.string.title_edit_rules);
2019-01-17 13:29:35 +00:00
View view = inflater.inflate(R.layout.fragment_rules, container, false);
// Get controls
rvRule = view.findViewById(R.id.rvRule);
pbWait = view.findViewById(R.id.pbWait);
grpReady = view.findViewById(R.id.grpReady);
fab = view.findViewById(R.id.fab);
// Wire controls
rvRule.setHasFixedSize(false);
LinearLayoutManager llm = new LinearLayoutManager(getContext());
rvRule.setLayoutManager(llm);
adapter = new AdapterRule(this);
2019-01-17 13:29:35 +00:00
rvRule.setAdapter(adapter);
2019-04-04 06:11:18 +00:00
DividerItemDecoration itemDecorator = new DividerItemDecoration(getContext(), llm.getOrientation());
2019-04-01 07:23:43 +00:00
itemDecorator.setDrawable(getContext().getDrawable(R.drawable.divider));
rvRule.addItemDecoration(itemDecorator);
2019-01-17 13:29:35 +00:00
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
2019-01-18 08:23:53 +00:00
Bundle args = new Bundle();
args.putLong("account", account);
args.putLong("folder", folder);
FragmentRule fragment = new FragmentRule();
fragment.setArguments(args);
2019-01-17 13:29:35 +00:00
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
2019-01-18 08:23:53 +00:00
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("rule");
2019-01-17 13:29:35 +00:00
fragmentTransaction.commit();
}
});
// Initialize
grpReady.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
DB db = DB.getInstance(getContext());
2019-01-18 08:23:53 +00:00
db.rule().liveRules(folder).observe(getViewLifecycleOwner(), new Observer<List<TupleRuleEx>>() {
2019-01-17 13:29:35 +00:00
@Override
2019-01-17 21:41:00 +00:00
public void onChanged(List<TupleRuleEx> rules) {
2019-01-17 13:29:35 +00:00
if (rules == null)
rules = new ArrayList<>();
adapter.set(rules);
pbWait.setVisibility(View.GONE);
grpReady.setVisibility(View.VISIBLE);
}
});
}
}