This commit is contained in:
parent
179f2015bc
commit
e0a937e231
@ -1,12 +1,8 @@
|
|||||||
// history_page.dart
|
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_contacts/flutter_contacts.dart';
|
import 'package:flutter_contacts/flutter_contacts.dart';
|
||||||
import 'package:intl/intl.dart'; // For date formatting
|
import 'package:intl/intl.dart';
|
||||||
import 'package:url_launcher/url_launcher.dart'; // For launching URLs (phone calls, SMS)
|
import 'package:url_launcher/url_launcher.dart';
|
||||||
import 'package:dialer/features/contacts/contact_state.dart';
|
import 'package:dialer/features/contacts/contact_state.dart';
|
||||||
|
|
||||||
// Import the helper functions
|
|
||||||
import 'package:dialer/widgets/username_color_generator.dart';
|
import 'package:dialer/widgets/username_color_generator.dart';
|
||||||
import 'package:dialer/widgets/color_darkener.dart';
|
import 'package:dialer/widgets/color_darkener.dart';
|
||||||
|
|
||||||
@ -33,11 +29,9 @@ class HistoryPage extends StatefulWidget {
|
|||||||
_HistoryPageState createState() => _HistoryPageState();
|
_HistoryPageState createState() => _HistoryPageState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _HistoryPageState extends State<HistoryPage> {
|
class _HistoryPageState extends State<HistoryPage> with SingleTickerProviderStateMixin {
|
||||||
List<History> histories = [];
|
List<History> histories = [];
|
||||||
bool loading = true;
|
bool loading = true;
|
||||||
|
|
||||||
// Track expanded items
|
|
||||||
int? _expandedIndex;
|
int? _expandedIndex;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -59,7 +53,6 @@ class _HistoryPageState extends State<HistoryPage> {
|
|||||||
}
|
}
|
||||||
List<Contact> contacts = contactState.contacts;
|
List<Contact> contacts = contactState.contacts;
|
||||||
|
|
||||||
// Ensure there are enough contacts
|
|
||||||
if (contacts.isEmpty) {
|
if (contacts.isEmpty) {
|
||||||
setState(() {
|
setState(() {
|
||||||
loading = false;
|
loading = false;
|
||||||
@ -67,7 +60,6 @@ class _HistoryPageState extends State<HistoryPage> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build histories using the contacts
|
|
||||||
setState(() {
|
setState(() {
|
||||||
histories = List.generate(
|
histories = List.generate(
|
||||||
contacts.length >= 10 ? 10 : contacts.length,
|
contacts.length >= 10 ? 10 : contacts.length,
|
||||||
@ -83,6 +75,47 @@ class _HistoryPageState extends State<HistoryPage> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List _buildGroupedList(List<History> historyList) {
|
||||||
|
// Sort histories by date (most recent first)
|
||||||
|
historyList.sort((a, b) => b.date.compareTo(a.date));
|
||||||
|
|
||||||
|
final now = DateTime.now();
|
||||||
|
final today = DateTime(now.year, now.month, now.day);
|
||||||
|
final yesterday = today.subtract(const Duration(days: 1));
|
||||||
|
|
||||||
|
List<History> todayHistories = [];
|
||||||
|
List<History> yesterdayHistories = [];
|
||||||
|
List<History> olderHistories = [];
|
||||||
|
|
||||||
|
for (var history in historyList) {
|
||||||
|
final callDate = DateTime(history.date.year, history.date.month, history.date.day);
|
||||||
|
if (callDate == today) {
|
||||||
|
todayHistories.add(history);
|
||||||
|
} else if (callDate == yesterday) {
|
||||||
|
yesterdayHistories.add(history);
|
||||||
|
} else {
|
||||||
|
olderHistories.add(history);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Combine them with headers
|
||||||
|
final items = <dynamic>[];
|
||||||
|
if (todayHistories.isNotEmpty) {
|
||||||
|
items.add('Today');
|
||||||
|
items.addAll(todayHistories);
|
||||||
|
}
|
||||||
|
if (yesterdayHistories.isNotEmpty) {
|
||||||
|
items.add('Yesterday');
|
||||||
|
items.addAll(yesterdayHistories);
|
||||||
|
}
|
||||||
|
if (olderHistories.isNotEmpty) {
|
||||||
|
items.add('Older');
|
||||||
|
items.addAll(olderHistories);
|
||||||
|
}
|
||||||
|
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final contactState = ContactState.of(context);
|
final contactState = ContactState.of(context);
|
||||||
@ -90,9 +123,6 @@ class _HistoryPageState extends State<HistoryPage> {
|
|||||||
if (loading || contactState.loading) {
|
if (loading || contactState.loading) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
backgroundColor: Colors.black,
|
backgroundColor: Colors.black,
|
||||||
appBar: AppBar(
|
|
||||||
title: const Text('History'),
|
|
||||||
),
|
|
||||||
body: const Center(
|
body: const Center(
|
||||||
child: CircularProgressIndicator(),
|
child: CircularProgressIndicator(),
|
||||||
),
|
),
|
||||||
@ -102,9 +132,6 @@ class _HistoryPageState extends State<HistoryPage> {
|
|||||||
if (histories.isEmpty) {
|
if (histories.isEmpty) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
backgroundColor: Colors.black,
|
backgroundColor: Colors.black,
|
||||||
appBar: AppBar(
|
|
||||||
title: const Text('History'),
|
|
||||||
),
|
|
||||||
body: const Center(
|
body: const Center(
|
||||||
child: Text(
|
child: Text(
|
||||||
'No call history available.',
|
'No call history available.',
|
||||||
@ -114,19 +141,66 @@ class _HistoryPageState extends State<HistoryPage> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Scaffold(
|
// Filter missed calls
|
||||||
backgroundColor: Colors.black,
|
List<History> missedCalls = histories.where((h) => h.callStatus == 'missed').toList();
|
||||||
appBar: AppBar(
|
|
||||||
title: const Text('History'),
|
final allItems = _buildGroupedList(histories);
|
||||||
|
final missedItems = _buildGroupedList(missedCalls);
|
||||||
|
|
||||||
|
return DefaultTabController(
|
||||||
|
length: 2,
|
||||||
|
child: Scaffold(
|
||||||
|
backgroundColor: Colors.black,
|
||||||
|
appBar: PreferredSize(
|
||||||
|
preferredSize: const Size.fromHeight(kToolbarHeight),
|
||||||
|
child: Container(
|
||||||
|
color: Colors.black,
|
||||||
|
child: const TabBar(
|
||||||
|
tabs: [
|
||||||
|
Tab(text: 'All Calls'),
|
||||||
|
Tab(text: 'Missed Calls'),
|
||||||
|
],
|
||||||
|
indicatorColor: Colors.white,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
body: TabBarView(
|
||||||
|
children: [
|
||||||
|
// All Calls
|
||||||
|
_buildListView(allItems),
|
||||||
|
// Missed Calls
|
||||||
|
_buildListView(missedItems),
|
||||||
|
],
|
||||||
|
),
|
||||||
),
|
),
|
||||||
body: ListView.builder(
|
);
|
||||||
itemCount: histories.length,
|
}
|
||||||
itemBuilder: (context, index) {
|
|
||||||
final history = histories[index];
|
Widget _buildListView(List items) {
|
||||||
|
return ListView.builder(
|
||||||
|
itemCount: items.length,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
final item = items[index];
|
||||||
|
|
||||||
|
if (item is String) {
|
||||||
|
// This is a header item
|
||||||
|
return Container(
|
||||||
|
padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
|
||||||
|
color: Colors.grey[900],
|
||||||
|
child: Text(
|
||||||
|
item,
|
||||||
|
style: const TextStyle(
|
||||||
|
color: Colors.white70,
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else if (item is History) {
|
||||||
|
final history = item;
|
||||||
final contact = history.contact;
|
final contact = history.contact;
|
||||||
final isExpanded = _expandedIndex == index;
|
final isExpanded = _expandedIndex == index;
|
||||||
|
|
||||||
// Generate the avatar color using the same logic as the contacts page
|
// Generate the avatar color
|
||||||
Color avatarColor = generateColorFromName(contact.displayName);
|
Color avatarColor = generateColorFromName(contact.displayName);
|
||||||
|
|
||||||
return Column(
|
return Column(
|
||||||
@ -196,9 +270,9 @@ class _HistoryPageState extends State<HistoryPage> {
|
|||||||
children: [
|
children: [
|
||||||
TextButton.icon(
|
TextButton.icon(
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
if (contact.phones.isNotEmpty) {
|
if (history.contact.phones.isNotEmpty) {
|
||||||
final Uri smsUri =
|
final Uri smsUri =
|
||||||
Uri(scheme: 'sms', path: contact.phones.first.number);
|
Uri(scheme: 'sms', path: history.contact.phones.first.number);
|
||||||
if (await canLaunchUrl(smsUri)) {
|
if (await canLaunchUrl(smsUri)) {
|
||||||
await launchUrl(smsUri);
|
await launchUrl(smsUri);
|
||||||
} else {
|
} else {
|
||||||
@ -213,15 +287,28 @@ class _HistoryPageState extends State<HistoryPage> {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
icon: const Icon(Icons.message, color: Colors.white),
|
icon: const Icon(Icons.message, color: Colors.white),
|
||||||
label:
|
label: const Text('Message', style: TextStyle(color: Colors.white)),
|
||||||
const Text('Message', style: TextStyle(color: Colors.white)),
|
),
|
||||||
|
TextButton.icon(
|
||||||
|
onPressed: () {
|
||||||
|
// Navigate to Call Details page
|
||||||
|
Navigator.push(
|
||||||
|
context,
|
||||||
|
MaterialPageRoute(
|
||||||
|
builder: (_) => CallDetailsPage(history: history),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
icon: const Icon(Icons.info, color: Colors.white),
|
||||||
|
label: const Text('Details', style: TextStyle(color: Colors.white)),
|
||||||
),
|
),
|
||||||
TextButton.icon(
|
TextButton.icon(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
// Implement block number functionality
|
// Implement block number functionality
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
const SnackBar(
|
const SnackBar(
|
||||||
content: Text('Number blocked (functionality not implemented)')),
|
content: Text('Number blocked (functionality not implemented)'),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
icon: const Icon(Icons.block, color: Colors.white),
|
icon: const Icon(Icons.block, color: Colors.white),
|
||||||
@ -232,7 +319,120 @@ class _HistoryPageState extends State<HistoryPage> {
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
},
|
}
|
||||||
|
|
||||||
|
return const SizedBox.shrink();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class CallDetailsPage extends StatelessWidget {
|
||||||
|
final History history;
|
||||||
|
|
||||||
|
const CallDetailsPage({Key? key, required this.history}) : super(key: key);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final contact = history.contact;
|
||||||
|
|
||||||
|
return Scaffold(
|
||||||
|
backgroundColor: Colors.black,
|
||||||
|
appBar: AppBar(
|
||||||
|
title: const Text('Call Details'),
|
||||||
|
backgroundColor: Colors.black,
|
||||||
|
),
|
||||||
|
body: Padding(
|
||||||
|
padding: const EdgeInsets.all(16.0),
|
||||||
|
child: Column(
|
||||||
|
children: [
|
||||||
|
// Display Contact Name and Thumbnail
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
(contact.thumbnail != null && contact.thumbnail!.isNotEmpty)
|
||||||
|
? CircleAvatar(
|
||||||
|
backgroundImage: MemoryImage(contact.thumbnail!),
|
||||||
|
radius: 30,
|
||||||
|
)
|
||||||
|
: CircleAvatar(
|
||||||
|
backgroundColor: Colors.grey[700],
|
||||||
|
radius: 30,
|
||||||
|
child: Text(
|
||||||
|
contact.displayName.isNotEmpty
|
||||||
|
? contact.displayName[0].toUpperCase()
|
||||||
|
: '?',
|
||||||
|
style: const TextStyle(color: Colors.white),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 16),
|
||||||
|
Expanded(
|
||||||
|
child: Text(
|
||||||
|
contact.displayName,
|
||||||
|
style: const TextStyle(color: Colors.white, fontSize: 24),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
const SizedBox(height: 24),
|
||||||
|
|
||||||
|
// Display call type, status, date, attempts
|
||||||
|
DetailRow(
|
||||||
|
label: 'Call Type:',
|
||||||
|
value: history.callType,
|
||||||
|
),
|
||||||
|
DetailRow(
|
||||||
|
label: 'Call Status:',
|
||||||
|
value: history.callStatus,
|
||||||
|
),
|
||||||
|
DetailRow(
|
||||||
|
label: 'Date:',
|
||||||
|
value: DateFormat('MMM dd, yyyy - hh:mm a').format(history.date),
|
||||||
|
),
|
||||||
|
DetailRow(
|
||||||
|
label: 'Attempts:',
|
||||||
|
value: '${history.attempts}',
|
||||||
|
),
|
||||||
|
|
||||||
|
const SizedBox(height: 24),
|
||||||
|
|
||||||
|
// If you have more details like duration, contact number, etc.
|
||||||
|
if (contact.phones.isNotEmpty)
|
||||||
|
DetailRow(
|
||||||
|
label: 'Number:',
|
||||||
|
value: contact.phones.first.number,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class DetailRow extends StatelessWidget {
|
||||||
|
final String label;
|
||||||
|
final String value;
|
||||||
|
|
||||||
|
const DetailRow({Key? key, required this.label, required this.value}) : super(key: key);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Padding(
|
||||||
|
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
||||||
|
child: Row(
|
||||||
|
children: [
|
||||||
|
Text(
|
||||||
|
label,
|
||||||
|
style: const TextStyle(color: Colors.white70, fontWeight: FontWeight.bold),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 8),
|
||||||
|
Expanded(
|
||||||
|
child: Text(
|
||||||
|
value,
|
||||||
|
style: const TextStyle(color: Colors.white),
|
||||||
|
textAlign: TextAlign.right,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user